Index: clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.h =================================================================== --- clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.h +++ clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.h @@ -65,6 +65,9 @@ // Whether arrays need to be initialized or not. Default is false. bool IgnoreArrays; + // Whether fix-its for initializers of fundamental type use literals. Only + // effective in C++11 mode. Default is false. + bool LiteralInitializers; }; } // namespace cppcoreguidelines Index: clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp =================================================================== --- clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp +++ clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp @@ -255,7 +255,8 @@ ProTypeMemberInitCheck::ProTypeMemberInitCheck(StringRef Name, ClangTidyContext *Context) : ClangTidyCheck(Name, Context), - IgnoreArrays(Options.get("IgnoreArrays", false)) {} + IgnoreArrays(Options.get("IgnoreArrays", false)), + LiteralInitializers(Options.get("LiteralInitializers", false)) {} void ProTypeMemberInitCheck::registerMatchers(MatchFinder *Finder) { if (!getLangOpts().CPlusPlus) @@ -319,6 +320,7 @@ void ProTypeMemberInitCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { Options.store(Opts, "IgnoreArrays", IgnoreArrays); + Options.store(Opts, "LiteralInitializers", LiteralInitializers); } // FIXME: Copied from clang/lib/Sema/SemaDeclCXX.cpp. @@ -343,6 +345,56 @@ return isIncompleteOrZeroLengthArrayType(Context, Type); } +static const char *getInitializer(QualType type, bool LiteralInitializers) { + const char *DefaultInitializer = "{}"; + if (!LiteralInitializers) + return DefaultInitializer; + + if (type->isPointerType()) + return " = nullptr"; + + const BuiltinType *BT = + dyn_cast(type->getCanonicalTypeInternal()); + if (!BT) + return DefaultInitializer; + + switch (BT->getKind()) { + case BuiltinType::Bool: + return " = false"; + case BuiltinType::Float: + return " = 0.0f"; + case BuiltinType::Double: + return " = 0.0"; + case BuiltinType::LongDouble: + return " = 0.0l"; + case BuiltinType::SChar: + case BuiltinType::Char_S: + case BuiltinType::WChar_S: + case BuiltinType::Char16: + case BuiltinType::Char32: + case BuiltinType::Short: + case BuiltinType::Int: + return " = 0"; + case BuiltinType::UChar: + case BuiltinType::Char_U: + case BuiltinType::WChar_U: + case BuiltinType::UShort: + case BuiltinType::UInt: + return " = 0u"; + case BuiltinType::Long: + return " = 0l"; + case BuiltinType::ULong: + return " = 0ul"; + case BuiltinType::LongLong: + return " = 0ll"; + case BuiltinType::ULongLong: + return " = 0ull"; + + default: + return DefaultInitializer; + } +} + void ProTypeMemberInitCheck::checkMissingMemberInitializer( ASTContext &Context, const CXXRecordDecl &ClassDecl, const CXXConstructorDecl *Ctor) { @@ -419,7 +471,7 @@ for (const FieldDecl *Field : FieldsToFix) { Diag << FixItHint::CreateInsertion( getLocationForEndOfToken(Context, Field->getSourceRange().getEnd()), - "{}"); + getInitializer(Field->getType(), LiteralInitializers)); } } else if (Ctor) { // Otherwise, rewrite the constructor's initializer list. Index: docs/ReleaseNotes.rst =================================================================== --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -67,6 +67,10 @@ Allow custom memory management functions to be considered as well. +- Added `LiteralInitializers` option to `cppcoreguidelines-pro-type-member-init` + If set to true, the check will provide fix-its with literal initializers + (``int i = 0;``) instead of curly braces (``int i{};``). + - New `readability-misleading-indentation `_ check Index: docs/clang-tidy/checks/cppcoreguidelines-pro-type-member-init.rst =================================================================== --- docs/clang-tidy/checks/cppcoreguidelines-pro-type-member-init.rst +++ docs/clang-tidy/checks/cppcoreguidelines-pro-type-member-init.rst @@ -33,6 +33,10 @@ zero-initialized during construction. For performance critical code, it may be important to not initialize fixed-size array members. Default is `0`. +.. option:: LiteralInitializers + If set to non-zero, the check will provide fix-its with literal initializers + (``int i = 0;``) instead of curly braces (``int i{};``). + This rule is part of the "Type safety" profile of the C++ Core Guidelines, corresponding to rule Type.6. See https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Pro-type-memberinit. Index: test/clang-tidy/cppcoreguidelines-pro-type-member-init-literal-initializers.cpp =================================================================== --- /dev/null +++ test/clang-tidy/cppcoreguidelines-pro-type-member-init-literal-initializers.cpp @@ -0,0 +1,40 @@ +// RUN: %check_clang_tidy %s cppcoreguidelines-pro-type-member-init %t -- -config="{CheckOptions: [{key: "cppcoreguidelines-pro-type-member-init.LiteralInitializers", value: 1}]}" -- -std=c++11 + +struct T { + int i; +}; + +struct S { + bool b; + // CHECK-FIXES: bool b = false; + char c; + // CHECK-FIXES: char c = 0; + signed char sc; + // CHECK-FIXES: signed char sc = 0; + unsigned char uc; + // CHECK-FIXES: unsigned char uc = 0u; + int i; + // CHECK-FIXES: int i = 0; + unsigned u; + // CHECK-FIXES: unsigned u = 0u; + long l; + // CHECK-FIXES: long l = 0l; + unsigned long ul; + // CHECK-FIXES: unsigned long ul = 0ul; + long long ll; + // CHECK-FIXES: long long ll = 0ll; + unsigned long long ull; + // CHECK-FIXES: unsigned long long ull = 0ull; + float f; + // CHECK-FIXES: float f = 0.0f; + double d; + // CHECK-FIXES: double d = 0.0; + long double ld; + // CHECK-FIXES: double ld = 0.0l; + int *ptr; + // CHECK-FIXES: int *ptr = nullptr; + T t; + // CHECK-FIXES: T t{}; + S() {}; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: constructor does not initialize these fields: +};