Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 Loading... | |
| 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, maybe we should _not_ report an error. | |
| 2072 if (allowMandated && i == size - 2) { | |
| 2073 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
| |
| 2074 nextStatement is ContinueStatement || | |
| 2075 nextStatement is ReturnStatement || | |
| 2076 nextStatement is ThrowExpression) { | |
| 2077 return; | |
| 2078 } | |
| 2079 } | |
| 2066 int offset = nextStatement.offset; | 2080 int offset = nextStatement.offset; |
| 2067 int length = lastStatement.end - offset; | 2081 int length = lastStatement.end - offset; |
| 2068 _errorReporter.reportErrorForOffset(HintCode.DEAD_CODE, offset, length); | 2082 _errorReporter.reportErrorForOffset(HintCode.DEAD_CODE, offset, length); |
| 2069 return; | 2083 return; |
| 2070 } | 2084 } |
| 2071 } | 2085 } |
| 2072 } | 2086 } |
| 2073 | 2087 |
| 2074 /** | 2088 /** |
| 2075 * Given some [Expression], this method returns [ValidResult.RESULT_TRUE] if i t is | 2089 * Given some [Expression], this method returns [ValidResult.RESULT_TRUE] if i t is |
| (...skipping 8866 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 10942 return null; | 10956 return null; |
| 10943 } | 10957 } |
| 10944 if (identical(node.staticElement, variable)) { | 10958 if (identical(node.staticElement, variable)) { |
| 10945 if (node.inSetterContext()) { | 10959 if (node.inSetterContext()) { |
| 10946 result = true; | 10960 result = true; |
| 10947 } | 10961 } |
| 10948 } | 10962 } |
| 10949 return null; | 10963 return null; |
| 10950 } | 10964 } |
| 10951 } | 10965 } |
| OLD | NEW |