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

Unified Diff: pkg/analyzer/test/generated/resolver_test.dart

Issue 686113007: Report HintCode.UNUSED_LOCAL_VARIABLE for local variables whose value is never used. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 1 month 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
« no previous file with comments | « pkg/analyzer/lib/src/generated/resolver.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analyzer/test/generated/resolver_test.dart
diff --git a/pkg/analyzer/test/generated/resolver_test.dart b/pkg/analyzer/test/generated/resolver_test.dart
index e05c4d0ab7d0da746eebf4e8d0eadd88f85eaecb..78830ab3fc7c1a358cb96293083aa0fa27705d1d 100644
--- a/pkg/analyzer/test/generated/resolver_test.dart
+++ b/pkg/analyzer/test/generated/resolver_test.dart
@@ -3206,6 +3206,137 @@ class B {}''');
verify([source, source2]);
}
+ void test_unusedLocalVariable() {
+ enableUnusedLocalVariable = true;
+ Source source = addSource(r'''
+main() {
+ var v = 1;
+ v = 2;
+}''');
+ resolve(source);
+ assertErrors(source, [HintCode.UNUSED_LOCAL_VARIABLE]);
+ verify([source]);
+ }
+
+ void test_unusedLocalVariable_isRead_notUsed_compoundAssign() {
+ enableUnusedLocalVariable = true;
+ Source source = addSource(r'''
+main() {
+ var v = 1;
+ v += 2;
+}''');
+ resolve(source);
+ assertErrors(source, [HintCode.UNUSED_LOCAL_VARIABLE]);
+ verify([source]);
+ }
+
+ void test_unusedLocalVariable_isRead_notUsed_postfixExpr() {
+ enableUnusedLocalVariable = true;
+ Source source = addSource(r'''
+main() {
+ var v = 1;
+ v++;
+}''');
+ resolve(source);
+ assertErrors(source, [HintCode.UNUSED_LOCAL_VARIABLE]);
+ verify([source]);
+ }
+
+ void test_unusedLocalVariable_isRead_notUsed_prefixExpr() {
+ enableUnusedLocalVariable = true;
+ Source source = addSource(r'''
+main() {
+ var v = 1;
+ ++v;
+}''');
+ resolve(source);
+ assertErrors(source, [HintCode.UNUSED_LOCAL_VARIABLE]);
+ verify([source]);
+ }
+
+ void test_unusedLocalVariable_isRead_usedArgument() {
+ enableUnusedLocalVariable = true;
+ Source source = addSource(r'''
+main() {
+ var v = 1;
+ print(++v);
+}
+print(x) {}''');
+ resolve(source);
+ assertErrors(source, []);
+ verify([source]);
+ }
+
+ void test_unusedLocalVariable_isRead_usedInvocationTarget() {
+ enableUnusedLocalVariable = true;
+ Source source = addSource(r'''
+class A {
+ foo() {}
+}
+main() {
+ var a = new A();
+ a.foo();
+}
+''');
+ resolve(source);
+ assertErrors(source, []);
+ verify([source]);
+ }
+
+ void test_unusedLocalVariable_isInvoked() {
+ enableUnusedLocalVariable = true;
+ Source source = addSource(r'''
+typedef Foo();
+main() {
+ Foo foo;
+ foo();
+}''');
+ resolve(source);
+ assertErrors(source, []);
+ verify([source]);
+ }
+
+ void test_unusedLocalVariable_inCatch_exception() {
+ enableUnusedLocalVariable = true;
+ Source source = addSource(r'''
+main() {
+ try {
+ } catch (exception) {
+ }
+}''');
+ resolve(source);
+ assertErrors(source, []);
+ verify([source]);
+ }
+
+ void test_unusedLocalVariable_inCatch_stackTrace() {
+ enableUnusedLocalVariable = true;
+ Source source = addSource(r'''
+main() {
+ try {
+ } catch (exception, stackTrace) {
+ }
+}''');
+ resolve(source);
+ assertErrors(source, [HintCode.UNUSED_LOCAL_VARIABLE]);
+ verify([source]);
+ }
+
+ void test_unusedLocalVariable_inCatch_stackTrace_used() {
+ enableUnusedLocalVariable = true;
+ Source source = addSource(r'''
+main() {
+ try {
+ } catch (exception, stackTrace) {
+ print('exceptino at $stackTrace');
+ }
+}
+print(x) {}''');
+ resolve(source);
+ assertErrors(source, []);
+ verify([source]);
+ }
+
void test_useOfVoidResult_assignmentExpression_function() {
Source source = addSource(r'''
void f() {}
@@ -6200,6 +6331,11 @@ class ResolverTestCase extends EngineTestCase {
*/
AnalysisContextImpl analysisContext2;
+ /**
+ * Specifies if [assertErrors] should check for [HintCode.UNUSED_LOCAL_VARIABLE].
+ */
+ bool enableUnusedLocalVariable = false;
+
@override
void setUp() {
reset();
@@ -6242,6 +6378,10 @@ class ResolverTestCase extends EngineTestCase {
void assertErrors(Source source, List<ErrorCode> expectedErrorCodes) {
GatheringErrorListener errorListener = new GatheringErrorListener();
for (AnalysisError error in analysisContext2.computeErrors(source)) {
+ if (error.errorCode == HintCode.UNUSED_LOCAL_VARIABLE &&
+ !enableUnusedLocalVariable) {
+ continue;
+ }
errorListener.onError(error);
}
errorListener.assertErrorsWithCodes(expectedErrorCodes);
« no previous file with comments | « pkg/analyzer/lib/src/generated/resolver.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698