Consider the following code:
enum MyEnum {
A, B, C;
}
int foo(MyEnum e) {
switch (e) {
case A:
return 1;
case B:
return 2;
case C:
return 3;
}
}
^ error: missing return statement
The compiler does not like this. Contrast this example with:
int bar() {
if (...) {
return 1;
} else {
return 2;
}
}
The issue with the switch could be addressed with a default
case, but you could argue that's unneeded here. All of the enum values are covered in the cases of the switch. Does static analysis of the switch statement know that, with returns in an exhaustive switch, the code block after the switch statement is unreachable?
I tried looking at the language spec, but I didn't see this point clearly addressed.