[cfe-dev] Question on -Wswitch-enum

Nicola Gigante nicola.gigante at gmail.com
Fri Aug 3 02:43:35 PDT 2012


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.

Thank you,
Nicola



More information about the cfe-dev mailing list