https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128193
Bug ID: 128193
Summary: -Wtype-limits does not always fire for g++ when gcc
warns
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: slyfox at gcc dot gnu.org
Target Milestone: ---
Was asked today why the warning is inconsistent between c and c++ and could not
come up with an answer. Can you help me figure it out? It looks like the
warning disappeared around gcc-6.
Bad example (against gcc-master from r17-5594-g1c3be8e0d429c2):
// $ cat a.c
int f(unsigned char c) { if (c > (255 - 0)) return 2; return 1; }
$ gcc/xg++ -B gcc -c a.c -Wall -Wextra
$ gcc/xgcc -B gcc -c a.c -Wall -Wextra
a.c: In function ‘f’:
a.c:1:32: warning: comparison is always false due to limited range of data type
[-Wtype-limits]
1 | int f(unsigned char c) { if (c > (255 - 0)) return 2; return 1; }
| ^
Why the mismatch? I would expect either 2 warning or none.
For comparison if we simplify `255 - 0` to `255` by hands both detect the
warning:
$ gcc/xg++ -B gcc -c a.c -Wall -Wextra
a.c: In function ‘int f(unsigned char)’:
a.c:1:32: warning: comparison is always false due to limited range of data type
[-Wtype-limits]
1 | int f(unsigned char c) { if (c > (255/* - 0*/)) return 2; return 1; }
| ~~^~~~~~~~~~~~~~~
$ gcc/xgcc -B gcc -c a.c -Wall -Wextra
a.c: In function ‘f’:
a.c:1:32: warning: comparison is always false due to limited range of data type
[-Wtype-limits]
1 | int f(unsigned char c) { if (c > (255/* - 0*/)) return 2; return 1; }
| ^
$ gcc/xgcc -B gcc -v
Reading specs from gcc/specs
COLLECT_GCC=gcc/xgcc
COLLECT_LTO_WRAPPER=gcc/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /home/slyfox/dev/git/gcc/configure --disable-multilib
--disable-bootstrap --disable-lto --disable-libsanitizer
--enable-languages=c,c++ CFLAGS='-O1 -g0' CXXFLAGS='-O1 -g0' LDFLAGS='-O1 -g0'
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 17.0.0 20261026 (experimental) (GCC)