From: Argyrios Kyrtzidis Date: Tue, 1 Feb 2011 19:32:59 +0000 (+0000) Subject: For "if ((a == b))" only warn if 'a' is a modifiable l-value. Caught by John! X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=70f233017bd3091137dcfbc00f6b2baea56edba9;p=clang For "if ((a == b))" only warn if 'a' is a modifiable l-value. Caught by John! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@124675 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 82771298e0..8448ed8079 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -9231,7 +9231,9 @@ void Sema::DiagnoseEqualityWithExtraParens(ParenExpr *parenE) { Expr *E = parenE->IgnoreParens(); if (BinaryOperator *opE = dyn_cast(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(); diff --git a/test/SemaCXX/warn-assignment-condition.cpp b/test/SemaCXX/warn-assignment-condition.cpp index 48cc137dac..6cfab55440 100644 --- a/test/SemaCXX/warn-assignment-condition.cpp +++ b/test/SemaCXX/warn-assignment-condition.cpp @@ -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)) {} }