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

Side by Side Diff: pkg/analyzer/lib/src/generated/resolver.dart

Issue 1943443002: If an if or do statement always exits, following statements are dead. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Add tests Created 4 years, 7 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
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 2042 matching lines...) Expand 10 before | Expand all | Expand 10 after
2053 } 2053 }
2054 if (element == null) { 2054 if (element == null) {
2055 _errorReporter 2055 _errorReporter
2056 .reportErrorForNode(hintCode, name, [library.identifier, nameStr]); 2056 .reportErrorForNode(hintCode, name, [library.identifier, nameStr]);
2057 } 2057 }
2058 } 2058 }
2059 } 2059 }
2060 2060
2061 /** 2061 /**
2062 * Given some [NodeList] of [Statement]s, from either a [Block] or 2062 * Given some [NodeList] of [Statement]s, from either a [Block] or
2063 * [SwitchMember], this loops through the list in reverse order searching for statements 2063 * [SwitchMember], this loops through the list searching for dead statements.
2064 * after a return, unlabeled break or unlabeled continue statement to mark the m as dead code. 2064 *
2065 * Dead statements are found after a return, an unlabeled break, an unlabeled
2066 * continue, (A THROW?), or an if statement that unconditionally exits.
2065 * 2067 *
2066 * @param statements some ordered list of statements in a [Block] or [SwitchMe mber] 2068 * @param statements some ordered list of statements in a [Block] or [SwitchMe mber]
2067 */ 2069 */
2068 void _checkForDeadStatementsInNodeList(NodeList<Statement> statements) { 2070 void _checkForDeadStatementsInNodeList(NodeList<Statement> statements) {
2071 bool statementExits(Statement statement) {
Brian Wilkerson 2016/05/04 17:28:19 With the exception of the handling of the break an
srawlins 2016/05/19 18:36:44 Done! Dramatically simpler!
2072 if (statement is ReturnStatement) {
2073 return true;
2074 }
2075 if ((statement is BreakStatement && statement.label == null) ||
2076 (statement is ContinueStatement && statement.label == null)) {
2077 return true;
2078 }
2079 if (statement is IfStatement) {
2080 Statement thenStatement = statement.thenStatement;
2081 Statement elseStatement = statement.elseStatement;
2082 if (elseStatement != null) {
2083 if (ExitDetector.exits(thenStatement) &&
2084 ExitDetector.exits(elseStatement)) {
2085 return true;
2086 }
2087 }
2088 } else if (statement is DoStatement) {
2089 if (statement.body != null && ExitDetector.exits(statement.body)) {
2090 return true;
2091 }
2092 }
2093 return false;
2094 }
2095
2069 int size = statements.length; 2096 int size = statements.length;
2070 for (int i = 0; i < size; i++) { 2097 for (int i = 0; i < size; i++) {
2071 Statement currentStatement = statements[i]; 2098 Statement currentStatement = statements[i];
2072 currentStatement?.accept(this); 2099 currentStatement?.accept(this);
2073 bool returnOrBreakingStatement = currentStatement is ReturnStatement || 2100 if (statementExits(currentStatement) && i != size - 1) {
2074 (currentStatement is BreakStatement &&
2075 currentStatement.label == null) ||
2076 (currentStatement is ContinueStatement &&
2077 currentStatement.label == null);
2078 if (returnOrBreakingStatement && i != size - 1) {
2079 Statement nextStatement = statements[i + 1]; 2101 Statement nextStatement = statements[i + 1];
2080 Statement lastStatement = statements[size - 1]; 2102 Statement lastStatement = statements[size - 1];
2081 int offset = nextStatement.offset; 2103 int offset = nextStatement.offset;
2082 int length = lastStatement.end - offset; 2104 int length = lastStatement.end - offset;
2083 _errorReporter.reportErrorForOffset(HintCode.DEAD_CODE, offset, length); 2105 _errorReporter.reportErrorForOffset(HintCode.DEAD_CODE, offset, length);
2084 return; 2106 return;
2085 } 2107 }
2086 } 2108 }
2087 } 2109 }
2088 2110
(...skipping 8794 matching lines...) Expand 10 before | Expand all | Expand 10 after
10883 return null; 10905 return null;
10884 } 10906 }
10885 if (identical(node.staticElement, variable)) { 10907 if (identical(node.staticElement, variable)) {
10886 if (node.inSetterContext()) { 10908 if (node.inSetterContext()) {
10887 result = true; 10909 result = true;
10888 } 10910 }
10889 } 10911 }
10890 return null; 10912 return null;
10891 } 10913 }
10892 } 10914 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/test/generated/hint_code_test.dart » ('j') | pkg/analyzer/test/generated/hint_code_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698