Bug 44842

Summary: gcc should not issue warnings for code that will never be executed
Product: gcc Reporter: Vincent Lefèvre <vincent-gcc>
Component: cAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: enhancement CC: bangerth, eggert, gcc-bugs, gjasny, gowen, jkratochvil, manu, mattiase, mnmoran, vincent-gcc
Priority: P3    
Version: unknown   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:

Description Vincent Lefèvre 2010-07-06 15:10:54 UTC
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.
Comment 1 pinskia@gmail.com 2010-07-06 16:33:54 UTC
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
>
Comment 2 Manuel López-Ibáñez 2010-07-06 17:24:00 UTC

*** This bug has been marked as a duplicate of 4210 ***