diff options
Diffstat (limited to 'clang-tools-extra')
4 files changed, 44 insertions, 12 deletions
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp index 67a2c3d5d52..b07f730380e 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp @@ -166,21 +166,22 @@ void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) { cxxConstructorDecl( isDefaultConstructor(), unless(isInstantiated()), forEachConstructorInitializer( - allOf(forField(unless(anyOf(isBitField(), - hasInClassInitializer(anything())))), - cxxCtorInitializer(isWritten(), - withInitializer(ignoringImplicit(Init))) - .bind("default")))), + cxxCtorInitializer( + forField(unless(anyOf(isBitField(), + hasInClassInitializer(anything()), + hasParent(recordDecl(isUnion()))))), + isWritten(), withInitializer(ignoringImplicit(Init))) + .bind("default"))), this); Finder->addMatcher( cxxConstructorDecl( unless(ast_matchers::isTemplateInstantiation()), forEachConstructorInitializer( - allOf(forField(hasInClassInitializer(anything())), - cxxCtorInitializer(isWritten(), - withInitializer(ignoringImplicit(Init))) - .bind("existing")))), + cxxCtorInitializer(forField(hasInClassInitializer(anything())), + isWritten(), + withInitializer(ignoringImplicit(Init))) + .bind("existing"))), this); } @@ -197,7 +198,7 @@ void UseDefaultMemberInitCheck::check(const MatchFinder::MatchResult &Result) { void UseDefaultMemberInitCheck::checkDefaultInit( const MatchFinder::MatchResult &Result, const CXXCtorInitializer *Init) { - const FieldDecl *Field = Init->getMember(); + const FieldDecl *Field = Init->getAnyMember(); SourceLocation StartLoc = Field->getLocStart(); if (StartLoc.isMacroID() && IgnoreMacros) diff --git a/clang-tools-extra/clang-tidy/readability/RedundantMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantMemberInitCheck.cpp index 6d3650059d4..8409f9f40d2 100644 --- a/clang-tools-extra/clang-tidy/readability/RedundantMemberInitCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/RedundantMemberInitCheck.cpp @@ -39,7 +39,8 @@ void RedundantMemberInitCheck::registerMatchers(MatchFinder *Finder) { forEachConstructorInitializer( cxxCtorInitializer(isWritten(), withInitializer(ignoringImplicit(Construct)), - unless(forField(hasType(isConstQualified())))) + unless(forField(hasType(isConstQualified()))), + unless(forField(hasParent(recordDecl(isUnion()))))) .bind("init"))), this); } @@ -52,7 +53,7 @@ void RedundantMemberInitCheck::check(const MatchFinder::MatchResult &Result) { Construct->getArg(0)->isDefaultArgument()) { if (Init->isAnyMemberInitializer()) { diag(Init->getSourceLocation(), "initializer for member %0 is redundant") - << Init->getMember() + << Init->getAnyMember() << FixItHint::CreateRemoval(Init->getSourceRange()); } else { diag(Init->getSourceLocation(), diff --git a/clang-tools-extra/test/clang-tidy/modernize-use-default-member-init.cpp b/clang-tools-extra/test/clang-tidy/modernize-use-default-member-init.cpp index 5b9b93c42a7..0ed65df3ed6 100644 --- a/clang-tools-extra/test/clang-tidy/modernize-use-default-member-init.cpp +++ b/clang-tools-extra/test/clang-tidy/modernize-use-default-member-init.cpp @@ -173,6 +173,16 @@ struct PositiveString { // CHECK-FIXES: const char *s{"foo"}; }; +struct PositiveStruct { + PositiveStruct() : s(7) {} + // CHECK-FIXES: PositiveStruct() {} + struct { + int s; + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use default member initializer for 's' + // CHECK-FIXES: int s{7}; + }; +}; + template <typename T> struct NegativeTemplate { NegativeTemplate() : t() {} diff --git a/clang-tools-extra/test/clang-tidy/readability-redundant-member-init.cpp b/clang-tools-extra/test/clang-tidy/readability-redundant-member-init.cpp index f15cb1b9d43..90b52fd1a48 100644 --- a/clang-tools-extra/test/clang-tidy/readability-redundant-member-init.cpp +++ b/clang-tools-extra/test/clang-tidy/readability-redundant-member-init.cpp @@ -105,6 +105,17 @@ struct F8 : Foo::Template<N_THINGS> { // CHECK-FIXES: F8() {} }; +// Anonymous struct +struct F9 { + F9() : s1() {} + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: initializer for member 's1' is redundant + // CHECK-FIXES: F9() {} + struct { + S s1; + S s2; + }; +}; + // Initializer not written struct NF1 { NF1() {} @@ -197,3 +208,12 @@ struct NF14 { NF14() : f() {} V f; }; + +// Anonymous union member +struct NF15 { + NF15() : s1() {} + union { + S s1; + S s2; + }; +}; |