public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* [Fwd: Re: assertion warnings]
@ 2005-11-16  6:49 bill
  2005-11-16 14:50 ` Tony Wetmore
  0 siblings, 1 reply; 2+ messages in thread
From: bill @ 2005-11-16  6:49 UTC (permalink / raw)
  To: gcc-help

For exactly the same reasons that "if (i=2)" gives
a warning--it is syntactically correct, but it's
usually an error.  Assertions should not have side 
effects, and coding that uses side effects of an
assertion is very bad practice.  Coding "assert(x=2)"
or "assert(x++)" is almost always an unintended
mistake or bad design, and I'm hoping there's a way
to get gcc to catch instances for me.  An instance
of this sort came up yesterday, and I'd really like
to be able to easily modify my makefiles to catch
any other instances.

Note, that I don't think it "must" give a warning.  I'm
asking if there is a way I can coerce it to do so, eg 
'-Wassertions_with_side_effects'


Arturas Moskvinas wrote:

>Why do you think it must give you a warning? x=2 returns some result,
>and x++ also returns some result.
>
>Arturas M.
>
>  



>>Is there any way to get gcc to generate warnings for the following code?
>>-Wall gives no complaints at all.
>>I expect that it's asking too much to get a warning for the first
>>assertion, but
>>the other two seem to be pretty obvious candidates for a warning.
>>
>>#include <assert.h>
>>int
>>foo(int *x)
>>{
>>    *x = *x+1;
>>    return *x;
>>}
>>
>>int
>>main()
>>{
>>    int x;
>>    assert(foo(&x));
>>    assert(x=2);
>>    assert(x++);
>>    return 0;
>>}
>>    
>>

>



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2005-11-16 14:50 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-16  6:49 [Fwd: Re: assertion warnings] bill
2005-11-16 14:50 ` Tony Wetmore

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).