Skip to content

Commit

Permalink
[clang-tidy] Fix handling of members in readability-redundant-member-…
Browse files Browse the repository at this point in the history
…init (llvm#93217)

Compare class type instead of just assuming
that called constructor belong to same class.

Fixes llvm#91605
  • Loading branch information
PiotrZSL authored Jun 5, 2024
1 parent 31ba25e commit 461dcd4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,35 @@ void RedundantMemberInitCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {

void RedundantMemberInitCheck::registerMatchers(MatchFinder *Finder) {
auto ConstructorMatcher =
cxxConstructExpr(argumentCountIs(0),
hasDeclaration(cxxConstructorDecl(ofClass(cxxRecordDecl(
unless(isTriviallyDefaultConstructible()))))))
cxxConstructExpr(
argumentCountIs(0),
hasDeclaration(cxxConstructorDecl(
ofClass(cxxRecordDecl(unless(isTriviallyDefaultConstructible()))
.bind("class")))))
.bind("construct");

auto HasUnionAsParent = hasParent(recordDecl(isUnion()));

auto HasTypeEqualToConstructorClass = hasType(qualType(
hasCanonicalType(qualType(hasDeclaration(equalsBoundNode("class"))))));

Finder->addMatcher(
cxxConstructorDecl(
unless(isDelegatingConstructor()), ofClass(unless(isUnion())),
forEachConstructorInitializer(
cxxCtorInitializer(withInitializer(ConstructorMatcher),
unless(forField(fieldDecl(
anyOf(hasType(isConstQualified()),
hasParent(recordDecl(isUnion())))))))
cxxCtorInitializer(
withInitializer(ConstructorMatcher),
anyOf(isBaseInitializer(),
forField(fieldDecl(unless(hasType(isConstQualified())),
unless(HasUnionAsParent),
HasTypeEqualToConstructorClass))))
.bind("init")))
.bind("constructor"),
this);

Finder->addMatcher(fieldDecl(hasInClassInitializer(ConstructorMatcher),
unless(hasParent(recordDecl(isUnion()))))
HasTypeEqualToConstructorClass,
unless(HasUnionAsParent))
.bind("field"),
this);
}
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,11 @@ Changes in existing checks
<clang-tidy/checks/readability/redundant-inline-specifier>` check to properly
emit warnings for static data member with an in-class initializer.

- Improved :doc:`readability-redundant-member-init
<clang-tidy/checks/readability/redundant-member-init>` check to avoid
false-positives when type of the member does not match the type of the
initializer.

- Improved :doc:`readability-static-accessed-through-instance
<clang-tidy/checks/readability/static-accessed-through-instance>` check to
support calls to overloaded operators as base expression and provide fixes to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,19 @@ struct D7 {

D7<int> d7i;
D7<S> d7s;

struct SS {
SS() = default;
SS(S s) : s(s) {}

S s;
};

struct D8 {
SS ss = S();
};

struct D9 {
D9() : ss(S()) {}
SS ss;
};

0 comments on commit 461dcd4

Please sign in to comment.