Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: case not evaluated in enumerated type?

Tags:

c

gcc

clang

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:

Warning: 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.

like image 446
nacho4d Avatar asked Jun 18 '12 04:06

nacho4d


1 Answers

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.

like image 113
Potatoswatter Avatar answered Oct 23 '22 19:10

Potatoswatter