[clang-tools-extra] [clang-tidy] only diagnose definitions in readability-enum-initial-value (PR #107652)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 6 15:40:56 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tidy
@llvm/pr-subscribers-clang-tools-extra
Author: Julian Schmidt (5chmidti)
<details>
<summary>Changes</summary>
With the `isDefinition` matcher, the analysis and diagnostics will be
constrained to definitions only. Previously forward declarations were
diagnosed as well.
Fixes #<!-- -->107590
---
Full diff: https://github.com/llvm/llvm-project/pull/107652.diff
3 Files Affected:
- (modified) clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp (+8-6)
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4)
- (modified) clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value.c (+14)
``````````diff
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,
+};
+
``````````
</details>
https://github.com/llvm/llvm-project/pull/107652
More information about the cfe-commits
mailing list