Skip to content

Commit

Permalink
[clang-tidy] fix incorrect hint for InitListExpr in prefer-member-ini…
Browse files Browse the repository at this point in the history
…tializer (#81560)
  • Loading branch information
HerrCai0907 committed Feb 16, 2024
1 parent e606dc1 commit e9cec39
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ void PreferMemberInitializerCheck::check(
SourceLocation InsertPos;
SourceRange ReplaceRange;
bool AddComma = false;
bool AddBrace = false;
bool InvalidFix = false;
unsigned Index = Field->getFieldIndex();
const CXXCtorInitializer *LastInListInit = nullptr;
Expand All @@ -215,6 +216,7 @@ void PreferMemberInitializerCheck::check(
InsertPos = Init->getRParenLoc();
else {
ReplaceRange = Init->getInit()->getSourceRange();
AddBrace = isa<InitListExpr>(Init->getInit());
}
break;
}
Expand Down Expand Up @@ -279,6 +281,9 @@ void PreferMemberInitializerCheck::check(
if (HasInitAlready) {
if (InsertPos.isValid())
Diag << FixItHint::CreateInsertion(InsertPos, NewInit);
else if (AddBrace)
Diag << FixItHint::CreateReplacement(ReplaceRange,
("{" + NewInit + "}").str());
else
Diag << FixItHint::CreateReplacement(ReplaceRange, NewInit);
} else {
Expand Down
3 changes: 2 additions & 1 deletion clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ Changes in existing checks
<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c48-prefer-in-class-initializers-to-member-initializers-in-constructors-for-constant-initializers>`_,
which was deprecated since :program:`clang-tidy` 17. This rule is now covered
by :doc:`cppcoreguidelines-use-default-member-init
<clang-tidy/checks/cppcoreguidelines/use-default-member-init>`.
<clang-tidy/checks/cppcoreguidelines/use-default-member-init>` and fixes
incorrect hints when using list-initialization.

- Improved :doc:`google-build-namespaces
<clang-tidy/checks/google/build-namespaces>` check by replacing the local
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,3 +616,26 @@ class Foo {
#undef INVALID_HANDLE_VALUE
#undef RGB
}

namespace GH77684 {
struct S1 {
// CHECK-MESSAGES: :[[@LINE+1]]:16: warning: 'M' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
S1() : M{} { M = 0; }
// CHECK-FIXES: S1() : M{0} { }
int M;
};
struct S2 {
// CHECK-MESSAGES: :[[@LINE+1]]:17: warning: 'M' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
S2() : M{2} { M = 1; }
// CHECK-FIXES: S2() : M{1} { }
int M;
};
struct T { int a; int b; int c; };
T v;
struct S3 {
// CHECK-MESSAGES: :[[@LINE+1]]:21: warning: 'M' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
S3() : M{1,2,3} { M = v; }
// CHECK-FIXES: S3() : M{v} { }
T M;
};
}

0 comments on commit e9cec39

Please sign in to comment.