]> granicus.if.org Git - clang/commitdiff
For "if ((a == b))" only warn if 'a' is a modifiable l-value. Caught by John!
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Tue, 1 Feb 2011 19:32:59 +0000 (19:32 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Tue, 1 Feb 2011 19:32:59 +0000 (19:32 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@124675 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaExpr.cpp
test/SemaCXX/warn-assignment-condition.cpp

index 82771298e0ae0287f80fdebdf8cf107595008795..8448ed80793a2401c11faa5019e03fa851ef4550 100644 (file)
@@ -9231,7 +9231,9 @@ void Sema::DiagnoseEqualityWithExtraParens(ParenExpr *parenE) {
   Expr *E = parenE->IgnoreParens();
 
   if (BinaryOperator *opE = dyn_cast<BinaryOperator>(E))
-    if (opE->getOpcode() == BO_EQ) {
+    if (opE->getOpcode() == BO_EQ &&
+        opE->getLHS()->IgnoreParenImpCasts()->isModifiableLvalue(Context)
+                                                           == Expr::MLV_Valid) {
       SourceLocation Loc = opE->getOperatorLoc();
 
       Diag(Loc, diag::warn_equality_with_extra_parens) << E->getSourceRange();
index 48cc137dac732bfa7294cce9b05e9180512c0ebe..6cfab5544027441d9b4052d1f8f7e55314515fd3 100644 (file)
@@ -109,4 +109,14 @@ void test() {
   if ((x == 5)) {} // expected-warning {{equality comparison with extraneous parentheses}} \
                    // expected-note {{use '=' to turn this equality comparison into an assignment}} \
                    // expected-note {{remove extraneous parentheses around the comparison to silence this warning}}
+  if ((5 == x)) {}
+}
+
+void (*fn)();
+
+void test2() {
+    if ((fn == test2)) {} // expected-warning {{equality comparison with extraneous parentheses}} \
+                          // expected-note {{use '=' to turn this equality comparison into an assignment}} \
+                          // expected-note {{remove extraneous parentheses around the comparison to silence this warning}}
+    if ((test2 == fn)) {}
 }