-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[clang][diagnostics] added warning for possible enum compare typo #168445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
AaronBallman
merged 6 commits into
llvm:main
from
GrumpyPigSkin:clang-diagnostics-warn-on-enum-compare-typo
Nov 20, 2025
+165
−0
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
92c22e6
[clang][diagnostics] added warning for possible enum compare typo
GrumpyPigSkin 2db6fa4
[clang][diagnostics] Improved warning message for enum-compare-typo
GrumpyPigSkin 7fb63d3
[clang][diagnostics] Moved check for enum-compare-typo and improved d…
GrumpyPigSkin c026f8b
[clang][diagnostics] Updated test code for enum-compare-typo
GrumpyPigSkin 6803a95
[clang][diagnostics] Code formatting
GrumpyPigSkin bda83e3
[clang][diagnostics] Implemented code review comments and added relea…
GrumpyPigSkin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // RUN: %clang_cc1 -fsyntax-only -Wenum-compare-typo -verify %s | ||
| // RUN: %clang_cc1 -fsyntax-only -Wenum-compare-typo -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s | ||
|
|
||
| enum E { | ||
| kOptionGood1 = 1ull << 0, | ||
| kOptionGood2 = 1ull >> 0, | ||
| // expected-warning@+3 {{comparison operator '<' in enumerator constant is likely a typo for the shift operator '<<'}} | ||
| // expected-note@+2 {{use '<<' to perform a bitwise shift}} | ||
| // CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:22-[[@LINE+1]]:23}:"<<" | ||
| kOptionBad1 = 1ull < 1, | ||
| // expected-warning@+3 {{comparison operator '>' in enumerator constant is likely a typo for the shift operator '>>'}} | ||
| // expected-note@+2 {{use '>>' to perform a bitwise shift}} | ||
| // CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:19-[[@LINE+1]]:20}:">>" | ||
| kOptionBad2 = 1 > 3, | ||
| // expected-warning@+3 {{comparison operator '>' in enumerator constant is likely a typo for the shift operator '>>'}} | ||
| // expected-note@+2 {{use '>>' to perform a bitwise shift}} | ||
| // CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:20-[[@LINE+1]]:21}:">>" | ||
| kOptionBad3 = (1 > 2) | ||
| }; | ||
|
|
||
| // Ensure the warning does not fire on valid code | ||
GrumpyPigSkin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| enum F { | ||
| kSomeValue = 10, | ||
| kComparison = kSomeValue > 5, // No warning | ||
| kMaxEnum = 255, | ||
| kIsValid = kMaxEnum > 0, // No warning | ||
| kSum = 10 + 20, // No warning | ||
| kShift = 2 << 5 // No warning | ||
| }; | ||
|
|
||
| // Ensure the diagnostic group works | ||
|
|
||
| #pragma clang diagnostic push | ||
| #pragma clang diagnostic ignored "-Wenum-compare-typo" | ||
| enum G { | ||
| kIgnored = 1 < 10 // No warning | ||
| }; | ||
| #pragma clang diagnostic pop | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My inclination is for "may" rather than "likely", there also needs to be a way to suppress this warning locally - having to put pragmas around blocks is error prone, and using a global
-Wno-flag loses the warning for all code.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say "is potentially a typo for a shift operator '%1'"
As for the suppression, I think an explicit cast to bool would be a reasonable marker of "I meant to do that".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have gone with "is potentially a typo for a shift operator '%1'".
I used any explicit cast to suppress the warning, is that fine?