[clang-tools-extra] r302161 - [clang-tidy] fix readability-implicit-bool-cast false alarm on |=, &=

Alexander Kornienko via cfe-commits cfe-commits at lists.llvm.org
Thu May 4 08:34:31 PDT 2017


Author: alexfh
Date: Thu May  4 10:34:31 2017
New Revision: 302161

URL: http://llvm.org/viewvc/llvm-project?rev=302161&view=rev
Log:
[clang-tidy] fix readability-implicit-bool-cast false alarm on |=, &=

Modified:
    clang-tools-extra/trunk/clang-tidy/readability/ImplicitBoolCastCheck.cpp
    clang-tools-extra/trunk/test/clang-tidy/readability-implicit-bool-cast.cpp

Modified: clang-tools-extra/trunk/clang-tidy/readability/ImplicitBoolCastCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/ImplicitBoolCastCheck.cpp?rev=302161&r1=302160&r2=302161&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/ImplicitBoolCastCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/readability/ImplicitBoolCastCheck.cpp Thu May  4 10:34:31 2017
@@ -40,24 +40,6 @@ AST_MATCHER(Stmt, isNULLMacroExpansion)
   return isNULLMacroExpansion(&Node, Finder->getASTContext());
 }
 
-ast_matchers::internal::Matcher<Expr> createExceptionCasesMatcher() {
-  return expr(anyOf(hasParent(explicitCastExpr()),
-                    allOf(isMacroExpansion(), unless(isNULLMacroExpansion())),
-                    isInTemplateInstantiation(),
-                    hasAncestor(functionTemplateDecl())));
-}
-
-StatementMatcher createImplicitCastFromBoolMatcher() {
-  return implicitCastExpr(
-      unless(createExceptionCasesMatcher()),
-      anyOf(hasCastKind(CK_IntegralCast), hasCastKind(CK_IntegralToFloating),
-            // Prior to C++11 cast from bool literal to pointer was allowed.
-            allOf(anyOf(hasCastKind(CK_NullToPointer),
-                        hasCastKind(CK_NullToMemberPointer)),
-                  hasSourceExpression(cxxBoolLiteral()))),
-      hasSourceExpression(expr(hasType(qualType(booleanType())))));
-}
-
 StringRef getZeroLiteralToCompareWithForType(CastKind CastExprKind,
                                              QualType Type,
                                              ASTContext &Context) {
@@ -284,10 +266,15 @@ void ImplicitBoolCastCheck::registerMatc
     return;
   }
 
+  auto exceptionCases = expr(
+      anyOf(hasParent(explicitCastExpr()),
+            allOf(isMacroExpansion(), unless(isNULLMacroExpansion())),
+            isInTemplateInstantiation(), hasAncestor(functionTemplateDecl())));
+
   Finder->addMatcher(
       implicitCastExpr(
           // Exclude cases common to implicit cast to and from bool.
-          unless(createExceptionCasesMatcher()),
+          unless(exceptionCases),
           // Exclude case of using if or while statements with variable
           // declaration, e.g.:
           //   if (int var = functionCall()) {}
@@ -303,17 +290,30 @@ void ImplicitBoolCastCheck::registerMatc
           .bind("implicitCastToBool"),
       this);
 
+  auto implicitCastFromBool = implicitCastExpr(
+      unless(exceptionCases),
+      anyOf(hasCastKind(CK_IntegralCast), hasCastKind(CK_IntegralToFloating),
+            // Prior to C++11 cast from bool literal to pointer was allowed.
+            allOf(anyOf(hasCastKind(CK_NullToPointer),
+                        hasCastKind(CK_NullToMemberPointer)),
+                  hasSourceExpression(cxxBoolLiteral()))),
+      hasSourceExpression(expr(hasType(booleanType()))));
+
+  auto boolComparison = binaryOperator(
+      anyOf(hasOperatorName("=="), hasOperatorName("!=")),
+      hasLHS(implicitCastFromBool), hasRHS(implicitCastFromBool));
+  auto boolOpAssignment = binaryOperator(
+      anyOf(hasOperatorName("|="), hasOperatorName("&=")),
+      hasLHS(expr(hasType(booleanType()))), hasRHS(implicitCastFromBool));
   Finder->addMatcher(
       implicitCastExpr(
-          createImplicitCastFromBoolMatcher(),
+          implicitCastFromBool,
           // Exclude comparisons of bools, as they are always cast to integers
           // in such context:
           //   bool_expr_a == bool_expr_b
           //   bool_expr_a != bool_expr_b
-          unless(hasParent(binaryOperator(
-              anyOf(hasOperatorName("=="), hasOperatorName("!=")),
-              hasLHS(createImplicitCastFromBoolMatcher()),
-              hasRHS(createImplicitCastFromBoolMatcher())))),
+          unless(hasParent(
+              binaryOperator(anyOf(boolComparison, boolOpAssignment)))),
           // Check also for nested casts, for example: bool -> int -> float.
           anyOf(hasParent(implicitCastExpr().bind("furtherImplicitCast")),
                 anything()))

Modified: clang-tools-extra/trunk/test/clang-tidy/readability-implicit-bool-cast.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/readability-implicit-bool-cast.cpp?rev=302161&r1=302160&r2=302161&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/readability-implicit-bool-cast.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/readability-implicit-bool-cast.cpp Thu May  4 10:34:31 2017
@@ -48,8 +48,11 @@ float implicitCastFromBoolInReturnValue(
   // CHECK-FIXES: return static_cast<float>(boolean);
 }
 
-void implicitCastFromBoolInSingleBoolExpressions() {
+void implicitCastFromBoolInSingleBoolExpressions(bool b1, bool b2) {
   bool boolean = true;
+  boolean = b1 && b2;
+  boolean |= !b1 || !b2;
+  boolean &= b1;
 
   int integer = boolean - 3;
   // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: implicit cast bool -> 'int'




More information about the cfe-commits mailing list