[PATCH] Warn on empty switch statements

Richard Smith richard at metafoo.co.uk
Wed May 29 12:49:14 PDT 2013


On Wed, May 29, 2013 at 7:35 AM, Aaron Ballman <aaron at aaronballman.com>wrote:

> -Wunreachable-code doesn't warn on empty statement bodies for the
> switch.  So, for instance:
>
> int main() {
>   int i = 0;
>   switch(i) {}
> }
>
> yields no warnings in clang with -Wunreachable-code (or -Wall), but at
> /W3 in MSVC you get: warning C4060: switch statement contains no
> 'case' or 'default' labels.  Will it catch lots of bugs?  Probably
> not.  But it also should have basically no false positives either.  It
> would be nice for people who want their code to also be warning-free
> under MSVC.


I think we need more justification for adding the warning than this.

It doesn't seem reasonable to expect us to implement the union of all
warnings which every existent compiler produces. Some of those warnings are
really dumb. For instance, /W3 also enables C4800, which I don't think we
will ever want to implement.

We already warn on:

  switch(i);

... and changing the ';' to '{}' is one of the ways we expect people to
silence that warning, if (for whatever obscure reason) they really wanted a
switch with no body.

On Tue, May 28, 2013 at 11:07 PM, Richard Smith <richard at metafoo.co.uk>
> wrote:
> > What kinds of bugs does this catch that -Wunreachable-code misses?
> >
> > On Tue, May 28, 2013 at 8:27 AM, Aaron Ballman <aaron at aaronballman.com>
> > wrote:
> >>
> >> This patch addresses PR4546; it warns when a switch statement contains
> >> a body, but the body does not contain any case or default labels.
> >>
> >> ~Aaron
> >>
> >> _______________________________________________
> >> cfe-commits mailing list
> >> cfe-commits at cs.uiuc.edu
> >> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
> >>
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130529/8e3714c9/attachment.html>


More information about the cfe-commits mailing list