Skip to content

Commit 7deca85

Browse files
authored
[clang-tidy] fix false positive when member initialization depends on structured binging variable in cppcoreguidelines-prefer-member-initializer (#108743)
Fixes: #82970 Detecting dependiences with `varDecl` is too strict. It will ignore the `bingingDecl`. This patch wants to use `valueDecl` to match more cases including `bingingDecl`.
1 parent 0b041f1 commit 7deca85

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static void updateAssignmentLevel(
8383
memberExpr(hasObjectExpression(cxxThisExpr()),
8484
member(fieldDecl(indexNotLessThan(Field->getFieldIndex()))));
8585
auto DeclMatcher = declRefExpr(
86-
to(varDecl(unless(parmVarDecl()), hasDeclContext(equalsNode(Ctor)))));
86+
to(valueDecl(unless(parmVarDecl()), hasDeclContext(equalsNode(Ctor)))));
8787
const bool HasDependence = !match(expr(anyOf(MemberMatcher, DeclMatcher,
8888
hasDescendant(MemberMatcher),
8989
hasDescendant(DeclMatcher))),

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ Changes in existing checks
111111
<clang-tidy/checks/bugprone/casting-through-void>` check to suggest replacing
112112
the offending code with ``reinterpret_cast``, to more clearly express intent.
113113

114+
- Improved :doc:`cppcoreguidelines-prefer-member-initializer
115+
<clang-tidy/checks/cppcoreguidelines/prefer-member-initializer>` check to avoid
116+
false positive when member initialization depends on a structured binging
117+
variable.
118+
114119
- Improved :doc:`modernize-use-std-format
115120
<clang-tidy/checks/modernize/use-std-format>` check to support replacing
116121
member function calls too.

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,3 +639,14 @@ struct S3 {
639639
T M;
640640
};
641641
}
642+
643+
namespace GH82970 {
644+
struct InitFromBingingDecl {
645+
int m;
646+
InitFromBingingDecl() {
647+
struct { int i; } a;
648+
auto [n] = a;
649+
m = n;
650+
}
651+
};
652+
} // namespace GH82970

0 commit comments

Comments
 (0)