[clang-tools-extra] [clang-tidy] fix incorrect hint for InitListExpr in prefer-member-initializer (PR #81560)

Congcong Cai via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 15 07:38:13 PST 2024


https://github.com/HerrCai0907 updated https://github.com/llvm/llvm-project/pull/81560

>From 35dba54b1d6d158118c34d0f1bd8038b64c9bda4 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Tue, 13 Feb 2024 09:11:06 +0800
Subject: [PATCH 1/3] [clang-tidy] fix incorrect hint for InitListExpr in
 prefer-member-initializer

Fixes: #77684.
---
 .../PreferMemberInitializerCheck.cpp          |  5 ++++
 clang-tools-extra/docs/ReleaseNotes.rst       |  3 ++-
 .../prefer-member-initializer.cpp             | 23 +++++++++++++++++++
 3 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
index de96c3dc4efef7..7ede900d7ba7b1 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
@@ -203,6 +203,7 @@ void PreferMemberInitializerCheck::check(
     SourceLocation InsertPos;
     SourceRange ReplaceRange;
     bool AddComma = false;
+    bool AddBracket = false;
     bool InvalidFix = false;
     unsigned Index = Field->getFieldIndex();
     const CXXCtorInitializer *LastInListInit = nullptr;
@@ -216,6 +217,7 @@ void PreferMemberInitializerCheck::check(
         else {
           ReplaceRange = Init->getInit()->getSourceRange();
         }
+        AddBracket = isa<InitListExpr>(Init->getInit());
         break;
       }
       if (Init->isMemberInitializer() &&
@@ -279,6 +281,9 @@ void PreferMemberInitializerCheck::check(
     if (HasInitAlready) {
       if (InsertPos.isValid())
         Diag << FixItHint::CreateInsertion(InsertPos, NewInit);
+      else if (AddBracket)
+        Diag << FixItHint::CreateReplacement(ReplaceRange,
+                                             ("{" + NewInit + "}").str());
       else
         Diag << FixItHint::CreateReplacement(ReplaceRange, NewInit);
     } else {
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index ee68c8f49b3df2..f8bc85d78315bf 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -127,7 +127,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
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/prefer-member-initializer.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/prefer-member-initializer.cpp
index 8086caa2aa6f2c..63b4919608ff18 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/prefer-member-initializer.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/prefer-member-initializer.cpp
@@ -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;
+};
+}

>From 03ae7072afdf426406a4afd6e1e0a6b31c6ac00b Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Wed, 14 Feb 2024 23:17:12 +0800
Subject: [PATCH 2/3] fix comment

---
 .../cppcoreguidelines/PreferMemberInitializerCheck.cpp      | 6 +++---
 .../cppcoreguidelines/prefer-member-initializer.cpp         | 6 +++---
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
index 7ede900d7ba7b1..5f046c502eb383 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
@@ -203,7 +203,7 @@ void PreferMemberInitializerCheck::check(
     SourceLocation InsertPos;
     SourceRange ReplaceRange;
     bool AddComma = false;
-    bool AddBracket = false;
+    bool AddBrace = false;
     bool InvalidFix = false;
     unsigned Index = Field->getFieldIndex();
     const CXXCtorInitializer *LastInListInit = nullptr;
@@ -216,8 +216,8 @@ void PreferMemberInitializerCheck::check(
           InsertPos = Init->getRParenLoc();
         else {
           ReplaceRange = Init->getInit()->getSourceRange();
+          AddBrace = isa<InitListExpr>(Init->getInit());
         }
-        AddBracket = isa<InitListExpr>(Init->getInit());
         break;
       }
       if (Init->isMemberInitializer() &&
@@ -281,7 +281,7 @@ void PreferMemberInitializerCheck::check(
     if (HasInitAlready) {
       if (InsertPos.isValid())
         Diag << FixItHint::CreateInsertion(InsertPos, NewInit);
-      else if (AddBracket)
+      else if (AddBrace)
         Diag << FixItHint::CreateReplacement(ReplaceRange,
                                              ("{" + NewInit + "}").str());
       else
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/prefer-member-initializer.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/prefer-member-initializer.cpp
index 63b4919608ff18..f4dc58ed58efb8 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/prefer-member-initializer.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/prefer-member-initializer.cpp
@@ -621,13 +621,13 @@ 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\} }}
+// 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\} }}
+// CHECK-FIXES:  S2() : M{1}
   int M;
 };
 struct T { int a; int b; int c; };
@@ -635,7 +635,7 @@ 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\} }}
+// CHECK-FIXES:  S3() : M{v}
   T M;
 };
 }

>From ef27e5e26a53ecd1b014fe1491ba9736689b4ec5 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Thu, 15 Feb 2024 23:37:58 +0800
Subject: [PATCH 3/3] fix test

---
 .../cppcoreguidelines/prefer-member-initializer.cpp         | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/prefer-member-initializer.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/prefer-member-initializer.cpp
index f4dc58ed58efb8..e784a350c48f5a 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/prefer-member-initializer.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/prefer-member-initializer.cpp
@@ -621,13 +621,13 @@ 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}
+// 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}
+// CHECK-FIXES:  S2() : M{1} { }
   int M;
 };
 struct T { int a; int b; int c; };
@@ -635,7 +635,7 @@ 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}
+// CHECK-FIXES:  S3() : M{v} { }
   T M;
 };
 }



More information about the cfe-commits mailing list