diff options
author | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2011-02-01 18:24:22 +0000 |
---|---|---|
committer | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2011-02-01 18:24:22 +0000 |
commit | 0e2dc3a1159806c8303b0979be1ce1526cc64ed3 (patch) | |
tree | af7b32e291a316e1c5e7fae41b03e46fece42813 /lib/Sema | |
parent | 63b54104700873dc4a5b95b3108052580b5370e7 (diff) |
Warn for "if ((a == b))" where the equality expression is needlessly wrapped inside parentheses.
It's highly likely that the user intended an assignment used as condition.
Addresses rdar://8848646.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@124668 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Sema')
-rw-r--r-- | lib/Sema/SemaExpr.cpp | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 21bf4203e3..82771298e0 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -9225,8 +9225,30 @@ void Sema::DiagnoseAssignmentAsCondition(Expr *E) { << FixItHint::CreateInsertion(Close, ")"); } +/// \brief Redundant parentheses over an equality comparison can indicate +/// that the user intended an assignment used as condition. +void Sema::DiagnoseEqualityWithExtraParens(ParenExpr *parenE) { + Expr *E = parenE->IgnoreParens(); + + if (BinaryOperator *opE = dyn_cast<BinaryOperator>(E)) + if (opE->getOpcode() == BO_EQ) { + SourceLocation Loc = opE->getOperatorLoc(); + + Diag(Loc, diag::warn_equality_with_extra_parens) << E->getSourceRange(); + + Diag(Loc, diag::note_equality_comparison_to_assign) + << FixItHint::CreateReplacement(Loc, "="); + + Diag(Loc, diag::note_equality_comparison_silence) + << FixItHint::CreateRemoval(parenE->getSourceRange().getBegin()) + << FixItHint::CreateRemoval(parenE->getSourceRange().getEnd()); + } +} + bool Sema::CheckBooleanCondition(Expr *&E, SourceLocation Loc) { DiagnoseAssignmentAsCondition(E); + if (ParenExpr *parenE = dyn_cast<ParenExpr>(E)) + DiagnoseEqualityWithExtraParens(parenE); if (!E->isTypeDependent()) { if (E->isBoundMemberFunction(Context)) |