[clang-tools-extra] [clang-tidy] Add AllowExplicitReferencedInitialValues to `readability-enum-initial-value` (PR #189459)

Baranov Victor via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 24 09:32:55 PDT 2026


=?utf-8?q?Björn?= Svensson <bjorn.a.svensson at est.tech>,
=?utf-8?q?Björn?= Svensson <bjorn.a.svensson at est.tech>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/189459 at github.com>


================
@@ -20,15 +20,39 @@ using namespace clang::ast_matchers;
 
 namespace clang::tidy::readability {
 
-static bool isNoneEnumeratorsInitialized(const EnumDecl &Node) {
-  return llvm::all_of(Node.enumerators(), [](const EnumConstantDecl *ECD) {
-    return ECD->getInitExpr() == nullptr;
+/// Check if \p ECD is initialized by referencing another enumerator in the
+/// same enum (e.g., `last = first`).
+static bool isSelfReference(const EnumConstantDecl *ECD) {
+  const Expr *Init = ECD->getInitExpr();
+  if (!Init)
+    return false;
+  const auto *CE = dyn_cast<ConstantExpr>(Init);
+  if (!CE)
+    return false;
+  const auto *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr());
+  if (!DRE)
+    return false;
+  const auto *RefECD = dyn_cast<EnumConstantDecl>(DRE->getDecl());
----------------
vbvictor wrote:

You can use dyn_cast_if_present to combine multiple checks in one, see
https://llvm.org/docs/ProgrammersManual.html#the-isa-cast-and-dyn-cast-templates

https://github.com/llvm/llvm-project/pull/189459


More information about the cfe-commits mailing list