I recently upgraded to the new compiler Clang LLVM 4.0 and its nice. Is just in this case it is showing me an ugly warning for some old legacy code:
case value not in enumerated type 'SomeConstants' (aka 'enum SomeConstants') [-Wswitch]
switch (var) {
case kConstant: case 3: case 4: case 8: case 35: //WARNING HERE :(
// do my thing here
break;
case kOtherConstant:
// do another thing here
break;
default:
break;
}
var
could be one of the values defined in the enum
something like this:
typedef enum SomeConstants {
kConstant,
kOtherConstant,
};
and as you see 2, 4, 8, 35 are not defined (that is why the compiler is complining), but in reality they happen (this is one the obscure parts of this closed source library I am using).
Is there a way I can somehow modify my switch code so I don't get the harmless but annoying warning? Right now I am silencing it using:
switch (var) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch"
case kConstant: case 3: case 4: case 8: case 35:
#pragma GCC diagnostic pop
...
I wonder if there is a more elegant way of solving this.
You can cast the expression of the switch()
statement to int
so it doesn't/can't perform that check.
After all, it's actually being used to hold an int
value, not one of the listed enumerators.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With