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

Alexander Kornienko via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 23 08:13:54 PDT 2017


Author: alexfh
Date: Thu Mar 23 10:13:54 2017
New Revision: 298607

URL: http://llvm.org/viewvc/llvm-project?rev=298607&view=rev
Log:
[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

Modified:
    clang-tools-extra/trunk/clang-tidy/misc/RedundantExpressionCheck.cpp
    clang-tools-extra/trunk/test/clang-tidy/misc-redundant-expression.cpp

Modified: clang-tools-extra/trunk/clang-tidy/misc/RedundantExpressionCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/RedundantExpressionCheck.cpp?rev=298607&r1=298606&r2=298607&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/RedundantExpressionCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/RedundantExpressionCheck.cpp Thu Mar 23 10:13:54 2017
@@ -239,6 +239,11 @@ static bool rangesFullyCoverDomain(Binar
       (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;
 }
 

Modified: clang-tools-extra/trunk/test/clang-tidy/misc-redundant-expression.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-redundant-expression.cpp?rev=298607&r1=298606&r2=298607&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/misc-redundant-expression.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/misc-redundant-expression.cpp Thu Mar 23 10:13:54 2017
@@ -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;




More information about the cfe-commits mailing list