From e96dad9544f0f87dc65ba097f4fb05b25a3fb804 Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Tue, 1 Mar 2011 20:34:48 +0000 Subject: [PATCH] Don't warn about unused values in ternary ?: expressions unless both the LHS and RHS are "unused" (side-effect free). Patch by Justin Bogner! Fixes PR 8282. llvm-svn: 126779 --- clang/lib/AST/Expr.cpp | 12 +++++++----- clang/test/Sema/warn-unused-value.c | 9 +++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp index 8e44c073682b..930457c249c2 100644 --- a/clang/lib/AST/Expr.cpp +++ b/clang/lib/AST/Expr.cpp @@ -1404,13 +1404,15 @@ bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1, return false; case ConditionalOperatorClass: { - // The condition must be evaluated, but if either the LHS or RHS is a - // warning, warn about them. + // 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(this); - if (Exp->getLHS() && - Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx)) + if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx)) + return false; + if (!Exp->getLHS()) return true; - return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx); + return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx); } case MemberExprClass: diff --git a/clang/test/Sema/warn-unused-value.c b/clang/test/Sema/warn-unused-value.c index 876eb9e4823e..95cd8fb70400 100644 --- a/clang/test/Sema/warn-unused-value.c +++ b/clang/test/Sema/warn-unused-value.c @@ -72,6 +72,15 @@ int test_logical_bar() { return x; } +// PR8282 +void conditional_for_control_flow(int cond, int x, int y) +{ + cond? y++ : x; // no-warning + cond? y : ++x; // no-warning + cond? (x |= y) : ++x; // no-warning + cond? y : x; // expected-warning {{expression result unused}} +} + struct s0 { int f0; }; void f0(int a);