diff options
| author | Gabor Horvath <xazax.hun@gmail.com> | 2017-12-20 12:22:16 +0000 |
|---|---|---|
| committer | Gabor Horvath <xazax.hun@gmail.com> | 2017-12-20 12:22:16 +0000 |
| commit | 91c6671a71449fbd51a6f6214bca43aa7ce690c5 (patch) | |
| tree | 31a7b5170a55ad673e906f2a7b69d2601e541a28 /clang-tools-extra/test/clang-tidy/misc-redundant-expression.cpp | |
| parent | bf8519b5c9aa2c381b5f4725c21601a99b1b37c2 (diff) | |
| download | bcm5719-llvm-91c6671a71449fbd51a6f6214bca43aa7ce690c5.tar.gz bcm5719-llvm-91c6671a71449fbd51a6f6214bca43aa7ce690c5.zip | |
[clang-tidy] Misc redundant expression checker updated for ineffective bitwise operator expressions
Examples:
* Always evaluates to 0:
```
int X;
if (0 & X) return;
```
* Always evaluates to ~0:
```
int Y;
if (Y | ~0) return;
```
* The symbol is unmodified:
```
int Z;
Z &= ~0;
```
Patch by: Lilla Barancsuk!
Differential Revision: https://reviews.llvm.org/D39285
llvm-svn: 321168
Diffstat (limited to 'clang-tools-extra/test/clang-tidy/misc-redundant-expression.cpp')
| -rw-r--r-- | clang-tools-extra/test/clang-tidy/misc-redundant-expression.cpp | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/clang-tools-extra/test/clang-tidy/misc-redundant-expression.cpp b/clang-tools-extra/test/clang-tidy/misc-redundant-expression.cpp index d4644f2f363..36ffbbdd52f 100644 --- a/clang-tools-extra/test/clang-tidy/misc-redundant-expression.cpp +++ b/clang-tools-extra/test/clang-tidy/misc-redundant-expression.cpp @@ -154,6 +154,8 @@ bool TestOverloadedOperator(MyStruct& S) { // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: both sides of overloaded operator are equivalent if (S >= S) return true; // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: both sides of overloaded operator are equivalent + + return true; } #define LT(x, y) (void)((x) < (y)) @@ -662,3 +664,59 @@ int TestWithMinMaxInt(int X) { return 0; } + +#define FLAG1 1 +#define FLAG2 2 +#define FLAG3 4 +#define FLAGS (FLAG1 | FLAG2 | FLAG3) +#define NOTFLAGS !(FLAG1 | FLAG2 | FLAG3) +int operatorConfusion(int X, int Y, long Z) +{ + // Ineffective & expressions. + Y = (Y << 8) & 0xff; + // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: ineffective bitwise and operation. + Y = (Y << 12) & 0xfff; + // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: ineffective bitwise and + Y = (Y << 12) & 0xff; + // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: ineffective bitwise and + Y = (Y << 8) & 0x77; + // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: ineffective bitwise and + Y = (Y << 5) & 0x11; + // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: ineffective bitwise and + + // Tests for unmatched types + Z = (Z << 8) & 0xff; + // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: ineffective bitwise and operation. + Y = (Y << 12) & 0xfffL; + // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: ineffective bitwise and + Z = (Y << 12) & 0xffLL; + // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: ineffective bitwise and + Y = (Z << 8L) & 0x77L; + // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: ineffective bitwise and + + // Effective expressions. Do not check. + Y = (Y << 4) & 0x15; + Y = (Y << 3) & 0x250; + Y = (Y << 9) & 0xF33; + + int K = !(1 | 2 | 4); + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: ineffective logical negation operator used; did you mean '~'? + // CHECK-FIXES: {{^}} int K = ~(1 | 2 | 4);{{$}} + K = !(FLAG1 & FLAG2 & FLAG3); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: ineffective logical negation operator + // CHECK-FIXES: {{^}} K = ~(FLAG1 & FLAG2 & FLAG3);{{$}} + K = !(3 | 4); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: ineffective logical negation operator + // CHECK-FIXES: {{^}} K = ~(3 | 4);{{$}} + int NotFlags = !FLAGS; + // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: ineffective logical negation operator + // CHECK-FIXES: {{^}} int NotFlags = ~FLAGS;{{$}} + NotFlags = NOTFLAGS; + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: ineffective logical negation operator + return !(1 | 2 | 4); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: ineffective logical negation operator + // CHECK-FIXES: {{^}} return ~(1 | 2 | 4);{{$}} +} +#undef FLAG1 +#undef FLAG2 +#undef FLAG3 |

