Skip to content

Commit 991b6a7

Browse files
committed
[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
1 parent f13b7d0 commit 991b6a7

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,18 @@ void EnumInitialValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
141141
}
142142

143143
void EnumInitialValueCheck::registerMatchers(MatchFinder *Finder) {
144-
Finder->addMatcher(
145-
enumDecl(unless(isMacro()), unless(hasConsistentInitialValues()))
146-
.bind("inconsistent"),
147-
this);
144+
Finder->addMatcher(enumDecl(isDefinition(), unless(isMacro()),
145+
unless(hasConsistentInitialValues()))
146+
.bind("inconsistent"),
147+
this);
148148
if (!AllowExplicitZeroFirstInitialValue)
149149
Finder->addMatcher(
150-
enumDecl(hasZeroInitialValueForFirstEnumerator()).bind("zero_first"),
150+
enumDecl(isDefinition(), hasZeroInitialValueForFirstEnumerator())
151+
.bind("zero_first"),
151152
this);
152153
if (!AllowExplicitSequentialInitialValues)
153-
Finder->addMatcher(enumDecl(unless(isMacro()), hasSequentialInitialValues())
154+
Finder->addMatcher(enumDecl(isDefinition(), unless(isMacro()),
155+
hasSequentialInitialValues())
154156
.bind("sequential"),
155157
this);
156158
}

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ Changes in existing checks
132132
<clang-tidy/checks/modernize/use-std-print>` check to support replacing
133133
member function calls too.
134134

135+
- Improved :doc:`readability-enum-initial-value
136+
<clang-tidy/checks/readability-enum-initial-value>` check to only issue
137+
diagnostics for the definition of an ``enum``.
138+
135139
- Improved :doc:`performance-avoid-endl
136140
<clang-tidy/checks/performance/avoid-endl>` check to use ``std::endl`` as
137141
placeholder when lexer cannot get source text.

clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,17 @@ enum EnumSequentialInitialValue {
7878
EnumSequentialInitialValue_2 = 4,
7979
// CHECK-FIXES-ENABLE: EnumSequentialInitialValue_2 ,
8080
};
81+
82+
// gh107590
83+
enum WithFwdDecl : int;
84+
85+
enum WithFwdDecl : int {
86+
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: inital values in enum 'WithFwdDecl' are not consistent
87+
// CHECK-MESSAGES-ENABLE: :[[@LINE-2]]:1: warning: inital values in enum 'WithFwdDecl' are not consistent
88+
E0,
89+
// CHECK-FIXES: E0 = 0,
90+
E1 = 1,
91+
E2,
92+
// CHECK-FIXES: E2 = 2,
93+
};
94+

0 commit comments

Comments
 (0)