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

Side by Side 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: Two tests 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 unified diff | Download patch
« no previous file with comments | « no previous file | pkg/analyzer/test/generated/hint_code_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library analyzer.src.generated.resolver; 5 library analyzer.src.generated.resolver;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 8
9 import 'package:analyzer/dart/ast/ast.dart'; 9 import 'package:analyzer/dart/ast/ast.dart';
10 import 'package:analyzer/dart/ast/token.dart'; 10 import 'package:analyzer/dart/ast/token.dart';
(...skipping 1895 matching lines...) Expand 10 before | Expand all | Expand 10 after
1906 for (Combinator combinator in node.combinators) { 1906 for (Combinator combinator in node.combinators) {
1907 _checkCombinator(library, combinator); 1907 _checkCombinator(library, combinator);
1908 } 1908 }
1909 } 1909 }
1910 } 1910 }
1911 return super.visitImportDirective(node); 1911 return super.visitImportDirective(node);
1912 } 1912 }
1913 1913
1914 @override 1914 @override
1915 Object visitSwitchCase(SwitchCase node) { 1915 Object visitSwitchCase(SwitchCase node) {
1916 _checkForDeadStatementsInNodeList(node.statements); 1916 _checkForDeadStatementsInNodeList(node.statements, allowMandated: true);
1917 return super.visitSwitchCase(node); 1917 return super.visitSwitchCase(node);
1918 } 1918 }
1919 1919
1920 @override 1920 @override
1921 Object visitSwitchDefault(SwitchDefault node) { 1921 Object visitSwitchDefault(SwitchDefault node) {
1922 _checkForDeadStatementsInNodeList(node.statements); 1922 _checkForDeadStatementsInNodeList(node.statements, allowMandated: true);
1923 return super.visitSwitchDefault(node); 1923 return super.visitSwitchDefault(node);
1924 } 1924 }
1925 1925
1926 @override 1926 @override
1927 Object visitTryStatement(TryStatement node) { 1927 Object visitTryStatement(TryStatement node) {
1928 node.body?.accept(this); 1928 node.body?.accept(this);
1929 node.finallyBlock?.accept(this); 1929 node.finallyBlock?.accept(this);
1930 NodeList<CatchClause> catchClauses = node.catchClauses; 1930 NodeList<CatchClause> catchClauses = node.catchClauses;
1931 int numOfCatchClauses = catchClauses.length; 1931 int numOfCatchClauses = catchClauses.length;
1932 List<DartType> visitedTypes = new List<DartType>(); 1932 List<DartType> visitedTypes = new List<DartType>();
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
2038 .reportErrorForNode(hintCode, name, [library.identifier, nameStr]); 2038 .reportErrorForNode(hintCode, name, [library.identifier, nameStr]);
2039 } 2039 }
2040 } 2040 }
2041 } 2041 }
2042 2042
2043 /** 2043 /**
2044 * Given some [NodeList] of [Statement]s, from either a [Block] or 2044 * Given some [NodeList] of [Statement]s, from either a [Block] or
2045 * [SwitchMember], this loops through the list searching for dead statements. 2045 * [SwitchMember], this loops through the list searching for dead statements.
2046 * 2046 *
2047 * @param statements some ordered list of statements in a [Block] or [SwitchMe mber] 2047 * @param statements some ordered list of statements in a [Block] or [SwitchMe mber]
2048 * @param allowMandated allow dead statements mandated by the language spec.
2049 * This allows for a final break, continue, return, or throw statem ent
2050 * at the end of a switch case, that are mandated by the language s pec.
2048 */ 2051 */
2049 void _checkForDeadStatementsInNodeList(NodeList<Statement> statements) { 2052 void _checkForDeadStatementsInNodeList(
2053 NodeList<Statement> statements, {bool allowMandated: false}) {
2050 bool statementExits(Statement statement) { 2054 bool statementExits(Statement statement) {
2051 if (statement is BreakStatement) { 2055 if (statement is BreakStatement) {
2052 return statement.label == null; 2056 return statement.label == null;
2053 } else if (statement is ContinueStatement) { 2057 } else if (statement is ContinueStatement) {
2054 return statement.label == null; 2058 return statement.label == null;
2055 } 2059 }
2056 return ExitDetector.exits(statement); 2060 return ExitDetector.exits(statement);
2057 } 2061 }
2058 2062
2059 int size = statements.length; 2063 int size = statements.length;
2060 for (int i = 0; i < size; i++) { 2064 for (int i = 0; i < size; i++) {
2061 Statement currentStatement = statements[i]; 2065 Statement currentStatement = statements[i];
2062 currentStatement?.accept(this); 2066 currentStatement?.accept(this);
2063 if (statementExits(currentStatement) && i != size - 1) { 2067 if (statementExits(currentStatement) && i != size - 1) {
2064 Statement nextStatement = statements[i + 1]; 2068 Statement nextStatement = statements[i + 1];
2065 Statement lastStatement = statements[size - 1]; 2069 Statement lastStatement = statements[size - 1];
2070 // If mandated statements are allowed, and only the last statement is
2071 // dead, and it's a BreakStatement, then assume it is a statement
2072 // mandated by the language spec, there to avoid a
2073 // CASE_BLOCK_NOT_TERMINATED error.
2074 if (allowMandated && i == size - 2 && nextStatement is BreakStatement) {
2075 return;
2076 }
2066 int offset = nextStatement.offset; 2077 int offset = nextStatement.offset;
2067 int length = lastStatement.end - offset; 2078 int length = lastStatement.end - offset;
2068 _errorReporter.reportErrorForOffset(HintCode.DEAD_CODE, offset, length); 2079 _errorReporter.reportErrorForOffset(HintCode.DEAD_CODE, offset, length);
2069 return; 2080 return;
2070 } 2081 }
2071 } 2082 }
2072 } 2083 }
2073 2084
2074 /** 2085 /**
2075 * Given some [Expression], this method returns [ValidResult.RESULT_TRUE] if i t is 2086 * Given some [Expression], this method returns [ValidResult.RESULT_TRUE] if i t is
(...skipping 8863 matching lines...) Expand 10 before | Expand all | Expand 10 after
10939 return null; 10950 return null;
10940 } 10951 }
10941 if (identical(node.staticElement, variable)) { 10952 if (identical(node.staticElement, variable)) {
10942 if (node.inSetterContext()) { 10953 if (node.inSetterContext()) {
10943 result = true; 10954 result = true;
10944 } 10955 }
10945 } 10956 }
10946 return null; 10957 return null;
10947 } 10958 }
10948 } 10959 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/test/generated/hint_code_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698