switch/default -- enum, want compiler error

Hal Murray halmurray at sonic.net
Wed Apr 8 00:21:27 UTC 2026


> I believe clangd 21 has -Wcovered-switch-default which is slightly
> better, but still not what you seek. It raises a warning if you cover all
> cases and have a default. 

Neat.  Thanks.

I couldn't find anything about in in my gcc, but I did find
       -Wswitch-enum
           Warn whenever a "switch" statement has an index of enumerated 
type
           and lacks a "case" for one or more of the named codes of that
           enumeration.  "case" labels that do not correspond to 
enumerators
           also provoke warnings when this option is used, unless the
           enumeration is marked with the "flag_enum" attribute.  The only
           difference between -Wswitch and this option is that this option
           gives a warning about an omitted enumeration code even if there 
is a
           "default" label.
I still have to add the default case, and it's wasted code, but it does 
complain if I forget a case.

The gcc man page describes all the warnings.  Where does clang tell me 
about theirs?


If you have an enum with lots of values, you might end up writing code 
that only processes a few cases and ignores all the others.  With this 
option, you will have to write a case that explicitly mentions all the 
"others" that you are ignoring.  I'm willing to do that so that I can add 
a new case and have the compiler tell me if I forgot to think about a 
switch in some module way over there.




-- 
These are my opinions.  I hate spam.





More information about the devel mailing list