[Bug c/7652] -Wswitch-break : Warn if a switch case falls through

jasonwucj at gmail dot com gcc-bugzilla@gcc.gnu.org
Sun Jun 23 12:21:00 GMT 2013


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=7652

Chung-Ju Wu <jasonwucj at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jasonwucj at gmail dot com

--- Comment #19 from Chung-Ju Wu <jasonwucj at gmail dot com> ---
(In reply to David Binderman from comment #17)
> (In reply to Daniel Marjamäki from comment #7)
> > In my experience this type of check is really noisy if there is a warning
> > for every fall through.
> > 
> > I recommend that the warning is written only if the fall through cause
> > redundant or bad behaviour. such as:
> > 
> >     switch (foo) {
> >     case 1: x = y;   // <- redundant assignment
> >     case 2: x = z;
> >     };
> 
> I'd be happy with gcc warning for this kind of problem.
> 
> This specific case should be easier to catch than the 
> general case.


I believe such redundant assignment will be optimized out.


$ gcc --version
gcc (20130621) 4.9.0 20130621 (experimental)
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gcc -O2 -S pr7652.c


[pr7652.c]
  1
  2 extern int a;
  3 extern int b;
  4 extern int c;
  5
  6 int
  7 main(int argc, char **argv)
  8 {
  9   int x;
 10
 11   switch (argc)
 12     {
 13     case 1:
 14       x = a;
 15     case 7:
 16       x = b;
 17       break;
 18     default:
 19       x = c;
 20       break;
 21     }
 22
 23   return x;
 24 }


[pr7652.s]
  1         .file   "pr7652.c"
  2         .section        .text.startup,"ax",@progbits
  3         .p2align 4,,15
  4         .globl  main
  5         .type   main, @function
  6 main:
  7 .LFB0:
  8         .cfi_startproc
  9         movl    4(%esp), %eax
 10         cmpl    $1, %eax
 11         je      .L3
 12         cmpl    $7, %eax
 13         je      .L3
 14         movl    c, %eax
 15         ret
 16 .L3:
 17         movl    b, %eax
 18         ret
 19         .cfi_endproc
 20 .LFE0:
 21         .size   main, .-main
 22         .ident  "GCC: (20130621) 4.9.0 20130621 (experimental)"
 23         .section        .note.GNU-stack,"",@progbits


Apparently it is dead code.  IMHO, it may not be a good idea to
have compiler issue a warning everytime when compiler identifies
dead code statements.


More information about the Gcc-bugs mailing list