This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: No warning for unreachable code
Hi Segher,
On Wed, 4 Jun 2008, Segher Boessenkool wrote:
> > I just discovered a behaviour in gcc-4, which I don't understand: code
> >
> > unsigned int x;
> >
> > if (x < 0)
> > do_something();
> >
> > compiled with -Wall doesn't produce a warning, and the call to
> > do_something() is silently dropped, whereas if x is of type unsigned char,
> > I get as expected
> >
> > warning: comparison is always false due to limited range of data type
> >
> > Is there a reason for such behaviour or is this a bug?
>
> With -Wextra I get with both 4.2.4 and current TOT:
>
> x.c: In function 'f_int':
> x.c:5: warning: comparison of unsigned expression < 0 is always false
> x.c: In function 'f_char':
> x.c:11: warning: comparison is always false due to limited range of data type
>
> With just -Wall, 4.2.4 gives only the warning you mention, and TOT
> gives nothing.
>
> Are you suggesting that both warnings should be in -Wall? Or are
> you telling us that you should be using -Wextra ;-)
I'm suggesting that there's no big difference between unsigned char and
unsigned int (and unsigned long...) in this case, and, therefore
compiler's behaviour should be consistent. And in this case I personally
would prefer a warning in both cases with -Wall.
And this wasn't a purely theoretical observation. There is a driver in the
Linux kernel, that does exactly this: assigns function return code to an
unsigned int variable, and then checks for "<0"... And the compiler
happily throws the check away. Yes, I will submit a patch, but if there is
no strong reason against, maybe it would make sense to warn in these
cases.
Thanks
Guennadi
---
Guennadi Liakhovetski, Ph.D.
Freelance Open-Source Software Developer