Skip to content

Commit

Permalink
Adapt analyzer to new warnings around switch-case fallthrough.
Browse files Browse the repository at this point in the history
Fixes issue #27664.
BUG= http://dartbug.com/27664
R=brianwilkerson@google.com

Review URL: https://codereview.chromium.org/2446093003 .
  • Loading branch information
lrhn committed Oct 26, 2016
1 parent 103881d commit 1322877
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
6 changes: 3 additions & 3 deletions pkg/analyzer/lib/src/error/codes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3279,14 +3279,14 @@ class StaticWarningCode extends ErrorCode {

/**
* 13.9 Switch: It is a static warning if the last statement of the statement
* sequence <i>s<sub>k</sub></i> is not a break, continue, return or throw
* statement.
* sequence <i>s<sub>k</sub></i> is not a break, continue, rethrow, return
* or throw statement.
*/
static const StaticWarningCode CASE_BLOCK_NOT_TERMINATED =
const StaticWarningCode(
'CASE_BLOCK_NOT_TERMINATED',
"The last statement of the 'case' should be 'break', 'continue', "
"'return' or 'throw'.",
"'rethrow', 'return' or 'throw'.",
"Try adding one of the required statements.");

/**
Expand Down
10 changes: 7 additions & 3 deletions pkg/analyzer/lib/src/generated/error_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2596,7 +2596,10 @@ class ErrorVerifier extends RecursiveAstVisitor<Object> {
}
// no other switch member after this one
} else {
Statement statement = statements[statements.length - 1];
Statement statement = statements.last;
if (statement is Block && statement.statements.isNotEmpty) {
statement = statement.statements.last;
}
// terminated with statement
if (statement is BreakStatement ||
statement is ContinueStatement ||
Expand All @@ -2606,7 +2609,8 @@ class ErrorVerifier extends RecursiveAstVisitor<Object> {
// terminated with 'throw' expression
if (statement is ExpressionStatement) {
Expression expression = statement.expression;
if (expression is ThrowExpression) {
if (expression is ThrowExpression ||
expression is RethrowExpression) {
return;
}
}
Expand All @@ -2618,7 +2622,7 @@ class ErrorVerifier extends RecursiveAstVisitor<Object> {

/**
* Verify that the switch cases in the given switch [statement] are terminated
* with 'break', 'continue', 'return' or 'throw'.
* with 'break', 'continue', 'rethrow', 'return' or 'throw'.
*
* See [StaticWarningCode.CASE_BLOCK_NOT_TERMINATED].
*/
Expand Down
4 changes: 0 additions & 4 deletions tests/language/language_analyzer2.status
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
# BSD-style license that can be found in the LICENSE file.

[ $compiler == dart2analyzer ]
# Issue #27664
switch_case_warn_test/none: StaticWarning
switch_case_warn_test/retnon: StaticWarning
switch_case_warn_test/retval: StaticWarning

regress_26668_test: Fail # Issue 26678
regress_27617_test/1: MissingCompileTimeError
Expand Down
12 changes: 8 additions & 4 deletions tests/language/switch_case_warn_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
void testSwitch(int x) {
// Catch all control flow leaving the switch.
// Run switch in catch clause to check rethrow.
try {
TRY: try {
throw x;
} catch (x) {
// Add loop as break/continue target.
Expand Down Expand Up @@ -92,14 +92,16 @@ void testSwitch(int x) {
} while (false);
} finally {
// Catch all control flow leaving the switch and ignore it.
return;
// Use break instead of return to avoid warning for `return` and `return e`
// in same function.
break TRY;
}
}

// All these switch cases should cause warnings.
void testSwitchWarn(x) {
// Catch all control flow from the switch and ignore it.
try {
TRY: try {
throw 0;
} catch (e) {
// Wrap in loop as target for continue/break.
Expand Down Expand Up @@ -183,7 +185,9 @@ void testSwitchWarn(x) {
} while (false);
} finally {
// Catch all control flow leaving the switch and ignore it.
return;
// Use break instead of return to avoid warning for `return` and `return e`
// in same function.
break TRY;
}
}

Expand Down

0 comments on commit 1322877

Please sign in to comment.