[cfe-dev] Question on -Wswitch-enum

Benjamin Kramer benny.kra at gmail.com
Fri Aug 3 03:51:28 PDT 2012


On 03.08.2012, at 11:43, 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:
> 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.

-Weverything includes ALL warnings clang knows about, even ones that may seem useless for most codebases like the one you just hit. As far as I know the recommended way of handling this is explicitly blacklisting warnings you don't want with "-Weverything -Wno-switch-enum".

-Wswitch-enum is not silenced by default cases, just as documented by gcc's manual.

- Ben



More information about the cfe-dev mailing list