[cfe-dev] How can I use the result of a void expression? :p

Chris Lattner clattner at apple.com
Mon Aug 27 08:06:26 PDT 2007


On Aug 27, 2007, at 6:28 AM, Neil Booth wrote:

> Keith Bauer wrote:-
>
>>> Yep, that's a bug, but not because ?: has side effects.  It is
>>> because that warning should never be emitted for a void expression,
>>> patch here:
>>> http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-
>>> Mon-20070820/001680.html
>>
>> I think my patch is still necessary:
>>
>> int r(void);
>> void foo(int f)
>> {
>>     f ? r() : r() + r();
>> }
>> imac:Desktop keith$ gcc -fsyntax-only -Wall -Wextra side-effect- 
>> test.c
>> imac:Desktop keith$ clang -fsyntax-only side-effect-test.c
>> side-effect-test.c:4:5: warning: expression result unused
>>     f ? r() : r() + r();
>>     ^~~~~~~~~~~~~~~~~~~
>> 1 diagnostic generated.
>>
>> ( replacing + with , makes more sense but then my patch still isn't
>> sufficient ;) )
>>
>> Maybe the warning should never be emitted for expressions returning
>> void, but that's more of a consequence that a void expression should
>> always have a side effect than the underlying reason not to emit the
>> warning...
>
> Though it's most likely a separate issue, for the above testcase,
>
> 	f ? r() : r() + r();
> 	          ~~~ ^ ~~~
>
> "unused operation" would be more appropriate I suspect.

Aren't both correct?  In this case ?: is an operator returning int,  
and you're ignoring the returned value.  If you don't want this  
value, why not just use an if statement?

Keith, I'm saying that I think you *should* get a warning for your ?:  
line :)

-Chris



More information about the cfe-dev mailing list