11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Lex/Lexer.h"
15 using namespace clang::ast_matchers;
21 void UseBoolLiteralsCheck::registerMatchers(MatchFinder *
Finder) {
22 if (!getLangOpts().CPlusPlus)
27 has(ignoringParenImpCasts(integerLiteral().bind(
"literal"))),
28 hasImplicitDestinationType(qualType(booleanType())),
29 unless(isInTemplateInstantiation()),
30 anyOf(hasParent(explicitCastExpr().bind(
"cast")), anything())),
35 hasParent(implicitCastExpr(
36 hasImplicitDestinationType(qualType(booleanType())),
37 unless(isInTemplateInstantiation()))),
38 eachOf(hasTrueExpression(
39 ignoringParenImpCasts(integerLiteral().bind(
"literal"))),
41 ignoringParenImpCasts(integerLiteral().bind(
"literal"))))),
45 void UseBoolLiteralsCheck::check(
const MatchFinder::MatchResult &
Result) {
46 const auto *Literal = Result.Nodes.getNodeAs<IntegerLiteral>(
"literal");
47 const auto *Cast = Result.Nodes.getNodeAs<Expr>(
"cast");
48 bool LiteralBooleanValue = Literal->getValue().getBoolValue();
50 if (Literal->isInstantiationDependent())
53 const Expr *Expression = Cast ? Cast : Literal;
56 diag(Expression->getExprLoc(),
57 "converting integer literal to bool, use bool literal instead");
59 if (!Expression->getLocStart().isMacroID())
60 Diag << FixItHint::CreateReplacement(
61 Expression->getSourceRange(), LiteralBooleanValue ?
"true" :
"false");
std::unique_ptr< ast_matchers::MatchFinder > Finder