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) {
43 bool TraverseRequiresExpr(RequiresExpr *,
44 DataRecursionQueue * =
nullptr) {
48 bool VisitBinaryOperator(BinaryOperator *BO) {
49 if (BO->isAssignmentOp())
54 bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *OCE) {
55 if (OCE->isAssignmentOp())
61 ConditionVisitor(Check).TraverseStmt(If->getCond());
65 Visitor(*this).TraverseAST(*Result.Context);
69 SourceLocation OpLoc =
70 isa<BinaryOperator>(AssignmentExpr)
71 ? cast<BinaryOperator>(AssignmentExpr)->getOperatorLoc()
72 : cast<CXXOperatorCallExpr>(AssignmentExpr)->getOperatorLoc();
74 diag(OpLoc,
"an assignment within an 'if' condition is bug-prone")
75 << AssignmentExpr->getSourceRange();
77 "if it should be an assignment, move it out of the 'if' condition",
79 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)