Skip to content

Commit 326d749

Browse files
authored
[clang-tidy] Fix cppcoreguidelines-prefer-member-initializer false positive for inherited members (#153941)
```cpp struct Base { int m; }; template <class T> struct Derived : Base { Derived() { m = 0; } }; ``` would previously generate the following output: ``` <source>:7:15: warning: 'm' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer] 7 | Derived() { m = 0; } | ^~~~~~ | : m(0) ``` This patch fixes this false positive. Note that before this patch the checker won't give false positive for ```cpp struct Derived : Base { Derived() { m = 0; } }; ``` and the constructor's AST is ``` `-CXXConstructorDecl 0x557df03d1fb0 <line:7:3, col:22> col:3 Derived 'void ()' implicit-inline |-CXXCtorInitializer 'Base' | `-CXXConstructExpr 0x557df03d2748 <col:3> 'Base' 'void () noexcept' `-CompoundStmt 0x557df03d2898 <col:13, col:22> `-BinaryOperator 0x557df03d2878 <col:15, col:19> 'int' lvalue '=' |-MemberExpr 0x557df03d2828 <col:15> 'int' lvalue ->m 0x557df03d1c40 | `-ImplicitCastExpr 0x557df03d2808 <col:15> 'Base *' <UncheckedDerivedToBase (Base)> | `-CXXThisExpr 0x557df03d27f8 <col:15> 'Derived *' implicit this `-IntegerLiteral 0x557df03d2858 <col:19> 'int' 0 ``` so `isAssignmentToMemberOf` would return empty due to https://github.com/llvm/llvm-project/blob/f0967fca04c880e9aabd5be043a85127faabb4c6/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp#L118-L119 Fixes #104400
1 parent bd77e9a commit 326d749

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ void PreferMemberInitializerCheck::check(
191191
if (!AssignmentToMember)
192192
continue;
193193
const FieldDecl *Field = AssignmentToMember->Field;
194+
// Skip if the field is inherited from a base class.
195+
if (Field->getParent() != Class)
196+
continue;
194197
const Expr *InitValue = AssignmentToMember->Init;
195198
updateAssignmentLevel(Field, InitValue, Ctor, AssignedFields);
196199
if (!canAdvanceAssignment(AssignedFields[Field]))

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ Changes in existing checks
167167
an additional matcher that generalizes the copy-and-swap idiom pattern
168168
detection.
169169

170+
- Improved :doc:`cppcoreguidelines-prefer-member-initializer
171+
<clang-tidy/checks/cppcoreguidelines/prefer-member-initializer>` check to
172+
avoid false positives on inherited members in class templates.
173+
170174
- Improved :doc:`misc-header-include-cycle
171175
<clang-tidy/checks/misc/header-include-cycle>` check performance.
172176

clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/prefer-member-initializer.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,3 +650,16 @@ struct InitFromBindingDecl {
650650
}
651651
};
652652
} // namespace GH82970
653+
654+
struct A {
655+
int m;
656+
};
657+
658+
struct B : A {
659+
B() { m = 0; }
660+
};
661+
662+
template <class T>
663+
struct C : A {
664+
C() { m = 0; }
665+
};

0 commit comments

Comments
 (0)