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())),
34 void UseBoolLiteralsCheck::check(
const MatchFinder::MatchResult &
Result) {
35 const auto *Literal = Result.Nodes.getNodeAs<IntegerLiteral>(
"literal");
36 const auto *Cast = Result.Nodes.getNodeAs<Expr>(
"cast");
37 bool LiteralBooleanValue = Literal->getValue().getBoolValue();
39 if (Literal->isInstantiationDependent())
42 const Expr *Expression = Cast ? Cast : Literal;
45 diag(Expression->getExprLoc(),
46 "converting integer literal to bool, use bool literal instead");
48 if (!Expression->getLocStart().isMacroID())
49 Diag << FixItHint::CreateReplacement(
50 Expression->getSourceRange(), LiteralBooleanValue ?
"true" :
"false");
std::unique_ptr< ast_matchers::MatchFinder > Finder