Allow return statements from case labels (#1537)

This commit is contained in:
Zachary Henkel
2019-11-21 13:10:08 -06:00
committed by Herb Sutter
parent 0363246c03
commit cd478777c1

View File

@@ -9890,7 +9890,7 @@ Statement rules:
* [ES.75: Avoid `do`-statements](#Res-do) * [ES.75: Avoid `do`-statements](#Res-do)
* [ES.76: Avoid `goto`](#Res-goto) * [ES.76: Avoid `goto`](#Res-goto)
* [ES.77: Minimize the use of `break` and `continue` in loops](#Res-continue) * [ES.77: Minimize the use of `break` and `continue` in loops](#Res-continue)
* [ES.78: Always end a non-empty `case` with a `break`](#Res-break) * [ES.78: Don't rely on implicit fallthrough in `switch` statements](#Res-break)
* [ES.79: Use `default` to handle common cases (only)](#Res-default) * [ES.79: Use `default` to handle common cases (only)](#Res-default)
* [ES.84: Don't try to declare a local variable with no name](#Res-noname) * [ES.84: Don't try to declare a local variable with no name](#Res-noname)
* [ES.85: Make empty statements visible](#Res-empty) * [ES.85: Make empty statements visible](#Res-empty)
@@ -12663,11 +12663,11 @@ If you really need to break out a loop, a `break` is typically better than alter
??? ???
### <a name="Res-break"></a>ES.78: Always end a non-empty `case` with a `break` ### <a name="Res-break"></a>ES.78: Don't rely on implicit fallthrough in `switch` statements
##### Reason ##### Reason
Accidentally leaving out a `break` is a fairly common bug. Always end a non-empty `case` with a `break`. Accidentally leaving out a `break` is a fairly common bug.
A deliberate fallthrough can be a maintenance hazard and should be rare and explicit. A deliberate fallthrough can be a maintenance hazard and should be rare and explicit.
##### Example ##### Example
@@ -12694,6 +12694,16 @@ Multiple case labels of a single statement is OK:
break; break;
} }
Return statements in a case label are also OK:
switch (x) {
case 'a':
return 1;
case 'b':
return 2;
case 'c':
return 3;
}
##### Exceptions ##### Exceptions
In rare cases if fallthrough is deemed appropriate, be explicit and use the `[[fallthrough]]` annotation: In rare cases if fallthrough is deemed appropriate, be explicit and use the `[[fallthrough]]` annotation: