This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Fwd: Re: assertion warnings]


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;
}








Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]