Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(282)

Unified Diff: pkg/analyzer/lib/src/generated/resolver.dart

Issue 2064203002: Do not error on dead, mandated statements at end of switch cases (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/analyzer/lib/src/generated/resolver.dart
diff --git a/pkg/analyzer/lib/src/generated/resolver.dart b/pkg/analyzer/lib/src/generated/resolver.dart
index ca02438d7ee4fb47d33baf18f7a934a0e4910bd9..a33bcf8442005fc704a431d30ba8dffb3a495449 100644
--- a/pkg/analyzer/lib/src/generated/resolver.dart
+++ b/pkg/analyzer/lib/src/generated/resolver.dart
@@ -1913,13 +1913,13 @@ class DeadCodeVerifier extends RecursiveAstVisitor<Object> {
@override
Object visitSwitchCase(SwitchCase node) {
- _checkForDeadStatementsInNodeList(node.statements);
+ _checkForDeadStatementsInNodeList(node.statements, allowMandated: true);
return super.visitSwitchCase(node);
}
@override
Object visitSwitchDefault(SwitchDefault node) {
- _checkForDeadStatementsInNodeList(node.statements);
+ _checkForDeadStatementsInNodeList(node.statements, allowMandated: true);
return super.visitSwitchDefault(node);
}
@@ -2045,8 +2045,12 @@ class DeadCodeVerifier extends RecursiveAstVisitor<Object> {
* [SwitchMember], this loops through the list searching for dead statements.
*
* @param statements some ordered list of statements in a [Block] or [SwitchMember]
+ * @param allowMandated allow dead statements mandated by the language spec.
+ * This allows for a final break, continue, return, or throw statement
+ * at the end of a switch case, that are mandated by the language spec.
*/
- void _checkForDeadStatementsInNodeList(NodeList<Statement> statements) {
+ void _checkForDeadStatementsInNodeList(
+ NodeList<Statement> statements, {bool allowMandated: false}) {
bool statementExits(Statement statement) {
if (statement is BreakStatement) {
return statement.label == null;
@@ -2063,6 +2067,16 @@ class DeadCodeVerifier extends RecursiveAstVisitor<Object> {
if (statementExits(currentStatement) && i != size - 1) {
Statement nextStatement = statements[i + 1];
Statement lastStatement = statements[size - 1];
+ // If mandated statements are allowed, and only the last statement is
+ // dead, maybe we should _not_ report an error.
+ if (allowMandated && i == size - 2) {
+ if (nextStatement is BreakStatement ||
Paul Berry 2016/06/14 18:39:27 IMHO this is overly permissive. The important cas
Brian Wilkerson 2016/06/14 18:59:41 I could buy that; we're essentially leaving a sing
srawlins 2016/06/14 21:09:55 Yeah... I think at this point we just won't be sma
srawlins 2016/06/14 21:09:55 Ah, good point. I had not thought this through all
+ nextStatement is ContinueStatement ||
+ nextStatement is ReturnStatement ||
+ nextStatement is ThrowExpression) {
+ return;
+ }
+ }
int offset = nextStatement.offset;
int length = lastStatement.end - offset;
_errorReporter.reportErrorForOffset(HintCode.DEAD_CODE, offset, length);

Powered by Google App Engine
This is Rietveld 408576698