[clang-tools-extra] [clang-tidy] readability-enum-initial-value: fix self-reference detection (PR #228401)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 25 09:01:24 PST 2026
https://github.com/garima-gupta511 updated https://github.com/llvm/llvm-project/pull/228401
>From 8b1098ad0ea6e42f13aa2d0d42241581773e1e68 Mon Sep 17 00:00:00 2001
From: Garima Gupta <garima.gupta at siemens.com>
Date: Fri, 18 Sep 2026 17:49:03 +0100
Subject: [PATCH 1/4] [clang-tidy] readability-enum-initial-value: fix
self-reference detection
Fix `AllowReferencedInitialValues` failing to recognize references to another
enumerator in unscoped enums when the reference is implicitly converted to the
enum's underlying type.
Add a regression test for this case.
---
.../readability/EnumInitialValueCheck.cpp | 16 ++++++++++++----
.../checks/readability/enum-initial-value.md | 5 +++--
.../enum-initial-value-allow-refs.cpp | 19 +++++++++++++++++++
3 files changed, 34 insertions(+), 6 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp b/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
index 620c5bece4ebad..b95c7daa933a8b 100644
--- a/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
@@ -21,11 +21,19 @@ using namespace clang::ast_matchers;
namespace clang::tidy::readability {
/// Check if \p ECD is initialized by referencing another enumerator in the
-/// same enum (e.g., `last = first`).
+/// same enum (e.g., `last = first`). For unscoped enums, the reference may be
+/// wrapped in an implicit conversion to the enum's underlying type.
static bool isSelfReference(const EnumConstantDecl *ECD) {
- const auto *CE = dyn_cast_if_present<ConstantExpr>(ECD->getInitExpr());
- const auto *DRE =
- dyn_cast_if_present<DeclRefExpr>(CE ? CE->getSubExpr() : nullptr);
+ const Expr *Init = ECD->getInitExpr();
+ if (!Init)
+ return false;
+
+ if (const auto *CE = dyn_cast<ConstantExpr>(Init))
+ Init = CE->getSubExpr();
+
+ Init = Init->IgnoreImpCasts();
+
+ const auto *DRE = dyn_cast<DeclRefExpr>(Init);
const auto *RefECD =
dyn_cast_if_present<EnumConstantDecl>(DRE ? DRE->getDecl() : nullptr);
return RefECD && RefECD->getDeclContext() == ECD->getDeclContext();
diff --git a/clang-tools-extra/docs/clang-tidy/checks/readability/enum-initial-value.md b/clang-tools-extra/docs/clang-tidy/checks/readability/enum-initial-value.md
index aec3af8c11eec2..d504e4bb86b4d0 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/readability/enum-initial-value.md
+++ b/clang-tools-extra/docs/clang-tidy/checks/readability/enum-initial-value.md
@@ -94,8 +94,9 @@ enum G {
````{option} AllowReferencedInitialValues
When `true`, enumerators initialized by referencing another enumerator
in the same enum are allowed, and the remaining enumerators are checked for
-consistency. This implements the `INT09-C-EX1` exception from the CERT C
-Coding Standard.
+consistency. References to earlier enumerators are also recognized when they
+occur after implicitly initialized enumerators. This implements the
+`INT09-C-EX1` exception from the CERT C Coding Standard.
Default is `false`.
```c++
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value-allow-refs.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value-allow-refs.cpp
index f66b75fec37100..5f1b0e282816e0 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value-allow-refs.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value-allow-refs.cpp
@@ -24,6 +24,25 @@ enum class ERefAll {
ERefAll_last = ERefAll_b,
};
+// OK: first-only + self-refs, where later self-references do not need to
+// immediately follow the enumerator they reference and do not require every
+// preceding enumerator to be explicitly initialized.
+enum EFirstThenRefs {
+ EFirstThenRefs_a = 0,
+ EFirstThenRefs_b,
+ EFirstThenRefs_c,
+ EFirstThenRefs_first = EFirstThenRefs_a,
+ EFirstThenRefs_last = EFirstThenRefs_c,
+};
+
+// OK: a self-reference may appear before later implicit enumerators.
+enum ERefThenImplicit {
+ ERefThenImplicit_a = 0,
+ ERefThenImplicit_alias = ERefThenImplicit_a,
+ ERefThenImplicit_b,
+ ERefThenImplicit_c,
+};
+
// Error: literal duplicate (not a reference).
enum class ERefErr {
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: initial values in enum 'ERefErr' are not consistent
>From 32cf365fdb236001250ff60dad312d7b0bafa499 Mon Sep 17 00:00:00 2001
From: Garima Gupta <garima.gupta at siemens.com>
Date: Fri, 2 Oct 2026 21:45:19 +0100
Subject: [PATCH 2/4] Address review feedback points
---
clang-tools-extra/docs/ReleaseNotes.md | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md
index c2ee1b06c43e06..6f6069691a7fa0 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -270,7 +270,9 @@ infrastructure are described first, followed by tool-specific sections.
<clang-tidy/checks/readability/enum-initial-value>` check by adding
the {option}`AllowReferencedInitialValues` to support the
`INT09-C-EX1` exception, allowing enumerators initialized by referencing
- another enumerator in the same enum (e.g., `last = first`).
+ another enumerator in the same enum (e.g., `last = first`), including
+ references to earlier enumerators that occur after implicitly initialized
+ enumerators.
- Improved {doc}`readability-function-cognitive-complexity
<clang-tidy/checks/readability/function-cognitive-complexity>` check by fixing
>From cb24bf7be721911093fd566dcedb137e7cfc7f67 Mon Sep 17 00:00:00 2001
From: Garima Gupta <garima.gupta at siemens.com>
Date: Mon, 23 Nov 2026 17:33:01 +0000
Subject: [PATCH 3/4] [clang-tidy] Handle parenthesized enum references
Use IgnoreParenImpCasts() to handle parenthesized enumerator references. Add test coverage.
---
.../clang-tidy/readability/EnumInitialValueCheck.cpp | 7 +------
clang-tools-extra/docs/ReleaseNotes.md | 4 +---
.../checkers/readability/enum-initial-value-allow-refs.c | 4 ++--
.../checkers/readability/enum-initial-value-allow-refs.cpp | 2 +-
4 files changed, 5 insertions(+), 12 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp b/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
index b95c7daa933a8b..351fcc99b41c72 100644
--- a/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
@@ -28,12 +28,7 @@ static bool isSelfReference(const EnumConstantDecl *ECD) {
if (!Init)
return false;
- if (const auto *CE = dyn_cast<ConstantExpr>(Init))
- Init = CE->getSubExpr();
-
- Init = Init->IgnoreImpCasts();
-
- const auto *DRE = dyn_cast<DeclRefExpr>(Init);
+ const auto *DRE = dyn_cast<DeclRefExpr>(Init->IgnoreParenImpCasts());
const auto *RefECD =
dyn_cast_if_present<EnumConstantDecl>(DRE ? DRE->getDecl() : nullptr);
return RefECD && RefECD->getDeclContext() == ECD->getDeclContext();
diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md
index 6f6069691a7fa0..c2ee1b06c43e06 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -270,9 +270,7 @@ infrastructure are described first, followed by tool-specific sections.
<clang-tidy/checks/readability/enum-initial-value>` check by adding
the {option}`AllowReferencedInitialValues` to support the
`INT09-C-EX1` exception, allowing enumerators initialized by referencing
- another enumerator in the same enum (e.g., `last = first`), including
- references to earlier enumerators that occur after implicitly initialized
- enumerators.
+ another enumerator in the same enum (e.g., `last = first`).
- Improved {doc}`readability-function-cognitive-complexity
<clang-tidy/checks/readability/function-cognitive-complexity>` check by fixing
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value-allow-refs.c b/clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value-allow-refs.c
index 092626be1c6655..b4884d0bd3cfcf 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value-allow-refs.c
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value-allow-refs.c
@@ -14,7 +14,7 @@ enum ERef1 {
enum ERef2 {
ERef2_a = 0,
ERef2_b,
- ERef2_last = ERef2_b,
+ ERef2_last = ((ERef2_b)),
};
// OK: all + self-ref.
@@ -37,7 +37,7 @@ enum ERefCross {
ERefCross_b,
// CHECK-MESSAGES: :[[@LINE-1]]:3: note: uninitialized enumerator 'ERefCross_b' defined here
// CHECK-FIXES: ERefCross_b = 1,
- ERefCross_last = ERefOther_b,
+ ERefCross_last = (ERefOther_b),
};
// Error: inconsistent even after ignoring self-refs.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value-allow-refs.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value-allow-refs.cpp
index 5f1b0e282816e0..582e15f7834fd7 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value-allow-refs.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value-allow-refs.cpp
@@ -14,7 +14,7 @@ enum class ERef {
enum class ERefFirst {
ERefFirst_a = 1,
ERefFirst_b,
- ERefFirst_alias = ERefFirst_a,
+ ERefFirst_alias = ((ERefFirst_a)),
};
// OK: all + self-ref.
>From 029a23e5e502d68a3451ccb643a12af28e0e604e Mon Sep 17 00:00:00 2001
From: Garima Gupta <garima.gupta at siemens.com>
Date: Wed, 25 Nov 2026 17:01:04 +0000
Subject: [PATCH 4/4] Review points added
---
.../docs/clang-tidy/checks/readability/enum-initial-value.md | 5 ++---
.../checkers/readability/enum-initial-value-allow-refs.c | 3 ++-
2 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/clang-tools-extra/docs/clang-tidy/checks/readability/enum-initial-value.md b/clang-tools-extra/docs/clang-tidy/checks/readability/enum-initial-value.md
index d504e4bb86b4d0..aec3af8c11eec2 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/readability/enum-initial-value.md
+++ b/clang-tools-extra/docs/clang-tidy/checks/readability/enum-initial-value.md
@@ -94,9 +94,8 @@ enum G {
````{option} AllowReferencedInitialValues
When `true`, enumerators initialized by referencing another enumerator
in the same enum are allowed, and the remaining enumerators are checked for
-consistency. References to earlier enumerators are also recognized when they
-occur after implicitly initialized enumerators. This implements the
-`INT09-C-EX1` exception from the CERT C Coding Standard.
+consistency. This implements the `INT09-C-EX1` exception from the CERT C
+Coding Standard.
Default is `false`.
```c++
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value-allow-refs.c b/clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value-allow-refs.c
index b4884d0bd3cfcf..d092ad653bd37c 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value-allow-refs.c
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value-allow-refs.c
@@ -14,7 +14,8 @@ enum ERef1 {
enum ERef2 {
ERef2_a = 0,
ERef2_b,
- ERef2_last = ((ERef2_b)),
+ ERef2_paren = ((ERef2_b)),
+ ERef2_last = ERef2_b,
};
// OK: all + self-ref.
More information about the cfe-commits
mailing list