Bug 44842 - gcc should not issue warnings for code that will never be executed
Summary: gcc should not issue warnings for code that will never be executed
Status: RESOLVED DUPLICATE of bug 4210
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: unknown
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-07-06 15:10 UTC by Vincent Lefèvre
Modified: 2010-07-06 17:24 UTC (History)
10 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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 ***