3 ms·
Not sure about other IDEs, but for Eclipse for Java has a warning/error flag for enum/switch issues described here. http://help.eclipse.org/juno/index.jsp?topi
by kyrra 13y ago
Not sure about other IDEs, but for Eclipse for Java has a warning/error flag for enum/switch issues described here.
http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fpreferences%2Fjava%2Fcompiler%2Fref-preferences-errors-warnings.htm http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse....
> Incomplete 'switch' cases on enum
> When enabled, the compiler will issue an error or a warning whenever it encounters a switch statement which does not contain case statements for every enum constant of the referenced enum nor a default case. A default case is assumed to cover missing case statements for any enum constant, so the compiler is silent in the presence of default cases unless the option "Signal even if 'default' case exists" (see below) is enabled.
EDIT: oddly, I thought the blog post's first example was Java, not C#. Man, I forget how similar the syntax is.
- dllthomas 13y agoAs a side note, traditional enum completeness checking in C is broken, in that you can only choose either to be warned statically when you need to add a new case (no default case) or to handle things at runtime if you get something unexpected (a default case). Fairly recent gcc (last few years) now has a -Wswitch-enum flag which says "warn me even when there is a default label" which is the safest option (I assume clang has something similar though I haven't actually checked).
- nhaehnle 13y agoThis option is also really annoying when you have switches over really large enum types, such as key codes, where it is usually legitimate to have a default case. I guess even more annotations are needed ;)
- dllthomas 13y agoHeh, doubtless. If you have a switch statement where adding a case to the enum doesn't mean you need to consider whether to add it to that switch, it sounds like an appropriate place to use pragmas to disable the warning for the one switch (http://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html http://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html). If you do need to consider that switch, then I'd say you should still suck it up and list every case (though possibly collapsing similar groups of cases into a single macro... "#define NUMERIC_KEYS case KEY_KP0: case KEY_KP1"...).
- pjmlp 13y agoLots of things in C are broken, not only enums.
- dllthomas 13y agoThanks.