]> granicus.if.org Git - clang/commitdiff
Don't emit an 'unused expression' warning for '||' and '&&' expressions that contain...
authorTed Kremenek <kremenek@apple.com>
Wed, 7 Apr 2010 18:49:21 +0000 (18:49 +0000)
committerTed Kremenek <kremenek@apple.com>
Wed, 7 Apr 2010 18:49:21 +0000 (18:49 +0000)
or similar side-effects.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@100676 91177308-0d34-0410-b5e6-96231b3b80d8

lib/AST/Expr.cpp
test/Sema/warn-unused-value.c

index 132245e671c580e8b438adeaaca2ddca3506496f..9c8de6bf9c8f70b8030563ae9ba708b4a5b55906 100644 (file)
@@ -837,19 +837,22 @@ bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
   }
   case BinaryOperatorClass: {
     const BinaryOperator *BO = cast<BinaryOperator>(this);
-    // Consider comma to have side effects if the LHS or RHS does.
-    if (BO->getOpcode() == BinaryOperator::Comma) {
-      // ((foo = <blah>), 0) is an idiom for hiding the result (and
-      // lvalue-ness) of an assignment written in a macro.
-      if (IntegerLiteral *IE =
-            dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
-        if (IE->getValue() == 0)
-          return false;
-
-      return (BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
-              BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
+    switch (BO->getOpcode()) {
+      default:
+        break;
+      // Consider ',', '||', '&&' to have side effects if the LHS or RHS does.
+      case BinaryOperator::Comma:
+        // ((foo = <blah>), 0) is an idiom for hiding the result (and
+        // lvalue-ness) of an assignment written in a macro.
+        if (IntegerLiteral *IE =
+              dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
+          if (IE->getValue() == 0)
+            return false;
+      case BinaryOperator::LAnd:
+      case BinaryOperator::LOr:
+        return (BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
+                BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
     }
-
     if (BO->isAssignmentOp())
       return false;
     Loc = BO->getOperatorLoc();
index 2e0fa54effbff271a09c54fcfdb14e1082483b99..cc8a848bbe218929ddc399a35e10ee2a52daa741 100644 (file)
@@ -51,3 +51,16 @@ void pr4806() {
   *pi;              // expected-warning {{expression result unused}}
   *pj;
 }
+
+// Don't warn about unused '||', '&&' expressions that contain assignments.
+int test_logical_foo1();
+int test_logical_foo2();
+int test_logical_foo3();
+int test_logical_bar() {
+  int x = 0;
+  (x = test_logical_foo1()) ||  // no-warning
+  (x = test_logical_foo2()) ||  // no-warning
+  (x = test_logical_foo3());    // no-warning
+  return x;
+}
+