[clang-tidy] Catch trivially true statements like a != 1 || a != 3

Catch trivially true statements of the form a != 1 || a != 3. Statements like
these are likely errors. They are particularly easy to miss when handling enums:

enum State {
RUNNING,
STOPPED,
STARTING,
ENDING
}

...
if (state != RUNNING || state != STARTING)
...

Patch by Blaise Watson!

Differential revision: https://reviews.llvm.org/D29858

llvm-svn: 298607
This commit is contained in:
Alexander Kornienko 2017-03-23 15:13:54 +00:00
parent 11ad339127
commit c3acd2e9c0
2 changed files with 9 additions and 0 deletions

View File

@ -239,6 +239,11 @@ static bool rangesFullyCoverDomain(BinaryOperatorKind OpcodeLHS,
(OpcodeRHS == BO_LT || OpcodeRHS == BO_LE))
return true;
// Handle cases where constants are different but both ops are !=, like:
// x != 5 || x != 10
if (OpcodeLHS == BO_NE && OpcodeRHS == BO_NE)
return true;
return false;
}

View File

@ -321,6 +321,8 @@ int TestRelational(int X, int Y) {
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: logical expression is always true
if (X <= 10 || X >= 11) return 1;
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: logical expression is always true
if (X != 7 || X != 14) return 1;
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: logical expression is always true
if (X < 7 && X < 6) return 1;
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: expression is redundant
@ -422,6 +424,8 @@ int TestRelatiopnalWithEnum(enum Color C) {
// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: logical expression is always false
if (C == Red && C != Red) return 1;
// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: logical expression is always false
if (C != Red || C != Yellow) return 1;
// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: logical expression is always true
// Should not match.
if (C == Red || C == Yellow) return 1;