[clang-tools-extra] [clang-tidy] Fix crash in `cppcoreguidelines-pro-type-member-init` with alias template in constructor initializer (PR #192786)

via cfe-commits cfe-commits at lists.llvm.org
Sat Apr 18 04:21:47 PDT 2026


https://github.com/flovent created https://github.com/llvm/llvm-project/pull/192786

Closes #192510.

>From a6697685185835cbe5102f1f8bd0af347d3e000f Mon Sep 17 00:00:00 2001
From: flovent <flbven at protonmail.com>
Date: Sat, 18 Apr 2026 19:18:06 +0800
Subject: [PATCH 1/2] [clang-tidy] Fix crash in
 `cppcoreguidelines-pro-type-member-init` with alias template in constructor
 initializer

---
 .../ProTypeMemberInitCheck.cpp                | 10 +++++++---
 clang-tools-extra/docs/ReleaseNotes.rst       |  9 ++++++---
 .../pro-type-member-init-no-crash.cpp         | 19 +++++++++++++++++++
 3 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
index 07d71968a07b8..74cd62cd869f8 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
@@ -571,9 +571,13 @@ void ProTypeMemberInitCheck::checkMissingBaseClassInitializer(
       return;
 
     for (const CXXCtorInitializer *Init : Ctor->inits())
-      if (Init->isBaseInitializer() && Init->isWritten())
-        BasesToInit.erase(
-            Init->getBaseClass()->getAsCXXRecordDecl()->getCanonicalDecl());
+      if (Init->isBaseInitializer() && Init->isWritten()) {
+        // In template AST BaseInitializer could be generated too even if it's
+        // not target to base class.
+        if (const CXXRecordDecl *CRD =
+                Init->getBaseClass()->getAsCXXRecordDecl())
+          BasesToInit.erase(CRD->getCanonicalDecl());
+      }
   }
 
   if (BasesToInit.empty())
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 95ed0061d654c..980939c6ae0a3 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -344,9 +344,12 @@ Changes in existing checks
     lambda captures such as ``[t{std::forward<T>(t)}]``.
 
 - Improved :doc:`cppcoreguidelines-pro-type-member-init
-  <clang-tidy/checks/cppcoreguidelines/pro-type-member-init>` check by fixing
-  a false positive when a base class has a forward declaration before its
-  definition.
+  <clang-tidy/checks/cppcoreguidelines/pro-type-member-init>` check:
+
+  - Fixed a false positive when a base class has a forward declaration before
+    its definition.
+
+  - Fixed a crash with alias template in constructor initializer.
 
 - Improved :doc:`cppcoreguidelines-pro-type-vararg
   <clang-tidy/checks/cppcoreguidelines/pro-type-vararg>` check by no longer
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init-no-crash.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init-no-crash.cpp
index 2e2964dda1daf..f5ba3a3bd134f 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init-no-crash.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init-no-crash.cpp
@@ -13,3 +13,22 @@ template <typename T> class NoCrash {
     template <typename U> B(U u) {}
   };
 };
+
+namespace gh192510 {
+  template<typename T>
+  struct C {
+
+  };
+
+  struct Base {  
+    int x;
+  };
+
+  template<typename T>
+  class X: public Base {
+    using INT = C<T>;
+
+    X(INT i) : INT(i) {}
+    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: constructor does not initialize these bases: Base
+  };
+}
\ No newline at end of file

>From 76b8651c5563d4b527c362c29eb3f89876f8ee8a Mon Sep 17 00:00:00 2001
From: flovent <flbven at protonmail.com>
Date: Sat, 18 Apr 2026 19:20:10 +0800
Subject: [PATCH 2/2] Add extra line

---
 .../cppcoreguidelines/pro-type-member-init-no-crash.cpp         | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init-no-crash.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init-no-crash.cpp
index f5ba3a3bd134f..c454874503eac 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init-no-crash.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init-no-crash.cpp
@@ -31,4 +31,4 @@ namespace gh192510 {
     X(INT i) : INT(i) {}
     // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: constructor does not initialize these bases: Base
   };
-}
\ No newline at end of file
+}



More information about the cfe-commits mailing list