Adapt analyzer to new warnings around switch-case fallthrough.

Fixes issue #27664.
BUG= http://dartbug.com/27664
R=brianwilkerson@google.com

Review URL: https://codereview.chromium.org/2446093003 .
This commit is contained in:
Lasse R.H. Nielsen 2016-10-26 10:51:50 +02:00
parent 103881d01c
commit 1322877f5b
4 changed files with 18 additions and 14 deletions

View file

@ -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.");
/**

View file

@ -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 ||
@ -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;
}
}
@ -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].
*/

View file

@ -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

View file

@ -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.
@ -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.
@ -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;
}
}