The following test warns about the comparison being always true only for two of the six tests: #include <cstdlib> void a (unsigned char x) { if (x < 0) abort (); } void b (unsigned short x) { if (x < 0) abort (); } void c (unsigned int x) { if (x < 0) abort (); } void d (unsigned long x) { if (x < 0) abort (); } void e (unsigned long long x) { if (x < 0) abort (); } void f (size_t x) { if (x < 0) abort (); } wilx@logout:::~/tmp> g++ -save-temps -fverbose-asm -O3 -c test.cxx test.cxx: In function 'void a(unsigned char)': test.cxx:5: warning: comparison is always false due to limited range of data type test.cxx: In function 'void b(short unsigned int)': test.cxx:11: warning: comparison is always false due to limited range of data type wilx@logout:::~/tmp> g++ -v Using built-in specs. Target: i386-unknown-freebsd4.10 Configured with: ../srcdir/configure --disable-nls --enable-version-specific-runtime-libs --enable-dwarf2 --with-cpu=pentium3 --with-arch=pentium3 --with-system-zlib --disable-shared --prefix=/home/4/wilx --enable-languages=c,c++,objc,ada --disable-sjlj-exceptions --enable-shared=libstdc++ --enable-shared=libobjc Thread model: posix gcc version 4.1.0 20050821 (experimental)
We do have an inconstaincy here, with -W, I get a warning for all 6 of them.
t.c: In function ‘void c(unsigned int)’: t.c:17: warning: comparison of unsigned expression < 0 is always false t.c: In function ‘void d(long unsigned int)’: t.c:23: warning: comparison of unsigned expression < 0 is always false t.c: In function ‘void e(long long unsigned int)’: t.c:29: warning: comparison of unsigned expression < 0 is always false
*** Bug 29694 has been marked as a duplicate of this bug. ***
Subject: Bug 23587 Author: manu Date: Sun May 20 20:29:55 2007 New Revision: 124875 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=124875 Log: 2007-05-20 Manuel Lopez-Ibanez <manu@gcc.gnu.org> PR middle-end/7651 PR c++/11856 PR c/12963 PR c/23587 PR other/29694 * c.opt (Wtype-limits): New. * doc/invoke.texi (Wtype-limits): Document it. (Wextra): Enabled by -Wextra. * c-opts.c (c_common_post_options): Enabled by -Wextra. * c-common.c (shorten_compare): Warn with Wtype-limits. testsuite/ * gcc.dg/compare6.c: Replace Wall with Wtype-limits. * gcc.dg/Wtype-limits.c: New. * gcc.dg/Wtype-limits-Wextra.c: New. * gcc.dg/Wtype-limits-no.c: New. * g++.dg/warn/Wtype-limits.C: New. * g++.dg/warn/Wtype-limits-Wextra.C: New. * g++.dg/warn/Wtype-limits-no.C: New. Added: trunk/gcc/testsuite/g++.dg/warn/Wtype-limits-Wextra.C trunk/gcc/testsuite/g++.dg/warn/Wtype-limits-no.C trunk/gcc/testsuite/g++.dg/warn/Wtype-limits.C trunk/gcc/testsuite/gcc.dg/Wtype-limits-Wextra.c trunk/gcc/testsuite/gcc.dg/Wtype-limits-no.c trunk/gcc/testsuite/gcc.dg/Wtype-limits.c Modified: trunk/gcc/ChangeLog trunk/gcc/c-common.c trunk/gcc/c-opts.c trunk/gcc/c.opt trunk/gcc/doc/invoke.texi trunk/gcc/testsuite/ChangeLog trunk/gcc/testsuite/gcc.dg/compare6.c
Fixed for GCC 4.3 (all warnings have been grouped under -Wtype-limits).