[clang-tools-extra] f94fbb5 - [clang-tidy] readability-enum-initial-value: fix self-reference detection (#228401)

via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 25 23:24:59 PST 2026


Author: garima-gupta511
Date: 2026-11-26T07:24:52Z
New Revision: f94fbb54378115d7936fb428641b3396612ac096

URL: https://github.com/llvm/llvm-project/commit/f94fbb54378115d7936fb428641b3396612ac096
DIFF: https://github.com/llvm/llvm-project/commit/f94fbb54378115d7936fb428641b3396612ac096.diff

LOG: [clang-tidy] readability-enum-initial-value: fix self-reference detection (#228401)

[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.

---
LLVM already supports enumerators that explicitly refer to another
enumerator through AllowReferencedInitialValues.
For example, the existing implementation supports:

  E_a,
  E_b,
  E_last = E_b,
};
The existing upstream tests also cover cases where the first enumerator
is explicitly initialized and a later enumerator refers to an earlier
one.

// OK: first-only + self-ref.
enum ERef2 {
  ERef2_a = 0,
  ERef2_b,
  ERef2_last = ERef2_b,
};
However, the following valid pattern was not handled correctly:

enum EFirstThenRefs {
  EFirstThenRefs_a = 0,
  EFirstThenRefs_b,
  EFirstThenRefs_c,
  EFirstThenRefs_first = EFirstThenRefs_a,
  EFirstThenRefs_last = EFirstThenRefs_c,
};
Here, the enum has an explicit first value, followed by implicitly
initialized enumerators, and then two enumerators that explicitly
reference earlier enumerators.
With AllowReferencedInitialValues enabled, these references should be
treated as allowed references rather than as inconsistent explicit
initializers.
This PR fixes the existing reference handling so that this pattern is
recognized correctly.
The change does not introduce a new reference/alias option; it fixes the
existing AllowReferencedInitialValues behavior for this case.

---
Note: GitHub Copilot in VS Code was used as an assistive tool during
development. I designed and implemented the changes, with some
assistance from Copilot. I reviewed and understood the changes before
submitting the PR, and the changes were also reviewed by my team.

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
    clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value-allow-refs.c
    clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value-allow-refs.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp b/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
index 620c5bece4ebad..351fcc99b41c72 100644
--- a/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
@@ -21,11 +21,14 @@ 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;
+
+  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/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..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,6 +14,7 @@ enum ERef1 {
 enum ERef2 {
   ERef2_a = 0,
   ERef2_b,
+  ERef2_paren = ((ERef2_b)),
   ERef2_last = ERef2_b,
 };
 
@@ -37,7 +38,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 f66b75fec37100..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.
@@ -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


        


More information about the cfe-commits mailing list