Author: alexfh Date: Thu May 4 11:06:08 2017 New Revision: 302164 URL: http://llvm.org/viewvc/llvm-project?rev=302164&view=rev Log: [clang-tidy] fix readability-implicit-bool-cast false positive with xor
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=302164&r1=302163&r2=302164&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/readability/ImplicitBoolCastCheck.cpp (original) +++ clang-tools-extra/trunk/clang-tidy/readability/ImplicitBoolCastCheck.cpp Thu May 4 11:06:08 2017 @@ -270,11 +270,22 @@ void ImplicitBoolCastCheck::registerMatc anyOf(hasParent(explicitCastExpr()), allOf(isMacroExpansion(), unless(isNULLMacroExpansion())), isInTemplateInstantiation(), hasAncestor(functionTemplateDecl()))); - + 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 boolXor = + binaryOperator(hasOperatorName("^"), hasLHS(implicitCastFromBool), + hasRHS(implicitCastFromBool)); Finder->addMatcher( implicitCastExpr( // Exclude cases common to implicit cast to and from bool. unless(exceptionCases), + unless(has(boolXor)), // Exclude case of using if or while statements with variable // declaration, e.g.: // if (int var = functionCall()) {} @@ -290,21 +301,12 @@ 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)); + auto boolOpAssignment = + binaryOperator(anyOf(hasOperatorName("|="), hasOperatorName("&=")), + hasLHS(expr(hasType(booleanType())))); Finder->addMatcher( implicitCastExpr( implicitCastFromBool, @@ -312,8 +314,8 @@ void ImplicitBoolCastCheck::registerMatc // in such context: // bool_expr_a == bool_expr_b // bool_expr_a != bool_expr_b - unless(hasParent( - binaryOperator(anyOf(boolComparison, boolOpAssignment)))), + unless(hasParent(binaryOperator( + anyOf(boolComparison, boolXor, 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=302164&r1=302163&r2=302164&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 11:06:08 2017 @@ -50,9 +50,12 @@ float implicitCastFromBoolInReturnValue( void implicitCastFromBoolInSingleBoolExpressions(bool b1, bool b2) { bool boolean = true; + boolean = b1 ^ b2; boolean = b1 && b2; boolean |= !b1 || !b2; boolean &= b1; + boolean = b1 == true; + boolean = b2 != false; int integer = boolean - 3; // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: implicit cast bool -> 'int' _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits