]> granicus.if.org Git - clang/commitdiff
[AST] Fixed extraneous warnings for binary conditional operator
authorNathan Huckleberry <nhuck@google.com>
Wed, 19 Jun 2019 18:37:01 +0000 (18:37 +0000)
committerNathan Huckleberry <nhuck@google.com>
Wed, 19 Jun 2019 18:37:01 +0000 (18:37 +0000)
Summary:
Binary conditional operator gave warnings where ternary operators
did not. They have been fixed to warn similarly to ternary operators.

Link: https://bugs.llvm.org/show_bug.cgi?id=42239
Reviewers: rsmith, aaron.ballman, nickdesaulniers

Reviewed By: rsmith, nickdesaulniers

Subscribers: srhines, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D63369

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

lib/AST/Expr.cpp
test/Sema/warn-binary-conditional-expression-unused.c [new file with mode: 0644]

index f5714d937878bd6eb82d76ef4f2629de6edca86d..cc92689cb922c1ad5d69f3a7ec8f2a82f874f6be 100644 (file)
@@ -2453,12 +2453,13 @@ bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
     // If only one of the LHS or RHS is a warning, the operator might
     // be being used for control flow. Only warn if both the LHS and
     // RHS are warnings.
-    const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
-    if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
-      return false;
-    if (!Exp->getLHS())
-      return true;
-    return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
+    const auto *Exp = cast<ConditionalOperator>(this);
+    return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) &&
+           Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
+  }
+  case BinaryConditionalOperatorClass: {
+    const auto *Exp = cast<BinaryConditionalOperator>(this);
+    return Exp->getFalseExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
   }
 
   case MemberExprClass:
diff --git a/test/Sema/warn-binary-conditional-expression-unused.c b/test/Sema/warn-binary-conditional-expression-unused.c
new file mode 100644 (file)
index 0000000..982d66d
--- /dev/null
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -fsyntax-only -Wunused-value -verify %s
+int main() {
+    int a;
+    int b;
+    a ? : b; //expected-warning{{expression result unused}}
+    a ? a : b; //expected-warning{{expression result unused}}
+    a ? : ++b;
+    a ? a : ++b;
+    ++a ? : b; //expected-warning{{expression result unused}}
+    ++a ? a : b; //expected-warning{{expression result unused}}
+    ++a ? : ++b;
+    ++a ? a : ++b;
+    return 0;
+};
+