[clang-tools-extra] 894140b - [clang-tidy] Ignore delegate constructors in cppcoreguidelines-pro-type-member-init

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 10 13:12:34 PDT 2023


Author: Piotr Zegar
Date: 2023-08-10T19:51:09Z
New Revision: 894140b4fdde10b6bff7fc99104b2292ade3c91c

URL: https://github.com/llvm/llvm-project/commit/894140b4fdde10b6bff7fc99104b2292ade3c91c
DIFF: https://github.com/llvm/llvm-project/commit/894140b4fdde10b6bff7fc99104b2292ade3c91c.diff

LOG: [clang-tidy] Ignore delegate constructors in cppcoreguidelines-pro-type-member-init

Ignore dependend delegate constructors.

Fixes: #37250

Reviewed By: ccotter

Differential Revision: https://reviews.llvm.org/D157367

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
index 07b88d5c49e80a..ff5a750a8dac45 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
@@ -281,8 +281,14 @@ ProTypeMemberInitCheck::ProTypeMemberInitCheck(StringRef Name,
 
 void ProTypeMemberInitCheck::registerMatchers(MatchFinder *Finder) {
   auto IsUserProvidedNonDelegatingConstructor =
-      allOf(isUserProvided(),
-            unless(anyOf(isInstantiated(), isDelegatingConstructor())));
+      allOf(isUserProvided(), unless(isInstantiated()),
+            unless(isDelegatingConstructor()),
+            ofClass(cxxRecordDecl().bind("parent")),
+            unless(hasAnyConstructorInitializer(cxxCtorInitializer(
+                isWritten(), unless(isMemberInitializer()),
+                hasTypeLoc(loc(
+                    qualType(hasDeclaration(equalsBoundNode("parent")))))))));
+
   auto IsNonTrivialDefaultConstructor = allOf(
       isDefaultConstructor(), unless(isUserProvided()),
       hasParent(cxxRecordDecl(unless(isTriviallyDefaultConstructible()))));

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 7f3ae301081231..703d8b0edd8cc9 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -176,6 +176,10 @@ Changes in existing checks
   <clang-tidy/checks/cppcoreguidelines/prefer-member-initializer>` check to
   ignore delegate constructors.
 
+- Improved :doc:`cppcoreguidelines-pro-type-member-init
+  <clang-tidy/checks/cppcoreguidelines/pro-type-member-init>` check to ignore
+  dependent delegate constructors.
+
 - Improved :doc:`cppcoreguidelines-pro-type-vararg
   <clang-tidy/checks/cppcoreguidelines/pro-type-vararg>` check to ignore
   false-positives in unevaluated context (e.g., ``decltype``, ``sizeof``, ...).

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init.cpp
index 2e2097bae361a7..8d6992afef08a9 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init.cpp
@@ -372,8 +372,7 @@ template <typename T>
 class PositiveSelfInitialization : NegativeAggregateType
 {
   PositiveSelfInitialization() : PositiveSelfInitialization() {}
-  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: constructor does not initialize these bases: NegativeAggregateType
-  // CHECK-FIXES: PositiveSelfInitialization() : NegativeAggregateType(), PositiveSelfInitialization() {}
+  // This will be detected by -Wdelegating-ctor-cycles and there is no proper way to fix this
 };
 
 class PositiveIndirectMember {
@@ -579,3 +578,42 @@ struct S3 {
     int C = 0;
   };
 };
+
+// Ignore issues from delegate constructors
+namespace PR37250 {
+  template <typename T>
+  struct A {
+    A() : A(42) {}
+    explicit A(int value) : value_(value) {}
+    int value_;
+  };
+
+  struct B {
+    B() : B(42) {}
+    explicit B(int value) : value_(value) {}
+    int value_;
+  };
+
+  template <typename T>
+  struct C {
+    C() : C(T()) {}
+    explicit C(T value) : value_(value) {}
+    T value_;
+  };
+
+  struct V {
+    unsigned size() const;
+  };
+
+  struct S {
+    unsigned size_;
+
+    S(unsigned size) : size_{size} {}
+
+    template<typename U>
+    S(const U& u) : S(u.size()) {}
+  };
+
+  const V v;
+  const S s{v};
+}


        


More information about the cfe-commits mailing list