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

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

Issue 1020243004: Report separate UNUSED_CATCH_CLAUSE and UNUSED_CATCH_STACK hints. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: tweak Created 5 years, 9 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 | Annotate | Revision Log
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 engine.resolver; 5 library engine.resolver;
6 6
7 import "dart:math" as math; 7 import "dart:math" as math;
8 import 'dart:collection'; 8 import 'dart:collection';
9 9
10 import 'package:analyzer/src/generated/utilities_collection.dart'; 10 import 'package:analyzer/src/generated/utilities_collection.dart';
(...skipping 14938 matching lines...) Expand 10 before | Expand all | Expand 10 after
14949 14949
14950 final LibraryElement _enclosingLibrary; 14950 final LibraryElement _enclosingLibrary;
14951 ClassElement _enclosingClass; 14951 ClassElement _enclosingClass;
14952 ExecutableElement _enclosingExec; 14952 ExecutableElement _enclosingExec;
14953 14953
14954 _GatherUsedElementsVisitor(this._enclosingLibrary); 14954 _GatherUsedElementsVisitor(this._enclosingLibrary);
14955 14955
14956 @override 14956 @override
14957 visitCatchClause(CatchClause node) { 14957 visitCatchClause(CatchClause node) {
14958 SimpleIdentifier exceptionParameter = node.exceptionParameter; 14958 SimpleIdentifier exceptionParameter = node.exceptionParameter;
14959 _useStaticElement(exceptionParameter); 14959 SimpleIdentifier stackTraceParameter = node.stackTraceParameter;
14960 if (exceptionParameter != null) {
14961 Element element = exceptionParameter.staticElement;
14962 usedElements.addCatchException(element);
14963 if (stackTraceParameter != null || node.onKeyword == null) {
14964 _useElement(element);
14965 }
14966 }
14967 if (stackTraceParameter != null) {
14968 Element element = stackTraceParameter.staticElement;
14969 usedElements.addCatchStackTrace(element);
14970 }
14960 super.visitCatchClause(node); 14971 super.visitCatchClause(node);
14961 } 14972 }
14962 14973
14963 @override 14974 @override
14964 visitClassDeclaration(ClassDeclaration node) { 14975 visitClassDeclaration(ClassDeclaration node) {
14965 ClassElement enclosingClassOld = _enclosingClass; 14976 ClassElement enclosingClassOld = _enclosingClass;
14966 try { 14977 try {
14967 _enclosingClass = node.element; 14978 _enclosingClass = node.element;
14968 super.visitClassDeclaration(node); 14979 super.visitClassDeclaration(node);
14969 } finally { 14980 } finally {
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
15078 return; 15089 return;
15079 } 15090 }
15080 if (parent2 is VariableDeclarationList) { 15091 if (parent2 is VariableDeclarationList) {
15081 return; 15092 return;
15082 } 15093 }
15083 } 15094 }
15084 // OK 15095 // OK
15085 _useElement(element); 15096 _useElement(element);
15086 } 15097 }
15087 15098
15088 void _useStaticElement(SimpleIdentifier identifier) {
15089 if (identifier != null) {
15090 _useElement(identifier.staticElement);
15091 }
15092 }
15093
15094 static bool _isReadIdentifier(SimpleIdentifier node) { 15099 static bool _isReadIdentifier(SimpleIdentifier node) {
15095 // not reading at all 15100 // not reading at all
15096 if (!node.inGetterContext()) { 15101 if (!node.inGetterContext()) {
15097 return false; 15102 return false;
15098 } 15103 }
15099 // check if useless reading 15104 // check if useless reading
15100 AstNode parent = node.parent; 15105 AstNode parent = node.parent;
15101 if (parent.parent is ExpressionStatement && 15106 if (parent.parent is ExpressionStatement &&
15102 (parent is PrefixExpression || 15107 (parent is PrefixExpression ||
15103 parent is PostfixExpression || 15108 parent is PostfixExpression ||
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
15250 element.kind.displayName, 15255 element.kind.displayName,
15251 element.displayName 15256 element.displayName
15252 ]); 15257 ]);
15253 } 15258 }
15254 super.visitFunctionElement(element); 15259 super.visitFunctionElement(element);
15255 } 15260 }
15256 15261
15257 @override 15262 @override
15258 visitLocalVariableElement(LocalVariableElement element) { 15263 visitLocalVariableElement(LocalVariableElement element) {
15259 if (!_isUsedElement(element)) { 15264 if (!_isUsedElement(element)) {
15260 _reportErrorForElement( 15265 HintCode errorCode;
15261 HintCode.UNUSED_LOCAL_VARIABLE, element, [element.displayName]); 15266 if (_usedElements.isCatchException(element)) {
15267 errorCode = HintCode.UNUSED_CATCH_CLAUSE;
15268 } else if (_usedElements.isCatchStackTrace(element)) {
15269 errorCode = HintCode.UNUSED_CATCH_STACK;
15270 } else {
15271 errorCode = HintCode.UNUSED_LOCAL_VARIABLE;
15272 }
15273 _reportErrorForElement(errorCode, element, [element.displayName]);
15262 } 15274 }
15263 } 15275 }
15264 15276
15265 @override 15277 @override
15266 visitMethodElement(MethodElement element) { 15278 visitMethodElement(MethodElement element) {
15267 if (!_isUsedMember(element)) { 15279 if (!_isUsedMember(element)) {
15268 _reportErrorForElement(HintCode.UNUSED_ELEMENT, element, [ 15280 _reportErrorForElement(HintCode.UNUSED_ELEMENT, element, [
15269 element.kind.displayName, 15281 element.kind.displayName,
15270 element.displayName 15282 element.displayName
15271 ]); 15283 ]);
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
15333 } 15345 }
15334 15346
15335 class _UsedElements { 15347 class _UsedElements {
15336 /** 15348 /**
15337 * Resolved, locally defined elements that are used or potentially can be 15349 * Resolved, locally defined elements that are used or potentially can be
15338 * used. 15350 * used.
15339 */ 15351 */
15340 final HashSet<Element> elements = new HashSet<Element>(); 15352 final HashSet<Element> elements = new HashSet<Element>();
15341 15353
15342 /** 15354 /**
15355 * [LocalVariableElement]s that represent exceptions in [CatchClause]s.
15356 */
15357 final HashSet<LocalVariableElement> catchExceptionElements =
15358 new HashSet<LocalVariableElement>();
15359
15360 /**
15361 * [LocalVariableElement]s that represent stack traces in [CatchClause]s.
15362 */
15363 final HashSet<LocalVariableElement> catchStackTraceElements =
15364 new HashSet<LocalVariableElement>();
15365
15366 /**
15343 * Names of resolved or unresolved class members that are referenced in the 15367 * Names of resolved or unresolved class members that are referenced in the
15344 * library. 15368 * library.
15345 */ 15369 */
15346 final HashSet<String> members = new HashSet<String>(); 15370 final HashSet<String> members = new HashSet<String>();
15347 15371
15348 /** 15372 /**
15349 * Names of resolved or unresolved class members that are read in the 15373 * Names of resolved or unresolved class members that are read in the
15350 * library. 15374 * library.
15351 */ 15375 */
15352 final HashSet<String> readMembers = new HashSet<String>(); 15376 final HashSet<String> readMembers = new HashSet<String>();
15377
15378 void addCatchException(LocalVariableElement element) {
15379 if (element != null) {
15380 catchExceptionElements.add(element);
15381 }
15382 }
15383
15384 void addCatchStackTrace(LocalVariableElement element) {
15385 if (element != null) {
15386 catchStackTraceElements.add(element);
15387 }
15388 }
15389
15390 bool isCatchException(LocalVariableElement element) {
15391 return catchExceptionElements.contains(element);
15392 }
15393
15394 bool isCatchStackTrace(LocalVariableElement element) {
15395 return catchStackTraceElements.contains(element);
15396 }
15353 } 15397 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/generated/error.dart ('k') | pkg/analyzer/test/generated/resolver_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698