[cfe-dev] Question on -Wswitch-enum

Nico Weber thakis at chromium.org
Fri Aug 3 08:06:13 PDT 2012


On Fri, Aug 3, 2012 at 2:43 AM, Nicola Gigante <nicola.gigante at gmail.com> wrote:
> Hello.
>
> After switching to the clang shipped with Xcode 4.4,
> new warnings appeared in my previously "warnings-free" codebase.
>
> This is an example code:
> enum Enum {
>    First,
>    Second,
>    Third,
>    Fourth
> };
>
> int func(Enum e);
>
> int func(Enum e)
> {
>    switch(e)
>    {
>       case First:
>          return 42;
>       case Second:
>          return 84;
>       default:
>          return 0;
>    }
> }
>
> Compiling with -Weverything I get this warning:

Compiling with -Weverything isn't a good idea I think. It enables
warnings that aren't generally useful and warnings that aren't fully
baked. Warnings that are useful will eventually find their way into
-Wall or become enabled by default.

Nico

> prova.cpp:13:11: warning: enumeration values 'Third' and 'Fourth' not explicitly handled in switch [-Wswitch-enum]
>    switch(e)
>
> In my opinion, the presence of the default: label in the switch statement should silence this warning,
> because those enumeration values are actually handled. It's not like something like this:
> switch(e)
> {
>    case First:
>       return 42;
>    case Second:
>       return 84;
> }
>
> where the developer could have forgotten to handle all the cases.
> This warning is causing a lot of noise in my project, where i have enums with dozens of enum values and
> to silence this warning I have to replicate 70 case labels in every switch statement, while silencing this warning
> with -Wno-switch-enum would make it disappear also for really wrong cases like the last snippet above.
>
> So I would like to know if this is intentional, and if so what is the rationale behind this behaviour.
>
> Thank you,
> Nicola
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev



More information about the cfe-dev mailing list