[LLVMbugs] [Bug 17835] New: -Wunused-value and ternary operator gives unexpected behavior

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Wed Nov 6 13:13:15 PST 2013


            Bug ID: 17835
           Summary: -Wunused-value and ternary operator gives unexpected
           Product: clang
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: Static Analyzer
          Assignee: kremenek at apple.com
          Reporter: kaduk-llvm at mit.edu
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

This is using a trunk snapshot from FreeBSD's lang/clang-devel port, it seems
to be LLVM svn r193887.  The behavior is slightly different in older versions
of clang, but still unexpected/confusing.

This program:
int main(int argc, char **argv) {
    (1 < 2) ? 3 : 4;
    return 0;
(correctly) gives a warning: expression result unused (-Wunused-value),
pointing to the '3'.  However, if the '<' is changed to '>', the warning still
points to the '3' (and not the '4' as one might expect, given that the
conditional expression is a constant).
It seems that C99 requires that the two branches of the ternary expression have
the same (or compatible) type, so "by the spec" it suffices to check the return
value of just one branch.  However, if I apply a '(void)' cast to either the
'3' or the '4', with either the '<' or '>' version of the test program, no
warning is generated, even with -Wall.  If the warning points to only the '3'
value, it is nonintuitive that a cast on the '4' branch will quiesce the

I'm not really sure what the best resolution to this situation is -- from
looking at bug 10030 it seems that maybe one should expect CFG analysis in this
scope, but I am unfamiliar with the details.
Another thing to consider would be a warning if the types of the two branches
of the ternary expression were incompatible, that would make the behavior with
(void) casts easier to understand.

You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20131106/8022329e/attachment.html>

More information about the llvm-bugs mailing list