[clang-tools-extra] 461dcd4 - [clang-tidy] Fix handling of members in readability-redundant-member-init (#93217)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Jun 5 11:17:37 PDT 2024
Author: Piotr Zegar
Date: 2024-06-05T20:17:32+02:00
New Revision: 461dcd4a000fa2b88759a275bc6803b89efc5972
URL: https://github.com/llvm/llvm-project/commit/461dcd4a000fa2b88759a275bc6803b89efc5972
DIFF: https://github.com/llvm/llvm-project/commit/461dcd4a000fa2b88759a275bc6803b89efc5972.diff
LOG: [clang-tidy] Fix handling of members in readability-redundant-member-init (#93217)
Compare class type instead of just assuming
that called constructor belong to same class.
Fixes #91605
Added:
Modified:
clang-tools-extra/clang-tidy/readability/RedundantMemberInitCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/test/clang-tidy/checkers/readability/redundant-member-init.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantMemberInitCheck.cpp
index 015347ee9294c..601ff44cdd10a 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantMemberInitCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantMemberInitCheck.cpp
@@ -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);
}
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 33b65caf2b02c..6947cf06f6e56 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -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
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-member-init.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-member-init.cpp
index 17b2714abca07..6f18a6043be93 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-member-init.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-member-init.cpp
@@ -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;
+};
More information about the cfe-commits
mailing list