Skip to content

Commit 461dcd4

Browse files
authored
[clang-tidy] Fix handling of members in readability-redundant-member-init (llvm#93217)
Compare class type instead of just assuming that called constructor belong to same class. Fixes llvm#91605
1 parent 31ba25e commit 461dcd4

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

clang-tools-extra/clang-tidy/readability/RedundantMemberInitCheck.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,35 @@ void RedundantMemberInitCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
4141

4242
void RedundantMemberInitCheck::registerMatchers(MatchFinder *Finder) {
4343
auto ConstructorMatcher =
44-
cxxConstructExpr(argumentCountIs(0),
45-
hasDeclaration(cxxConstructorDecl(ofClass(cxxRecordDecl(
46-
unless(isTriviallyDefaultConstructible()))))))
44+
cxxConstructExpr(
45+
argumentCountIs(0),
46+
hasDeclaration(cxxConstructorDecl(
47+
ofClass(cxxRecordDecl(unless(isTriviallyDefaultConstructible()))
48+
.bind("class")))))
4749
.bind("construct");
4850

51+
auto HasUnionAsParent = hasParent(recordDecl(isUnion()));
52+
53+
auto HasTypeEqualToConstructorClass = hasType(qualType(
54+
hasCanonicalType(qualType(hasDeclaration(equalsBoundNode("class"))))));
55+
4956
Finder->addMatcher(
5057
cxxConstructorDecl(
5158
unless(isDelegatingConstructor()), ofClass(unless(isUnion())),
5259
forEachConstructorInitializer(
53-
cxxCtorInitializer(withInitializer(ConstructorMatcher),
54-
unless(forField(fieldDecl(
55-
anyOf(hasType(isConstQualified()),
56-
hasParent(recordDecl(isUnion())))))))
60+
cxxCtorInitializer(
61+
withInitializer(ConstructorMatcher),
62+
anyOf(isBaseInitializer(),
63+
forField(fieldDecl(unless(hasType(isConstQualified())),
64+
unless(HasUnionAsParent),
65+
HasTypeEqualToConstructorClass))))
5766
.bind("init")))
5867
.bind("constructor"),
5968
this);
6069

6170
Finder->addMatcher(fieldDecl(hasInClassInitializer(ConstructorMatcher),
62-
unless(hasParent(recordDecl(isUnion()))))
71+
HasTypeEqualToConstructorClass,
72+
unless(HasUnionAsParent))
6373
.bind("field"),
6474
this);
6575
}

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,11 @@ Changes in existing checks
403403
<clang-tidy/checks/readability/redundant-inline-specifier>` check to properly
404404
emit warnings for static data member with an in-class initializer.
405405

406+
- Improved :doc:`readability-redundant-member-init
407+
<clang-tidy/checks/readability/redundant-member-init>` check to avoid
408+
false-positives when type of the member does not match the type of the
409+
initializer.
410+
406411
- Improved :doc:`readability-static-accessed-through-instance
407412
<clang-tidy/checks/readability/static-accessed-through-instance>` check to
408413
support calls to overloaded operators as base expression and provide fixes to

clang-tools-extra/test/clang-tidy/checkers/readability/redundant-member-init.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,19 @@ struct D7 {
302302

303303
D7<int> d7i;
304304
D7<S> d7s;
305+
306+
struct SS {
307+
SS() = default;
308+
SS(S s) : s(s) {}
309+
310+
S s;
311+
};
312+
313+
struct D8 {
314+
SS ss = S();
315+
};
316+
317+
struct D9 {
318+
D9() : ss(S()) {}
319+
SS ss;
320+
};

0 commit comments

Comments
 (0)