[clang-tools-extra] [clang-tidy] Fix `cppcoreguidelines-pro-type-member-init` check (PR #169832)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 2 23:51:48 PST 2025
https://github.com/zeyi2 updated https://github.com/llvm/llvm-project/pull/169832
>From f41310dcb601075d980c4d7163d8db1d5dee7913 Mon Sep 17 00:00:00 2001
From: mtx <mitchell.xu2 at gmail.com>
Date: Thu, 27 Nov 2025 23:50:47 +0800
Subject: [PATCH 1/3] [clang-tidy] Fix `cppcoreguidelines-pro-type-member-init`
check
---
.../ProTypeMemberInitCheck.cpp | 32 +++++++++++++++--
clang-tools-extra/docs/ReleaseNotes.rst | 5 +++
.../pro-type-member-init.ignorearrays.cpp | 36 +++++++++++++++++++
3 files changed, 71 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
index 66508da89f0dd..f8c096b4b4c66 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
@@ -361,7 +361,8 @@ void ProTypeMemberInitCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
}
// FIXME: Copied from clang/lib/Sema/SemaDeclCXX.cpp.
-static bool isIncompleteOrZeroLengthArrayType(ASTContext &Context, QualType T) {
+static bool isIncompleteOrZeroLengthArrayType(const ASTContext &Context,
+ QualType T) {
if (T->isIncompleteArrayType())
return true;
@@ -375,7 +376,7 @@ static bool isIncompleteOrZeroLengthArrayType(ASTContext &Context, QualType T) {
return false;
}
-static bool isEmpty(ASTContext &Context, const QualType &Type) {
+static bool isEmpty(const ASTContext &Context, const QualType &Type) {
if (const CXXRecordDecl *ClassDecl = Type->getAsCXXRecordDecl()) {
return ClassDecl->isEmpty();
}
@@ -582,6 +583,33 @@ void ProTypeMemberInitCheck::checkMissingBaseClassInitializer(
void ProTypeMemberInitCheck::checkUninitializedTrivialType(
const ASTContext &Context, const VarDecl *Var) {
+ // Verify that the record actually needs initialization
+ const CXXRecordDecl *Record = Var->getType()->getAsCXXRecordDecl();
+ if (!Record)
+ return;
+
+ SmallPtrSet<const FieldDecl *, 16> FieldsToInit;
+ bool AnyMemberHasInitPerUnion = false;
+ forEachFieldWithFilter(
+ *Record, Record->fields(), AnyMemberHasInitPerUnion,
+ [&](const FieldDecl *F) {
+ if (IgnoreArrays && F->getType()->isArrayType())
+ return;
+ if (F->hasInClassInitializer() && F->getParent()->isUnion()) {
+ AnyMemberHasInitPerUnion = true;
+ removeFieldInitialized(F, FieldsToInit);
+ }
+ if (!F->hasInClassInitializer() &&
+ utils::type_traits::isTriviallyDefaultConstructible(F->getType(),
+ Context) &&
+ !isEmpty(Context, F->getType()) && !F->isUnnamedBitField() &&
+ !AnyMemberHasInitPerUnion)
+ FieldsToInit.insert(F);
+ });
+
+ if (FieldsToInit.empty())
+ return;
+
const DiagnosticBuilder Diag =
diag(Var->getBeginLoc(), "uninitialized record type: %0") << Var;
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index a6f80e3721db1..3ad2ffc07da5c 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -424,6 +424,11 @@ Changes in existing checks
adding an option to allow pointer arithmetic via prefix/postfix increment or
decrement operators.
+- Improved :doc:`cppcoreguidelines-pro-type-member-init
+ <clang-tidy/checks/cppcoreguidelines/pro-type-member-init>` check to
+ correctly ignore ``std::array`` and other containers when ``IgnoreArrays``
+ option is set to ``true``.
+
- Improved :doc:`google-readability-casting
<clang-tidy/checks/google/readability-casting>` check by adding fix-it
notes for downcasts and casts to void pointer.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init.ignorearrays.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init.ignorearrays.cpp
index 01859b3ad98f4..e4cfe679cfce9 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init.ignorearrays.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init.ignorearrays.cpp
@@ -14,3 +14,39 @@ struct HasArrayMember {
int RawArray[4];
int Number;
};
+
+namespace std {
+template <typename T, int N>
+struct array {
+ T _Elems[N];
+ void fill(const T &);
+};
+}
+
+void test_local_std_array() {
+ std::array<int, 4> a;
+}
+
+struct OnlyArray {
+ int a[4];
+};
+
+void test_local_only_array() {
+ OnlyArray a;
+}
+
+struct Mixed {
+ int a[4];
+ int b;
+};
+
+void test_local_mixed() {
+ Mixed m;
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: uninitialized record type: 'm'
+}
+
+void test_std_array_fill() {
+ std::array<char, 10> someArray;
+ // CHECK-MESSAGES-NOT: warning: uninitialized record type: 'someArray'
+ someArray.fill('n');
+}
>From 29f44eb8f8ff90eac085670c0ee9b4c07a589717 Mon Sep 17 00:00:00 2001
From: mitchell <mitchell.xu2 at gmail.com>
Date: Fri, 28 Nov 2025 13:33:37 +0800
Subject: [PATCH 2/3] Update clang-tools-extra/docs/ReleaseNotes.rst
Co-authored-by: EugeneZelenko <eugene.zelenko at gmail.com>
---
clang-tools-extra/docs/ReleaseNotes.rst | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 3ad2ffc07da5c..edb2a98446eba 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -426,8 +426,8 @@ Changes in existing checks
- Improved :doc:`cppcoreguidelines-pro-type-member-init
<clang-tidy/checks/cppcoreguidelines/pro-type-member-init>` check to
- correctly ignore ``std::array`` and other containers when ``IgnoreArrays``
- option is set to ``true``.
+ correctly ignore ``std::array`` and other containers when `IgnoreArrays`
+ option is set to `true`.
- Improved :doc:`google-readability-casting
<clang-tidy/checks/google/readability-casting>` check by adding fix-it
>From 118c166c9913d3dbea19e26748058d05639b94f6 Mon Sep 17 00:00:00 2001
From: mtx <mitchell.xu2 at gmail.com>
Date: Wed, 3 Dec 2025 15:49:08 +0800
Subject: [PATCH 3/3] Fix
---
.../ProTypeMemberInitCheck.cpp | 49 ++++++++-----------
clang-tools-extra/docs/ReleaseNotes.rst | 4 +-
2 files changed, 22 insertions(+), 31 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
index f8c096b4b4c66..f2676468b6871 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
@@ -432,19 +432,13 @@ static llvm::StringLiteral getInitializer(QualType QT, bool UseAssignment) {
}
}
-void ProTypeMemberInitCheck::checkMissingMemberInitializer(
- ASTContext &Context, const CXXRecordDecl &ClassDecl,
- const CXXConstructorDecl *Ctor) {
- const bool IsUnion = ClassDecl.isUnion();
-
- if (IsUnion && ClassDecl.hasInClassInitializer())
- return;
-
- // Gather all fields (direct and indirect) that need to be initialized.
- SmallPtrSet<const FieldDecl *, 16> FieldsToInit;
+static void
+computeFieldsToInit(const ASTContext &Context, const RecordDecl &Record,
+ bool IgnoreArrays,
+ SmallPtrSetImpl<const FieldDecl *> &FieldsToInit) {
bool AnyMemberHasInitPerUnion = false;
forEachFieldWithFilter(
- ClassDecl, ClassDecl.fields(), AnyMemberHasInitPerUnion,
+ Record, Record.fields(), AnyMemberHasInitPerUnion,
[&](const FieldDecl *F) {
if (IgnoreArrays && F->getType()->isArrayType())
return;
@@ -459,6 +453,19 @@ void ProTypeMemberInitCheck::checkMissingMemberInitializer(
!AnyMemberHasInitPerUnion)
FieldsToInit.insert(F);
});
+}
+
+void ProTypeMemberInitCheck::checkMissingMemberInitializer(
+ ASTContext &Context, const CXXRecordDecl &ClassDecl,
+ const CXXConstructorDecl *Ctor) {
+ const bool IsUnion = ClassDecl.isUnion();
+
+ if (IsUnion && ClassDecl.hasInClassInitializer())
+ return;
+
+ // Gather all fields (direct and indirect) that need to be initialized.
+ SmallPtrSet<const FieldDecl *, 16> FieldsToInit;
+ computeFieldsToInit(Context, ClassDecl, IgnoreArrays, FieldsToInit);
if (FieldsToInit.empty())
return;
@@ -508,7 +515,7 @@ void ProTypeMemberInitCheck::checkMissingMemberInitializer(
// Collect all fields but only suggest a fix for the first member of unions,
// as initializing more than one union member is an error.
SmallPtrSet<const FieldDecl *, 16> FieldsToFix;
- AnyMemberHasInitPerUnion = false;
+ bool AnyMemberHasInitPerUnion = false;
forEachFieldWithFilter(ClassDecl, ClassDecl.fields(),
AnyMemberHasInitPerUnion, [&](const FieldDecl *F) {
if (!FieldsToInit.contains(F))
@@ -589,23 +596,7 @@ void ProTypeMemberInitCheck::checkUninitializedTrivialType(
return;
SmallPtrSet<const FieldDecl *, 16> FieldsToInit;
- bool AnyMemberHasInitPerUnion = false;
- forEachFieldWithFilter(
- *Record, Record->fields(), AnyMemberHasInitPerUnion,
- [&](const FieldDecl *F) {
- if (IgnoreArrays && F->getType()->isArrayType())
- return;
- if (F->hasInClassInitializer() && F->getParent()->isUnion()) {
- AnyMemberHasInitPerUnion = true;
- removeFieldInitialized(F, FieldsToInit);
- }
- if (!F->hasInClassInitializer() &&
- utils::type_traits::isTriviallyDefaultConstructible(F->getType(),
- Context) &&
- !isEmpty(Context, F->getType()) && !F->isUnnamedBitField() &&
- !AnyMemberHasInitPerUnion)
- FieldsToInit.insert(F);
- });
+ computeFieldsToInit(Context, *Record, IgnoreArrays, FieldsToInit);
if (FieldsToInit.empty())
return;
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index edb2a98446eba..27c47f3790ab4 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -426,8 +426,8 @@ Changes in existing checks
- Improved :doc:`cppcoreguidelines-pro-type-member-init
<clang-tidy/checks/cppcoreguidelines/pro-type-member-init>` check to
- correctly ignore ``std::array`` and other containers when `IgnoreArrays`
- option is set to `true`.
+ correctly ignore ``std::array`` and other array-like containers when
+ `IgnoreArrays` option is set to `true`.
- Improved :doc:`google-readability-casting
<clang-tidy/checks/google/readability-casting>` check by adding fix-it
More information about the cfe-commits
mailing list