summaryrefslogtreecommitdiffstats
path: root/clang/lib/Sema
diff options
context:
space:
mode:
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>2011-02-01 18:24:22 +0000
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>2011-02-01 18:24:22 +0000
commit8b6ec6870f65f4a4d4c88b2a9715706ba34ee6d9 (patch)
tree7d265363387c5fd3dc6641e7968ec640bd96a12f /clang/lib/Sema
parent5b4288440dd011062210c9f9e6ce4c50b03f4efb (diff)
downloadbcm5719-llvm-8b6ec6870f65f4a4d4c88b2a9715706ba34ee6d9.tar.gz
bcm5719-llvm-8b6ec6870f65f4a4d4c88b2a9715706ba34ee6d9.zip
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. llvm-svn: 124668
Diffstat (limited to 'clang/lib/Sema')
-rw-r--r--clang/lib/Sema/SemaExpr.cpp22
1 files changed, 22 insertions, 0 deletions
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 21bf4203e3e..82771298e0a 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/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))
OpenPOWER on IntegriCloud