[clang-tools-extra] Added options to readability-implicit-bool-conversion (PR #120087)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Dec 19 18:35:13 PST 2024
================
@@ -287,72 +292,83 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
auto BoolXor =
binaryOperator(hasOperatorName("^"), hasLHS(ImplicitCastFromBool),
hasRHS(ImplicitCastFromBool));
- auto ComparisonInCall = allOf(
- hasParent(callExpr()),
- hasSourceExpression(binaryOperator(hasAnyOperatorName("==", "!="))));
-
auto IsInCompilerGeneratedFunction = hasAncestor(namedDecl(anyOf(
isImplicit(), functionDecl(isDefaulted()), functionTemplateDecl())));
- Finder->addMatcher(
- traverse(TK_AsIs,
- implicitCastExpr(
- anyOf(hasCastKind(CK_IntegralToBoolean),
- hasCastKind(CK_FloatingToBoolean),
- hasCastKind(CK_PointerToBoolean),
- hasCastKind(CK_MemberPointerToBoolean)),
- // Exclude cases of C23 comparison result.
- unless(allOf(isC23(),
- hasSourceExpression(ignoringParens(
- binaryOperator(hasAnyOperatorName(
- ">", ">=", "==", "!=", "<", "<=")))))),
- // Exclude case of using if or while statements with variable
- // declaration, e.g.:
- // if (int var = functionCall()) {}
- unless(hasParent(
- stmt(anyOf(ifStmt(), whileStmt()), has(declStmt())))),
- // Exclude cases common to implicit cast to and from bool.
- unless(ExceptionCases), unless(has(BoolXor)),
- // Exclude C23 cases common to implicit cast to bool.
- unless(ComparisonInCall),
- // Retrieve also parent statement, to check if we need
- // additional parens in replacement.
- optionally(hasParent(stmt().bind("parentStmt"))),
- unless(isInTemplateInstantiation()),
- unless(IsInCompilerGeneratedFunction))
- .bind("implicitCastToBool")),
- this);
-
- auto BoolComparison = binaryOperator(hasAnyOperatorName("==", "!="),
- hasLHS(ImplicitCastFromBool),
- hasRHS(ImplicitCastFromBool));
- auto BoolOpAssignment = binaryOperator(hasAnyOperatorName("|=", "&="),
- hasLHS(expr(hasType(booleanType()))));
- auto BitfieldAssignment = binaryOperator(
- hasLHS(memberExpr(hasDeclaration(fieldDecl(hasBitWidth(1))))));
- auto BitfieldConstruct = cxxConstructorDecl(hasDescendant(cxxCtorInitializer(
- withInitializer(equalsBoundNode("implicitCastFromBool")),
- forField(hasBitWidth(1)))));
- Finder->addMatcher(
- traverse(
- TK_AsIs,
- implicitCastExpr(
- ImplicitCastFromBool, unless(ExceptionCases),
- // 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(BoolComparison, BoolXor,
- BoolOpAssignment, BitfieldAssignment)))),
- implicitCastExpr().bind("implicitCastFromBool"),
- unless(hasParent(BitfieldConstruct)),
- // Check also for nested casts, for example: bool -> int -> float.
- anyOf(hasParent(implicitCastExpr().bind("furtherImplicitCast")),
- anything()),
- unless(isInTemplateInstantiation()),
- unless(IsInCompilerGeneratedFunction))),
- this);
+ if (CheckConversionToBool) {
----------------
4m4n-x-B4w4ne wrote:
Done Thanks. Sorry for my carelessness.
https://github.com/llvm/llvm-project/pull/120087
More information about the cfe-commits
mailing list