Is there any way to make gcc
or clang
warn about missing breaks in switch statements?
Specifically, I almost always want case statements to end with breaks, and it would be great it I could get the compiler to complain if I don't. Even better would be if it would look for either a break statement or a "// fall through" comment.
Is there a different solution people use to help themselves not screw this up?
With Clang trunk, use -Wimplicit-fallthrough
. If you're using C++11, intentional fallthrough can be marked with a [[clang::fallthrough]];
statement (see the documentation for this attribute for more information). The warning does not (yet) check for 'fall through' comments. This feature won't be in the upcoming 3.1 release of Clang, but it will (probably!) be in 3.2.
Edit: Clang's attribute is now part of C++17, under the name [[fallthrough]];
.