[clang-tools-extra] [clang-tidy] ignore uninitialized std::array in member init (PR #98134)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 5 05:01:05 PST 2026
https://github.com/dsiroky updated https://github.com/llvm/llvm-project/pull/98134
>From e70c56ecb8bd899f6ff93c55303fca1fa217a336 Mon Sep 17 00:00:00 2001
From: David Siroky <david at siroky.cz>
Date: Tue, 9 Jul 2024 10:50:00 +0200
Subject: [PATCH] [clang-tidy] ignore uninitialized std::array in member init
cppcoreguidelines-pro-type-member-init check has an option IgnoreArrays
for ignoring uninitialized C arrays. This patch adds support for C++
std::array as well.
---
.../cppcoreguidelines/ProTypeMemberInitCheck.cpp | 13 ++++++++++++-
clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++
.../cppcoreguidelines/pro-type-member-init.rst | 7 ++++---
.../pro-type-member-init.ignorearrays.cpp | 7 +++++++
4 files changed, 27 insertions(+), 4 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
index 54b8a72c865af..a8e1dfa19d657 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
@@ -431,6 +431,16 @@ static StringRef getInitializer(QualType QT, bool UseAssignment) {
}
}
+static bool isStdArray(QualType QT) {
+ const auto *RT = QT->getAs<RecordType>();
+ if (!RT)
+ return false;
+ const auto *RD = RT->getDecl();
+ if (!RD || !RD->getIdentifier())
+ return false;
+ return RD->getName() == "array" && RD->isInStdNamespace();
+}
+
static void
computeFieldsToInit(const ASTContext &Context, const RecordDecl &Record,
bool IgnoreArrays,
@@ -439,7 +449,8 @@ computeFieldsToInit(const ASTContext &Context, const RecordDecl &Record,
forEachFieldWithFilter(
Record, Record.fields(), AnyMemberHasInitPerUnion,
[&](const FieldDecl *F) {
- if (IgnoreArrays && F->getType()->isArrayType())
+ if (IgnoreArrays &&
+ (F->getType()->isArrayType() || isStdArray(F->getType())))
return;
if (F->hasInClassInitializer() && F->getParent()->isUnion()) {
AnyMemberHasInitPerUnion = true;
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 0ad69f5fdc5aa..d22032e691410 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -158,6 +158,10 @@ Changes in existing checks
the invalidating function in the warning message when a custom invalidation
function is used (via the `InvalidationFunctions` option).
+- Improved :doc:`cppcoreguidelines-pro-type-member-init
+ <clang-tidy/checks/cppcoreguidelines/pro-type-member-init>` check by treating
+ ``std::array`` the same as built-in arrays when ``IgnoreArrays`` is enabled.
+
- Improved :doc:`cppcoreguidelines-pro-type-vararg
<clang-tidy/checks/cppcoreguidelines/pro-type-vararg>` check by no longer
warning on builtins with custom type checking (e.g., type-generic builtins
diff --git a/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/pro-type-member-init.rst b/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/pro-type-member-init.rst
index b86083f82300d..9439638e699cd 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/pro-type-member-init.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/pro-type-member-init.rst
@@ -29,9 +29,10 @@ Options
.. option:: IgnoreArrays
- If set to `true`, the check will not warn about array members that are not
- zero-initialized during construction. For performance critical code, it may
- be important to not initialize fixed-size array members. Default is `false`.
+ If set to `true`, the check will not warn about array members (including
+ ``std::array``) that are not zero-initialized during construction. For
+ performance critical code, it may be important to not initialize fixed-size
+ array members. Default is `false`.
.. option:: UseAssignment
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 e4cfe679cfce9..30db741b3f176 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
@@ -27,6 +27,13 @@ void test_local_std_array() {
std::array<int, 4> a;
}
+struct HasStdArrayMember {
+ // CHECK-MESSAGES: warning: constructor does not initialize these fields: Number
+ HasStdArrayMember() {}
+ std::array<int, 4> StdArray;
+ int Number;
+};
+
struct OnlyArray {
int a[4];
};
More information about the cfe-commits
mailing list