Skip to content

Commit e9cec39

Browse files
authored
[clang-tidy] fix incorrect hint for InitListExpr in prefer-member-initializer (#81560)
1 parent e606dc1 commit e9cec39

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ void PreferMemberInitializerCheck::check(
203203
SourceLocation InsertPos;
204204
SourceRange ReplaceRange;
205205
bool AddComma = false;
206+
bool AddBrace = false;
206207
bool InvalidFix = false;
207208
unsigned Index = Field->getFieldIndex();
208209
const CXXCtorInitializer *LastInListInit = nullptr;
@@ -215,6 +216,7 @@ void PreferMemberInitializerCheck::check(
215216
InsertPos = Init->getRParenLoc();
216217
else {
217218
ReplaceRange = Init->getInit()->getSourceRange();
219+
AddBrace = isa<InitListExpr>(Init->getInit());
218220
}
219221
break;
220222
}
@@ -279,6 +281,9 @@ void PreferMemberInitializerCheck::check(
279281
if (HasInitAlready) {
280282
if (InsertPos.isValid())
281283
Diag << FixItHint::CreateInsertion(InsertPos, NewInit);
284+
else if (AddBrace)
285+
Diag << FixItHint::CreateReplacement(ReplaceRange,
286+
("{" + NewInit + "}").str());
282287
else
283288
Diag << FixItHint::CreateReplacement(ReplaceRange, NewInit);
284289
} else {

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ Changes in existing checks
131131
<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c48-prefer-in-class-initializers-to-member-initializers-in-constructors-for-constant-initializers>`_,
132132
which was deprecated since :program:`clang-tidy` 17. This rule is now covered
133133
by :doc:`cppcoreguidelines-use-default-member-init
134-
<clang-tidy/checks/cppcoreguidelines/use-default-member-init>`.
134+
<clang-tidy/checks/cppcoreguidelines/use-default-member-init>` and fixes
135+
incorrect hints when using list-initialization.
135136

136137
- Improved :doc:`google-build-namespaces
137138
<clang-tidy/checks/google/build-namespaces>` check by replacing the local

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,3 +616,26 @@ class Foo {
616616
#undef INVALID_HANDLE_VALUE
617617
#undef RGB
618618
}
619+
620+
namespace GH77684 {
621+
struct S1 {
622+
// CHECK-MESSAGES: :[[@LINE+1]]:16: warning: 'M' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
623+
S1() : M{} { M = 0; }
624+
// CHECK-FIXES: S1() : M{0} { }
625+
int M;
626+
};
627+
struct S2 {
628+
// CHECK-MESSAGES: :[[@LINE+1]]:17: warning: 'M' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
629+
S2() : M{2} { M = 1; }
630+
// CHECK-FIXES: S2() : M{1} { }
631+
int M;
632+
};
633+
struct T { int a; int b; int c; };
634+
T v;
635+
struct S3 {
636+
// CHECK-MESSAGES: :[[@LINE+1]]:21: warning: 'M' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
637+
S3() : M{1,2,3} { M = v; }
638+
// CHECK-FIXES: S3() : M{v} { }
639+
T M;
640+
};
641+
}

0 commit comments

Comments
 (0)