https://github.com/5chmidti created 
https://github.com/llvm/llvm-project/pull/107652

With the `isDefinition` matcher, the analysis and diagnostics will be
constrained to definitions only. Previously forward declarations were
diagnosed as well.

Fixes #107590


>From e4c4a9954c59ec19ecb0c368e22e62404682370a Mon Sep 17 00:00:00 2001
From: Julian Schmidt <git.julian.schm...@gmail.com>
Date: Sat, 7 Sep 2024 00:37:57 +0200
Subject: [PATCH] [clang-tidy] only diagnose definitions in
 readability-enum-initial-value

With the `isDefinition` matcher, the analysis and diagnostics will be
constrained to definitions only. Previously forward declarations were
diagnosed as well.

Fixes #107590
---
 .../readability/EnumInitialValueCheck.cpp          | 14 ++++++++------
 clang-tools-extra/docs/ReleaseNotes.rst            |  4 ++++
 .../checkers/readability/enum-initial-value.c      | 14 ++++++++++++++
 3 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
index 8f2841c32259a2..60b129196ba955 100644
--- a/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
@@ -141,16 +141,18 @@ void 
EnumInitialValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
 }
 
 void EnumInitialValueCheck::registerMatchers(MatchFinder *Finder) {
-  Finder->addMatcher(
-      enumDecl(unless(isMacro()), unless(hasConsistentInitialValues()))
-          .bind("inconsistent"),
-      this);
+  Finder->addMatcher(enumDecl(isDefinition(), unless(isMacro()),
+                              unless(hasConsistentInitialValues()))
+                         .bind("inconsistent"),
+                     this);
   if (!AllowExplicitZeroFirstInitialValue)
     Finder->addMatcher(
-        enumDecl(hasZeroInitialValueForFirstEnumerator()).bind("zero_first"),
+        enumDecl(isDefinition(), hasZeroInitialValueForFirstEnumerator())
+            .bind("zero_first"),
         this);
   if (!AllowExplicitSequentialInitialValues)
-    Finder->addMatcher(enumDecl(unless(isMacro()), 
hasSequentialInitialValues())
+    Finder->addMatcher(enumDecl(isDefinition(), unless(isMacro()),
+                                hasSequentialInitialValues())
                            .bind("sequential"),
                        this);
 }
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 8d028f8863cb7a..469dd4e54b1181 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -120,6 +120,10 @@ Changes in existing checks
   <clang-tidy/checks/modernize/use-std-print>` check to support replacing
   member function calls too.
 
+- Improved :doc:`readability-enum-initial-value
+  <clang-tidy/checks/readability-enum-initial-value>` check to only issue
+  diagnostics for the definition of an ``enum``.
+
 - Improved :doc:`readablility-implicit-bool-conversion
   <clang-tidy/checks/readability/implicit-bool-conversion>` check
   by adding the option `UseUpperCaseLiteralSuffix` to select the
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value.c 
b/clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value.c
index c66288cbe3e957..36727d00c10a48 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value.c
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value.c
@@ -78,3 +78,17 @@ enum EnumSequentialInitialValue {
   EnumSequentialInitialValue_2 = 4,
   // CHECK-FIXES-ENABLE: EnumSequentialInitialValue_2 ,
 };
+
+// gh107590
+enum WithFwdDecl : int;
+
+enum WithFwdDecl : int {
+  // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: inital values in enum 
'WithFwdDecl' are not consistent
+  // CHECK-MESSAGES-ENABLE: :[[@LINE-2]]:1: warning: inital values in enum 
'WithFwdDecl' are not consistent
+  E0,
+  // CHECK-FIXES: E0 = 0,
+  E1 = 1,
+  E2,
+  // CHECK-FIXES: E2 = 2,
+};
+

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to