10#include "clang/AST/ASTContext.h"
11#include "clang/AST/RecursiveASTVisitor.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
19 Finder->addMatcher(translationUnitDecl(),
this);
23 const ast_matchers::MatchFinder::MatchResult &Result) {
24 class Visitor :
public RecursiveASTVisitor<Visitor> {
29 bool VisitIfStmt(IfStmt *If) {
30 class ConditionVisitor :
public RecursiveASTVisitor<ConditionVisitor> {
38 bool TraverseLambdaExpr(LambdaExpr *, DataRecursionQueue * =
nullptr) {
42 bool VisitBinaryOperator(BinaryOperator *BO) {
43 if (BO->isAssignmentOp())
48 bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *OCE) {
49 if (OCE->isAssignmentOp())
55 ConditionVisitor(Check).TraverseStmt(If->getCond());
59 Visitor(*this).TraverseAST(*Result.Context);
63 SourceLocation OpLoc =
64 isa<BinaryOperator>(AssignmentExpr)
65 ? cast<BinaryOperator>(AssignmentExpr)->getOperatorLoc()
66 : cast<CXXOperatorCallExpr>(AssignmentExpr)->getOperatorLoc();
68 diag(OpLoc,
"an assignment within an 'if' condition is bug-prone")
69 << AssignmentExpr->getSourceRange();
71 "if it should be an assignment, move it out of the 'if' condition",
73 diag(OpLoc,
"if it is meant to be an equality check, change '=' to '=='",
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Catches assignments within the condition clause of an if statement.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void report(const Expr *AssignmentExpr)