diff --git a/clang-tools-extra/clang-tidy/bugprone/AssignmentInIfConditionCheck.h b/clang-tools-extra/clang-tidy/bugprone/AssignmentInIfConditionCheck.h --- a/clang-tools-extra/clang-tidy/bugprone/AssignmentInIfConditionCheck.h +++ b/clang-tools-extra/clang-tidy/bugprone/AssignmentInIfConditionCheck.h @@ -25,7 +25,7 @@ : ClangTidyCheck(Name, Context) {} void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult &Result) override; - void report(const Expr *E); + void report(const Expr *AssignmentExpr); }; } // namespace bugprone diff --git a/clang-tools-extra/clang-tidy/bugprone/AssignmentInIfConditionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/AssignmentInIfConditionCheck.cpp --- a/clang-tools-extra/clang-tidy/bugprone/AssignmentInIfConditionCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/AssignmentInIfConditionCheck.cpp @@ -61,14 +61,18 @@ Visitor(*this).TraverseAST(*Result.Context); } -void AssignmentInIfConditionCheck::report(const Expr *MatchedDecl) { - diag(MatchedDecl->getBeginLoc(), - "an assignment within an 'if' condition is bug-prone"); - diag(MatchedDecl->getBeginLoc(), +void AssignmentInIfConditionCheck::report(const Expr *AssignmentExpr) { + SourceLocation OpLoc = + isa(AssignmentExpr) + ? cast(AssignmentExpr)->getOperatorLoc() + : cast(AssignmentExpr)->getOperatorLoc(); + + diag(OpLoc, "an assignment within an 'if' condition is bug-prone") + << AssignmentExpr->getSourceRange(); + diag(OpLoc, "if it should be an assignment, move it out of the 'if' condition", DiagnosticIDs::Note); - diag(MatchedDecl->getBeginLoc(), - "if it is meant to be an equality check, change '=' to '=='", + diag(OpLoc, "if it is meant to be an equality check, change '=' to '=='", DiagnosticIDs::Note); } diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/assignment-in-if-condition.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/assignment-in-if-condition.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/assignment-in-if-condition.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/assignment-in-if-condition.cpp @@ -3,7 +3,7 @@ void f(int arg) { int f = 3; if ((f = arg) || (f == (arg + 1))) - // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: an assignment within an 'if' condition is bug-prone [bugprone-assignment-in-if-condition] + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: an assignment within an 'if' condition is bug-prone [bugprone-assignment-in-if-condition] { f = 5; } @@ -12,7 +12,7 @@ void f1(int arg) { int f = 3; if ((f == arg) || (f = (arg + 1))) - // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: an assignment within an 'if' condition is bug-prone [bugprone-assignment-in-if-condition] + // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: an assignment within an 'if' condition is bug-prone [bugprone-assignment-in-if-condition] { f = 5; } @@ -21,7 +21,7 @@ void f2(int arg) { int f = 3; if (f = arg) - // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: an assignment within an 'if' condition is bug-prone [bugprone-assignment-in-if-condition] + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: an assignment within an 'if' condition is bug-prone [bugprone-assignment-in-if-condition] { f = 5; } @@ -32,7 +32,7 @@ void f3(int arg) { int f = 3; if ((f == arg) || ((arg + 6 < f) && (f = v))) - // CHECK-MESSAGES: :[[@LINE-1]]:40: warning: an assignment within an 'if' condition is bug-prone [bugprone-assignment-in-if-condition] + // CHECK-MESSAGES: :[[@LINE-1]]:42: warning: an assignment within an 'if' condition is bug-prone [bugprone-assignment-in-if-condition] { f = 5; } @@ -41,11 +41,11 @@ void f4(int arg) { int f = 3; if ((f == arg) || ((arg + 6 < f) && ((f = v) || (f < 8)))) - // CHECK-MESSAGES: :[[@LINE-1]]:41: warning: an assignment within an 'if' condition is bug-prone [bugprone-assignment-in-if-condition] + // CHECK-MESSAGES: :[[@LINE-1]]:43: warning: an assignment within an 'if' condition is bug-prone [bugprone-assignment-in-if-condition] { f = 5; } else if ((arg + 8 < f) && ((f = v) || (f < 8))) - // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: an assignment within an 'if' condition is bug-prone [bugprone-assignment-in-if-condition] + // CHECK-MESSAGES: :[[@LINE-1]]:35: warning: an assignment within an 'if' condition is bug-prone [bugprone-assignment-in-if-condition] { f = 6; } @@ -68,12 +68,12 @@ f = 6; } if (bo = 3) - // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: an assignment within an 'if' condition is bug-prone [bugprone-assignment-in-if-condition] + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: an assignment within an 'if' condition is bug-prone [bugprone-assignment-in-if-condition] { f = 7; } if ((arg == 3) || (bo = 6)) - // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: an assignment within an 'if' condition is bug-prone [bugprone-assignment-in-if-condition] + // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: an assignment within an 'if' condition is bug-prone [bugprone-assignment-in-if-condition] { f = 8; }