Ok, so we are looking for a rule by which
bool doGoodThing; switch (some_variable) { case 1: case 2: doGoodThing = true; default: doGoodThing = false; }
will give a warning but
bool doGoodThing; switch (some_variable) { case 1: case 2: doGoodThing = true; break; default: doGoodThing = false; }
(which is supposedly the intended code here) will not. One relatively simple way to do this is to always warn of failure, unless the case is empty. That is, do not warn about failure from case 1 to case 2, because there is no code between them, but warn about failure from 2 to the default value. However, this approach still warns of code that may be intentional.
A more complicated rule would be to issue a warning when there is an assignment to the variable x , so that there is no possible execution path to use the assigned value. That is, in all possible execution paths in which x is assigned the value v , x will either be reassigned to something else, or go out of scope before any code that uses x is executed. This will warn about your code, but not a fixed example.
Performing this analysis is certainly possible. However, I do not know of a single compiler that currently does such an analysis (although all this really means that gcc is not working).
source share