huangperry created this revision. Herald added a subscriber: carlosgalvezp. Herald added a project: All. huangperry requested review of this revision. Herald added a project: clang-tools-extra.
GitHub Issue #56009 Show implicit conversion warning to and from std::optional objects when running clang-tidy. Examples: std::optional<bool> a = 5; std::optional<int> b = false; Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D131517 Files: clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp Index: clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp +++ clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp @@ -8,6 +8,7 @@ #include "ImplicitBoolConversionCheck.h" #include "clang/AST/ASTContext.h" +#include "clang/AST/ExprCXX.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Lex/Lexer.h" #include "clang/Tooling/FixIt.h" @@ -270,7 +271,9 @@ // Prior to C++11 cast from bool literal to pointer was allowed. allOf(anyOf(hasCastKind(CK_NullToPointer), hasCastKind(CK_NullToMemberPointer)), - hasSourceExpression(cxxBoolLiteral()))), + hasSourceExpression(cxxBoolLiteral())), + hasDescendant(materializeTemporaryExpr( + allOf(hasType(booleanType()), hasDescendant(cxxBoolLiteral()))))), hasSourceExpression(expr(hasType(booleanType()))), unless(ExceptionCases)); auto BoolXor = @@ -282,7 +285,12 @@ anyOf(hasCastKind(CK_IntegralToBoolean), hasCastKind(CK_FloatingToBoolean), hasCastKind(CK_PointerToBoolean), - hasCastKind(CK_MemberPointerToBoolean)), + hasCastKind(CK_MemberPointerToBoolean), + hasSourceExpression(cxxConstructExpr( + hasType(classTemplateSpecializationDecl( + hasName("::std::optional"), + hasTemplateArgument(0, refersToType(booleanType())))), + unless(hasArgument(0, expr(hasType(booleanType()))))))), // Exclude case of using if or while statements with variable // declaration, e.g.: // if (int var = functionCall()) {}
Index: clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp +++ clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp @@ -8,6 +8,7 @@ #include "ImplicitBoolConversionCheck.h" #include "clang/AST/ASTContext.h" +#include "clang/AST/ExprCXX.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Lex/Lexer.h" #include "clang/Tooling/FixIt.h" @@ -270,7 +271,9 @@ // Prior to C++11 cast from bool literal to pointer was allowed. allOf(anyOf(hasCastKind(CK_NullToPointer), hasCastKind(CK_NullToMemberPointer)), - hasSourceExpression(cxxBoolLiteral()))), + hasSourceExpression(cxxBoolLiteral())), + hasDescendant(materializeTemporaryExpr( + allOf(hasType(booleanType()), hasDescendant(cxxBoolLiteral()))))), hasSourceExpression(expr(hasType(booleanType()))), unless(ExceptionCases)); auto BoolXor = @@ -282,7 +285,12 @@ anyOf(hasCastKind(CK_IntegralToBoolean), hasCastKind(CK_FloatingToBoolean), hasCastKind(CK_PointerToBoolean), - hasCastKind(CK_MemberPointerToBoolean)), + hasCastKind(CK_MemberPointerToBoolean), + hasSourceExpression(cxxConstructExpr( + hasType(classTemplateSpecializationDecl( + hasName("::std::optional"), + hasTemplateArgument(0, refersToType(booleanType())))), + unless(hasArgument(0, expr(hasType(booleanType()))))))), // Exclude case of using if or while statements with variable // declaration, e.g.: // if (int var = functionCall()) {}
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits