diff options
| author | GrumpyPigSkin <130710602+GrumpyPigSkin@users.noreply.github.com> | 2025-11-20 14:05:03 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-20 09:05:03 -0500 |
| commit | aeba7a8adef9a8ea624aa5be3b95a8a9fb5a17dd (patch) | |
| tree | 234bcaf4dfb986e2bff373a40e5731c1e8f77927 /clang/test | |
| parent | 150d9b7a779f405021b5a08718394772360a9ef8 (diff) | |
[clang][diagnostics] added warning for possible enum compare typo (#168445)
Added diagnosis and fixit comment for possible accidental comparison
operator in an enum.
Closes: #168146
Diffstat (limited to 'clang/test')
| -rw-r--r-- | clang/test/Sema/warn-enum-compare-typo.c | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/clang/test/Sema/warn-enum-compare-typo.c b/clang/test/Sema/warn-enum-compare-typo.c new file mode 100644 index 000000000000..f937df1e6cd8 --- /dev/null +++ b/clang/test/Sema/warn-enum-compare-typo.c @@ -0,0 +1,98 @@ +// 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 PossibleTypoLeft { + Val1 = 1 << 0, + // expected-warning@+3 {{comparison operator '<' is potentially a typo for a shift operator '<<'}} + // expected-note@+2 {{use '<<' to perform a bitwise shift}} + // CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:12-[[@LINE+1]]:13}:"<<" + Bad1 = 1 < 2, + // expected-warning@+3 {{comparison operator '>' is potentially a typo for a shift operator '>>'}} + // expected-note@+2 {{use '>>' to perform a bitwise shift}} + // CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:12-[[@LINE+1]]:13}:">>" + Bad2 = 1 > 3, + // expected-warning@+3 {{comparison operator '>' is potentially a typo for a shift operator '>>'}} + // expected-note@+2 {{use '>>' to perform a bitwise shift}} + // CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:13-[[@LINE+1]]:14}:">>" + Bad3 = (1 > 3) +}; + +enum PossibleTypoRight { + Val2 = 1 >> 0, + // expected-warning@+3 {{comparison operator '<' is potentially a typo for a shift operator '<<'}} + // expected-note@+2 {{use '<<' to perform a bitwise shift}} + // CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:12-[[@LINE+1]]:13}:"<<" + Bad4 = 1 < 2, + // expected-warning@+3 {{comparison operator '>' is potentially a typo for a shift operator '>>'}} + // expected-note@+2 {{use '>>' to perform a bitwise shift}} + // CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:12-[[@LINE+1]]:13}:">>" + Bad5 = 1 > 3, + // expected-warning@+3 {{comparison operator '<' is potentially a typo for a shift operator '<<'}} + // expected-note@+2 {{use '<<' to perform a bitwise shift}} + // CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:13-[[@LINE+1]]:14}:"<<" + Bad6 = (1 < 3) +}; + +// Case 3: Context provided by other bitwise operators (&, |) +// Even though there are no shifts, the presence of '|' implies flags. +enum PossibleTypoBitwiseOr { + FlagA = 0x1, + FlagB = 0x2, + FlagCombo = FlagA | FlagB, + // expected-warning@+3 {{comparison operator '<' is potentially a typo for a shift operator '<<'}} + // expected-note@+2 {{use '<<' to perform a bitwise shift}} + // CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:17-[[@LINE+1]]:18}:"<<" + FlagTypo1 = 1 < FlagCombo, + // expected-warning@+3 {{comparison operator '>' is potentially a typo for a shift operator '>>'}} + // expected-note@+2 {{use '>>' to perform a bitwise shift}} + // CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:17-[[@LINE+1]]:18}:">>" + FlagTypo2 = 1 > FlagCombo +}; + +enum PossibleTypoBitwiseAnd { + FlagAnd = FlagA & FlagB, + // expected-warning@+3 {{comparison operator '<' is potentially a typo for a shift operator '<<'}} + // expected-note@+2 {{use '<<' to perform a bitwise shift}} + // CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:17-[[@LINE+1]]:18}:"<<" + FlagTypo3 = 1 < FlagAnd, + // expected-warning@+3 {{comparison operator '>' is potentially a typo for a shift operator '>>'}} + // expected-note@+2 {{use '>>' to perform a bitwise shift}} + // CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:17-[[@LINE+1]]:18}:">>" + FlagTypo4 = 1 > FlagAnd +}; + +enum NoWarningOnDirectInit { + A = 0, + B = 1, + Ok1 = 1 < 2, // No warning expected + Ok2 = 1 > 2 // No warning expected +}; + +enum NoWarningOnArith { + D = 0 + 1, + E = D * 10, + F = E - D, + G = F / D, + Ok3 = 1 < E, // No warning expected + Ok4 = 1 > E // No warning expected +}; + +enum NoWarningOnExplicitCast { + Bit1 = 1 << 0, + Ok5 = (int)(1 < 2) // No warning expected +}; + +enum NoWarningOnNoneBitShift { + Bit2 = 1 << 0, + Ok6 = (3 < 2) // No warning expected +}; + +// Ensure the diagnostic group works +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wenum-compare-typo" +enum IGNORED { + Ok7 = 1 << 1, + Ignored3 = 1 < 10 // No warning +}; +#pragma clang diagnostic pop |
