[clang-tools-extra] [clang-tidy] Fix false positive in modernize-use-equals-default (PR #232363)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 16 05:57:47 PDT 2026
https://github.com/serge-sans-paille updated https://github.com/llvm/llvm-project/pull/232363
>From cfc8dd940b657565a3836d7cde772695e4007af5 Mon Sep 17 00:00:00 2001
From: serge-sans-paille <sguelton at mozilla.com>
Date: Thu, 15 Oct 2026 17:03:05 +0200
Subject: [PATCH 1/3] [clang-tidy] Fix false positive in
modernize-use-equals-default
A field initialized with the default constructor in the initialization
is value initialized, which is not the case if it is defaulted, so
filter tout this case.
Fix #231561
---
.../modernize/UseEqualsDefaultCheck.cpp | 8 +++--
.../checkers/modernize/use-equals-default.cpp | 34 +++++++++++++++----
2 files changed, 33 insertions(+), 9 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp
index af8774dc09f359..3b3e57078fb4c5 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp
@@ -247,9 +247,11 @@ void UseEqualsDefaultCheck::registerMatchers(MatchFinder *Finder) {
anyOf(
// Default constructor.
allOf(parameterCountIs(0),
- unless(hasAnyConstructorInitializer(allOf(
- isWritten(), unless(withInitializer(cxxConstructExpr(
- argumentCountIs(0))))))),
+ unless(hasAnyConstructorInitializer(
+ allOf(isWritten(),
+ unless(withInitializer(cxxConstructExpr(
+ argumentCountIs(0),
+ unless(requiresZeroInitialization()))))))),
unless(isVariadic()), IsPublicOrOutOfLineUntilCPP20),
// Copy constructor.
allOf(isCopyConstructor(),
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default.cpp
index 00264c7d51f5c8..d589a2dd49dfb1 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default.cpp
@@ -243,15 +243,17 @@ struct DC : KW {
// CHECK-FIXES: ~DC() override = default;
};
-// Delegating constructor with multiple initializers.
-struct DCM : KW, N {
- DCM() : KW(), N() {}
+// Delegating constructor with multiple bases.
+struct Base {
+ Base();
+};
+
+struct DCM : KW, Base {
+ DCM() : KW(), Base() {}
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
// CHECK-FIXES: DCM() = default;
};
-struct dummy {};
-
// Delegating constructor with field initializer.
struct DCF : KW {
@@ -259,7 +261,27 @@ struct DCF : KW {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
// CHECK-FIXES: DCF() = default;
- dummy d;
+ Base d{};
+};
+
+struct non_empty {
+ int a,b;
+};
+
+// Delegating constructor with zero-init
+struct DCN : non_empty {
+
+ DCN() : non_empty() {}
+ // defaulting would remove the zero-init of non_empty.
+};
+
+// Delegating constructor with zero-init
+struct DCNF {
+
+ DCNF() : n() {}
+ // defaulting would remove the zero-init of n
+
+ non_empty n;
};
// Delegating constructor with noexcept.
>From cfa54b89f2cfd05f3e6879632e87acfaa0717431 Mon Sep 17 00:00:00 2001
From: serge-sans-paille <sguelton at mozilla.com>
Date: Fri, 16 Oct 2026 11:43:53 +0200
Subject: [PATCH 2/3] fixup! [clang-tidy] Fix false positive in
modernize-use-equals-default
---
.../checkers/modernize/use-equals-default.cpp | 31 +++++++++++++++++++
1 file changed, 31 insertions(+)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default.cpp
index d589a2dd49dfb1..6ab9b33de51a4a 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default.cpp
@@ -244,6 +244,7 @@ struct DC : KW {
};
// Delegating constructor with multiple bases.
+// user-provided default ctor -> no zero-init required
struct Base {
Base();
};
@@ -264,6 +265,7 @@ struct DCF : KW {
Base d{};
};
+// trivial, non-empty -> value-init zeroes it
struct non_empty {
int a,b;
};
@@ -291,6 +293,35 @@ struct DCNE : KW {
// CHECK-FIXES: DCNE() noexcept = default;
};
+// Delegating constructor with zero-init of an array of class type.
+struct DCNA {
+ DCNA() : entries() {}
+ // defaulting would remove the zero-init of entries.
+ non_empty entries[32];
+};
+
+// Zero-init of an array of scalars.
+struct DCNS {
+ DCNS() : buf() {}
+ // defaulting would remove the zero-init of buf.
+ unsigned buf[8];
+};
+
+// Same for a plain scalar member.
+struct DCNSM {
+ DCNSM() : x() {}
+ // defaulting would remove the zero-init of x.
+ int x;
+};
+
+// user-provided default ctor -> no zero-init required
+struct DCAU {
+ Base entries[4];
+ DCAU() : entries() {}
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
+ // CHECK-FIXES: DCAU() = default;
+};
+
struct OverrideWithSemiColon : KW {
~OverrideWithSemiColon() override {};
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
>From 2d361c3564eb7c269e6d227b9ebf3b5c6761b562 Mon Sep 17 00:00:00 2001
From: serge-sans-paille <sguelton at mozilla.com>
Date: Fri, 16 Oct 2026 14:57:22 +0200
Subject: [PATCH 3/3] fixup! fixup! [clang-tidy] Fix false positive in
modernize-use-equals-default
---
.../clang-tidy/checkers/modernize/use-equals-default.cpp | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default.cpp
index 6ab9b33de51a4a..4131279c1a858c 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default.cpp
@@ -314,6 +314,14 @@ struct DCNSM {
int x;
};
+// Same for a plain scalar member with default init.
+struct DCNSMI {
+ DCNSMI() : x() {}
+ // Defaulting would remove the default-init of x.
+ // Note that assigning 0 would not change the linter behavior.
+ int x = 1;
+};
+
// user-provided default ctor -> no zero-init required
struct DCAU {
Base entries[4];
More information about the cfe-commits
mailing list