GCC issues warnings like "division by zero" or "right shift count >= width of type" even though the corresponding code will never be executed (under a condition that is always false); it shouldn't do this, at least by default. For instance: int tst (void) { int x; x = 0 ? 1 / 0 : 0; return x; if (0) { x = 1 / 0; x = 1 >> 128; } return x; } $ gcc-snapshot -std=c99 -O2 -c tst.c tst.c: In function 'tst': tst.c:8:13: warning: division by zero [-Wdiv-by-zero] tst.c:9:7: warning: right shift count >= width of type [enabled by default] One can see that GCC detects neither the first "return x;" nor the always-false condition, and issues spurious warnings for the lines: x = 1 / 0; x = 1 >> 128; On the other hand, GCC could successfully detect that the 1 / 0 in x = 0 ? 1 / 0 : 0; would never be executed. Note: always-false conditions occur in practice for platform-dependent code, e.g. by doing a test on integer types.
Subject: Re: New: gcc should not issue warnings for code that will never be executed This is a dup of a much older bug which I cannot find right now. On Jul 6, 2010, at 8:10 AM, "vincent at vinc17 dot org" <gcc-bugzilla@gcc.gnu.org > wrote: > GCC issues warnings like "division by zero" or "right shift count >= > width of > type" even though the corresponding code will never be executed > (under a > condition that is always false); it shouldn't do this, at least by > default. For > instance: > > int tst (void) > { > int x; > x = 0 ? 1 / 0 : 0; > return x; > if (0) > { > x = 1 / 0; > x = 1 >> 128; > } > return x; > } > > $ gcc-snapshot -std=c99 -O2 -c tst.c > tst.c: In function 'tst': > tst.c:8:13: warning: division by zero [-Wdiv-by-zero] > tst.c:9:7: warning: right shift count >= width of type [enabled by > default] > > One can see that GCC detects neither the first "return x;" nor the > always-false > condition, and issues spurious warnings for the lines: > > x = 1 / 0; > x = 1 >> 128; > > On the other hand, GCC could successfully detect that the 1 / 0 in > > x = 0 ? 1 / 0 : 0; > > would never be executed. > > Note: always-false conditions occur in practice for platform- > dependent code, > e.g. by doing a test on integer types. > > > -- > Summary: gcc should not issue warnings for code that will > never > be executed > Product: gcc > Version: unknown > Status: UNCONFIRMED > Severity: enhancement > Priority: P3 > Component: c > AssignedTo: unassigned at gcc dot gnu dot org > ReportedBy: vincent at vinc17 dot org > > > http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44842 >
*** This bug has been marked as a duplicate of 4210 ***