Split out several more hint tests

Change-Id: I6483656ca2ac9c7cfa00249cf37ccbb78758ffb6
Reviewed-on: https://dart-review.googlesource.com/c/92533
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Brian Wilkerson 2019-02-10 18:51:22 +00:00 committed by commit-bot@chromium.org
parent a49f7f1d44
commit 0a2672453f
20 changed files with 1913 additions and 2004 deletions

File diff suppressed because it is too large Load diff

View file

@ -765,21 +765,6 @@ class A {}''');
assertNoErrors(source);
}
test_undefinedGetter_inSubtype() async {
Source source = addSource(r'''
class A {}
class B extends A {
get b => 0;
}
f(var a) {
if(a is A) {
return a.b;
}
}''');
await computeAnalysisResult(source);
assertErrors(source, [StaticTypeWarningCode.UNDEFINED_GETTER]);
}
test_undefinedMethod_assignmentExpression_inSubtype() async {
Source source = addSource(r'''
class A {}
@ -844,175 +829,6 @@ f(A a, B b) {
await computeAnalysisResult(source);
assertNoErrors(source);
}
test_undefinedOperator_binaryExpression_inSubtype() async {
Source source = addSource(r'''
class A {}
class B extends A {
operator +(B b) {}
}
f(var a) {
if(a is A) {
a + 1;
}
}''');
await computeAnalysisResult(source);
assertErrors(source, [StaticTypeWarningCode.UNDEFINED_OPERATOR]);
}
test_undefinedOperator_indexBoth_inSubtype() async {
Source source = addSource(r'''
class A {}
class B extends A {
operator [](int index) {}
}
f(var a) {
if(a is A) {
a[0]++;
}
}''');
await computeAnalysisResult(source);
assertErrors(source, [
StaticTypeWarningCode.UNDEFINED_OPERATOR,
StaticTypeWarningCode.UNDEFINED_OPERATOR,
]);
}
test_undefinedOperator_indexGetter_inSubtype() async {
Source source = addSource(r'''
class A {}
class B extends A {
operator [](int index) {}
}
f(var a) {
if(a is A) {
a[0];
}
}''');
await computeAnalysisResult(source);
assertErrors(source, [StaticTypeWarningCode.UNDEFINED_OPERATOR]);
}
test_undefinedOperator_indexSetter_inSubtype() async {
Source source = addSource(r'''
class A {}
class B extends A {
operator []=(i, v) {}
}
f(var a) {
if(a is A) {
a[0] = 1;
}
}''');
await computeAnalysisResult(source);
assertErrors(source, [StaticTypeWarningCode.UNDEFINED_OPERATOR]);
}
test_undefinedOperator_postfixExpression() async {
Source source = addSource(r'''
class A {}
class B extends A {
operator +(B b) {return new B();}
}
f(var a) {
if(a is A) {
a++;
}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
}
test_undefinedOperator_prefixExpression() async {
Source source = addSource(r'''
class A {}
class B extends A {
operator +(B b) {return new B();}
}
f(var a) {
if(a is A) {
++a;
}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
}
test_undefinedSetter_inSubtype() async {
Source source = addSource(r'''
class A {}
class B extends A {
set b(x) {}
}
f(var a) {
if (a is A) {
a.b = 0;
}
}''');
await computeAnalysisResult(source);
assertErrors(source, [StaticTypeWarningCode.UNDEFINED_SETTER]);
}
test_unnecessaryNoSuchMethod_blockBody_notReturnStatement() async {
Source source = addSource(r'''
class A {
noSuchMethod(x) => super.noSuchMethod(x);
}
class B extends A {
mmm();
noSuchMethod(y) {
print(y);
}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_unnecessaryNoSuchMethod_blockBody_notSingleStatement() async {
Source source = addSource(r'''
class A {
noSuchMethod(x) => super.noSuchMethod(x);
}
class B extends A {
mmm();
noSuchMethod(y) {
print(y);
return super.noSuchMethod(y);
}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_unnecessaryNoSuchMethod_expressionBody_notNoSuchMethod() async {
Source source = addSource(r'''
class A {
noSuchMethod(x) => super.noSuchMethod(x);
}
class B extends A {
mmm();
noSuchMethod(y) => super.hashCode;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_unnecessaryNoSuchMethod_expressionBody_notSuper() async {
Source source = addSource(r'''
class A {
noSuchMethod(x) => super.noSuchMethod(x);
}
class B extends A {
mmm();
noSuchMethod(y) => 42;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
}
class PubSuggestionCodeTest extends ResolverTestCase {

View file

@ -144,6 +144,10 @@ mixin ResolutionTest implements ResourceProviderMixin {
expect(element.enclosingElement, expectedEnclosing);
}
bool get enableUnusedLocalVariable => false;
bool get enableUnusedElement => false;
/**
* Assert that the number of error codes in reported [errors] matches the
* number of [expected] error codes. The order of errors is ignored.
@ -153,11 +157,15 @@ mixin ResolutionTest implements ResourceProviderMixin {
var errorListener = new GatheringErrorListener();
for (AnalysisError error in result.errors) {
ErrorCode errorCode = error.errorCode;
if (errorCode == HintCode.UNUSED_CATCH_CLAUSE ||
errorCode == HintCode.UNUSED_CATCH_STACK ||
errorCode == HintCode.UNUSED_ELEMENT ||
errorCode == HintCode.UNUSED_FIELD ||
errorCode == HintCode.UNUSED_LOCAL_VARIABLE) {
if (!enableUnusedElement &&
(errorCode == HintCode.UNUSED_ELEMENT ||
errorCode == HintCode.UNUSED_FIELD)) {
continue;
}
if (!enableUnusedLocalVariable &&
(errorCode == HintCode.UNUSED_CATCH_CLAUSE ||
errorCode == HintCode.UNUSED_CATCH_STACK ||
errorCode == HintCode.UNUSED_LOCAL_VARIABLE)) {
continue;
}
errorListener.onError(error);

View file

@ -15,11 +15,26 @@ import 'invalid_override_different_default_values_named_test.dart'
import 'invalid_override_different_default_values_positional_test.dart'
as invalid_override_different_default_values_positional;
import 'invalid_required_param_test.dart' as invalid_required_param;
import 'undefined_getter.dart' as undefined_getter;
import 'top_level_instance_getter_test.dart' as top_level_instance_getter;
import 'top_level_instance_method_test.dart' as top_level_instance_method;
import 'type_check_is_not_null_test.dart' as type_check_is_not_null;
import 'type_check_is_null_test.dart' as type_check_is_null;
import 'undefined_getter_test.dart' as undefined_getter;
import 'undefined_hidden_name_test.dart' as undefined_hidden_name;
import 'undefined_operator_test.dart' as undefined_operator;
import 'undefined_setter_test.dart' as undefined_setter;
import 'undefined_shown_name_test.dart' as undefined_shown_name;
import 'unnecessary_cast_test.dart' as unnecessary_cast;
import 'unnecessary_no_such_method_test.dart' as unnecessary_no_such_method;
import 'unnecessary_type_check_false_test.dart' as unnecessary_type_check_false;
import 'unnecessary_type_check_true_test.dart' as unnecessary_type_check_true;
import 'unused_catch_clause_test.dart' as unused_catch_clause;
import 'unused_catch_stack_test.dart' as unused_catch_stack;
import 'unused_element_test.dart' as unused_element;
import 'unused_field_test.dart' as unused_field;
import 'unused_import_test.dart' as unused_import;
import 'unused_label_test.dart' as unused_label;
import 'unused_local_variable_test.dart' as unused_local_variable;
import 'unused_shown_name_test.dart' as unused_shown_name;
import 'use_of_void_result_test.dart' as use_of_void_result;
@ -34,11 +49,26 @@ main() {
invalid_override_different_default_values_named.main();
invalid_override_different_default_values_positional.main();
invalid_required_param.main();
top_level_instance_getter.main();
top_level_instance_method.main();
type_check_is_not_null.main();
type_check_is_null.main();
undefined_getter.main();
undefined_hidden_name.main();
undefined_operator.main();
undefined_setter.main();
undefined_shown_name.main();
unnecessary_cast.main();
unnecessary_no_such_method.main();
unnecessary_type_check_false.main();
unnecessary_type_check_true.main();
unused_catch_clause.main();
unused_catch_stack.main();
unused_element.main();
unused_field.main();
unused_import.main();
unused_label.main();
unused_local_variable.main();
unused_shown_name.main();
use_of_void_result.main();
}, name: 'diagnostics');

View file

@ -0,0 +1,434 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/src/error/codes.dart';
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/driver_resolution.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(TopLevelInstanceGetterTest);
});
}
@reflectiveTest
class TopLevelInstanceGetterTest extends DriverResolutionTest {
test_call() async {
await assertNoErrorsInCode('''
class A {
int Function() get g => () => 0;
}
var a = new A();
var b = a.g();
''');
TopLevelVariableDeclaration b = result.unit.declarations[2];
expect(b.variables.variables[0].declaredElement.type.toString(), 'int');
}
test_field() async {
await assertNoErrorsInCode('''
class A {
int g;
}
var b = new A().g;
''');
TopLevelVariableDeclaration b = result.unit.declarations[1];
expect(b.variables.variables[0].declaredElement.type.toString(), 'int');
}
test_field_call() async {
await assertNoErrorsInCode('''
class A {
int Function() g;
}
var a = new A();
var b = a.g();
''');
TopLevelVariableDeclaration b = result.unit.declarations[2];
expect(b.variables.variables[0].declaredElement.type.toString(), 'int');
}
test_field_prefixedIdentifier() async {
await assertNoErrorsInCode('''
class A {
int g;
}
var a = new A();
var b = a.g;
''');
TopLevelVariableDeclaration b = result.unit.declarations[2];
expect(b.variables.variables[0].declaredElement.type.toString(), 'int');
}
test_getter() async {
await assertNoErrorsInCode('''
class A {
int get g => 0;
}
var b = new A().g;
''');
TopLevelVariableDeclaration b = result.unit.declarations[1];
expect(b.variables.variables[0].declaredElement.type.toString(), 'int');
}
test_implicitlyTyped() async {
await assertErrorsInCode('''
class A {
get g => 0;
}
var b = new A().g;
''', [StrongModeCode.TOP_LEVEL_INSTANCE_GETTER]);
}
test_implicitlyTyped_call() async {
await assertErrorsInCode('''
class A {
get g => () => 0;
}
var a = new A();
var b = a.g();
''', [StrongModeCode.TOP_LEVEL_INSTANCE_GETTER]);
}
test_implicitlyTyped_field() async {
await assertErrorsInCode('''
class A {
var g = 0;
}
var b = new A().g;
''', [StrongModeCode.TOP_LEVEL_INSTANCE_GETTER]);
}
test_implicitlyTyped_field_call() async {
await assertErrorsInCode('''
class A {
var g = () => 0;
}
var a = new A();
var b = a.g();
''', [StrongModeCode.TOP_LEVEL_INSTANCE_GETTER]);
}
test_implicitlyTyped_field_prefixedIdentifier() async {
await assertErrorsInCode('''
class A {
var g = 0;
}
var a = new A();
var b = a.g;
''', [StrongModeCode.TOP_LEVEL_INSTANCE_GETTER]);
}
test_implicitlyTyped_fn() async {
// The reference to a.x triggers TOP_LEVEL_INSTANCE_GETTER because f is
// generic, so the type of a.x might affect the type of b.
await assertErrorsInCode('''
class A {
var x = 0;
}
int f<T>(x) => 0;
var a = new A();
var b = f(a.x);
''', [StrongModeCode.TOP_LEVEL_INSTANCE_GETTER]);
}
test_implicitlyTyped_fn_explicit_type_params() async {
// The reference to a.x does not trigger TOP_LEVEL_INSTANCE_GETTER because
// it can't possibly affect the type of b.
await assertNoErrorsInCode('''
class A {
var x = 0;
}
int f<T>(x) => 0;
var a = new A();
var b = f<int>(a.x);
''');
}
test_implicitlyTyped_fn_not_generic() async {
// The reference to a.x does not trigger TOP_LEVEL_INSTANCE_GETTER because
// it can't possibly affect the type of b.
await assertNoErrorsInCode('''
class A {
var x = 0;
}
int f(x) => 0;
var a = new A();
var b = f(a.x);
''');
}
test_implicitlyTyped_indexExpression() async {
// The reference to a.x does not trigger TOP_LEVEL_INSTANCE_GETTER because
// it can't possibly affect the type of b.
await assertNoErrorsInCode('''
class A {
var x = 0;
int operator[](int value) => 0;
}
var a = new A();
var b = a[a.x];
''');
}
test_implicitlyTyped_invoke() async {
// The reference to a.x triggers TOP_LEVEL_INSTANCE_GETTER because the
// closure is generic, so the type of a.x might affect the type of b.
await assertErrorsInCode('''
class A {
var x = 0;
}
var a = new A();
var b = (<T>(y) => 0)(a.x);
''', [StrongModeCode.TOP_LEVEL_INSTANCE_GETTER]);
}
test_implicitlyTyped_invoke_explicit_type_params() async {
// The reference to a.x does not trigger TOP_LEVEL_INSTANCE_GETTER because
// it can't possibly affect the type of b.
await assertNoErrorsInCode('''
class A {
var x = 0;
}
var a = new A();
var b = (<T>(y) => 0)<int>(a.x);
''');
}
test_implicitlyTyped_invoke_not_generic() async {
// The reference to a.x does not trigger TOP_LEVEL_INSTANCE_GETTER because
// it can't possibly affect the type of b.
await assertNoErrorsInCode('''
class A {
var x = 0;
}
var a = new A();
var b = ((y) => 0)(a.x);
''');
}
test_implicitlyTyped_method() async {
// The reference to a.x triggers TOP_LEVEL_INSTANCE_GETTER because f is
// generic, so the type of a.x might affect the type of b.
await assertErrorsInCode('''
class A {
var x = 0;
int f<T>(int x) => 0;
}
var a = new A();
var b = a.f(a.x);
''', [StrongModeCode.TOP_LEVEL_INSTANCE_GETTER]);
}
test_implicitlyTyped_method_explicit_type_params() async {
// The reference to a.x does not trigger TOP_LEVEL_INSTANCE_GETTER because
// it can't possibly affect the type of b.
await assertNoErrorsInCode('''
class A {
var x = 0;
int f<T>(x) => 0;
}
var a = new A();
var b = a.f<int>(a.x);
''');
}
test_implicitlyTyped_method_not_generic() async {
// The reference to a.x does not trigger TOP_LEVEL_INSTANCE_GETTER because
// it can't possibly affect the type of b.
await assertNoErrorsInCode('''
class A {
var x = 0;
int f(x) => 0;
}
var a = new A();
var b = a.f(a.x);
''');
}
test_implicitlyTyped_new() async {
// The reference to a.x triggers TOP_LEVEL_INSTANCE_GETTER because B is
// generic, so the type of a.x might affect the type of b.
await assertErrorsInCode('''
class A {
var x = 0;
}
class B<T> {
B(x);
}
var a = new A();
var b = new B(a.x);
''', [StrongModeCode.TOP_LEVEL_INSTANCE_GETTER]);
}
test_implicitlyTyped_new_explicit_type_params() async {
// The reference to a.x does not trigger TOP_LEVEL_INSTANCE_GETTER because
// it can't possibly affect the type of b.
await assertNoErrorsInCode('''
class A {
var x = 0;
}
class B<T> {
B(x);
}
var a = new A();
var b = new B<int>(a.x);
''');
}
test_implicitlyTyped_new_explicit_type_params_named() async {
// The reference to a.x does not trigger TOP_LEVEL_INSTANCE_GETTER because
// it can't possibly affect the type of b.
await assertNoErrorsInCode('''
class A {
var x = 0;
}
class B<T> {
B.named(x);
}
var a = new A();
var b = new B<int>.named(a.x);
''');
}
test_implicitlyTyped_new_explicit_type_params_prefixed() async {
// The reference to a.x does not trigger TOP_LEVEL_INSTANCE_GETTER because
// it can't possibly affect the type of b.
newFile('/test/lib/lib1.dart', content: '''
class B<T> {
B(x);
}
''');
await assertNoErrorsInCode('''
import 'lib1.dart' as foo;
class A {
var x = 0;
}
var a = new A();
var b = new foo.B<int>(a.x);
''');
}
test_implicitlyTyped_new_named() async {
// The reference to a.x triggers TOP_LEVEL_INSTANCE_GETTER because B is
// generic, so the type of a.x might affect the type of b.
await assertErrorsInCode('''
class A {
var x = 0;
}
class B<T> {
B.named(x);
}
var a = new A();
var b = new B.named(a.x);
''', [StrongModeCode.TOP_LEVEL_INSTANCE_GETTER]);
}
test_implicitlyTyped_new_not_generic() async {
// The reference to a.x does not trigger TOP_LEVEL_INSTANCE_GETTER because
// it can't possibly affect the type of b.
await assertNoErrorsInCode('''
class A {
var x = 0;
}
class B {
B(x);
}
var a = new A();
var b = new B(a.x);
''');
}
test_implicitlyTyped_new_not_generic_named() async {
// The reference to a.x does not trigger TOP_LEVEL_INSTANCE_GETTER because
// it can't possibly affect the type of b.
await assertNoErrorsInCode('''
class A {
var x = 0;
}
class B {
B.named(x);
}
var a = new A();
var b = new B.named(a.x);
''');
}
test_implicitlyTyped_new_not_generic_prefixed() async {
newFile('/test/lib/lib1.dart', content: '''
class B {
B(x);
}
''');
// The reference to a.x does not trigger TOP_LEVEL_INSTANCE_GETTER because
// it can't possibly affect the type of b.
await assertNoErrorsInCode('''
import 'lib1.dart' as foo;
class A {
var x = 0;
}
var a = new A();
var b = new foo.B(a.x);
''');
}
test_implicitlyTyped_new_prefixed() async {
newFile('/test/lib/lib1.dart', content: '''
class B<T> {
B(x);
}
''');
// The reference to a.x triggers TOP_LEVEL_INSTANCE_GETTER because B is
// generic, so the type of a.x might affect the type of b.
await assertErrorsInCode('''
import 'lib1.dart' as foo;
class A {
var x = 0;
}
var a = new A();
var b = new foo.B(a.x);
''', [StrongModeCode.TOP_LEVEL_INSTANCE_GETTER]);
}
test_implicitlyTyped_prefixedIdentifier() async {
await assertErrorsInCode('''
class A {
get g => 0;
}
var a = new A();
var b = a.g;
''', [StrongModeCode.TOP_LEVEL_INSTANCE_GETTER]);
}
test_implicitlyTyped_propertyAccessLhs() async {
// The reference to a.x triggers TOP_LEVEL_INSTANCE_GETTER because the type
// of a.x affects the lookup of y, which in turn affects the type of b.
await assertErrorsInCode('''
class A {
var x = new B();
int operator[](int value) => 0;
}
class B {
int y;
}
var a = new A();
var b = (a.x).y;
''', [StrongModeCode.TOP_LEVEL_INSTANCE_GETTER]);
}
test_prefixedIdentifier() async {
await assertNoErrorsInCode('''
class A {
int get g => 0;
}
var a = new A();
var b = a.g;
''');
TopLevelVariableDeclaration b = result.unit.declarations[2];
expect(b.variables.variables[0].declaredElement.type.toString(), 'int');
}
}

View file

@ -0,0 +1,89 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/src/error/codes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/driver_resolution.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(TopLevelInstanceMethodTest);
});
}
@reflectiveTest
class TopLevelInstanceMethodTest extends DriverResolutionTest {
test_noParameter() async {
await assertErrorsInCode('''
class A {
f() => 0;
}
var x = new A().f();
''', [StrongModeCode.TOP_LEVEL_INSTANCE_METHOD]);
}
test_parameter() async {
await assertNoErrorsInCode('''
class A {
int f(v) => 0;
}
var x = new A().f(0);
''');
}
test_parameter_generic() async {
await assertErrorsInCode('''
class A {
int f<T>(v) => 0;
}
var x = new A().f(0);
''', [StrongModeCode.TOP_LEVEL_INSTANCE_METHOD]);
}
test_parameter_generic_explicit() async {
await assertNoErrorsInCode('''
class A {
int f<T>(v) => 0;
}
var x = new A().f<int>(0);
''');
}
test_static() async {
await assertNoErrorsInCode('''
class A {
static f() => 0;
}
var x = A.f();
''');
}
test_tearOff() async {
await assertErrorsInCode('''
class A {
f() => 0;
}
var x = new A().f;
''', [StrongModeCode.TOP_LEVEL_INSTANCE_METHOD]);
}
test_tearOff_parameter() async {
await assertErrorsInCode('''
class A {
int f(v) => 0;
}
var x = new A().f;
''', [StrongModeCode.TOP_LEVEL_INSTANCE_METHOD]);
}
test_tearoff_static() async {
await assertNoErrorsInCode('''
class A {
static f() => 0;
}
var x = A.f;
''');
}
}

View file

@ -0,0 +1,25 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/src/error/codes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/driver_resolution.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(TypeCheckIsNotNullTest);
});
}
@reflectiveTest
class TypeCheckIsNotNullTest extends DriverResolutionTest {
test_not_Null() async {
await assertErrorsInCode(r'''
bool m(i) {
return i is! Null;
}
''', [HintCode.TYPE_CHECK_IS_NOT_NULL]);
}
}

View file

@ -0,0 +1,25 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/src/error/codes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/driver_resolution.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(TypeCheckIsNullTest);
});
}
@reflectiveTest
class TypeCheckIsNullTest extends DriverResolutionTest {
test_is_Null() async {
await assertErrorsInCode(r'''
bool m(i) {
return i is Null;
}
''', [HintCode.TYPE_CHECK_IS_NULL]);
}
}

View file

@ -22,8 +22,7 @@ class UndefinedGetterTest extends ResolverTestCase {
test_ifStatement_notPromoted() async {
await assertErrorsInCode('''
f() {
int x;
f(int x) {
if (x is String) {
x.length;
}
@ -33,8 +32,7 @@ f() {
test_ifStatement_promoted() async {
await assertNoErrorsInCode('''
f() {
Object x;
f(Object x) {
if (x is String) {
x.length;
}
@ -64,8 +62,7 @@ class UndefinedGetterWithControlFlowCollectionsTest extends ResolverTestCase {
test_ifElement_inList_notPromoted() async {
await assertErrorsInCode('''
f() {
int x;
f(int x) {
return [if (x is String) x.length];
}
''', [StaticTypeWarningCode.UNDEFINED_GETTER], verify: false);
@ -73,8 +70,7 @@ f() {
test_ifElement_inList_promoted() async {
await assertNoErrorsInCode('''
f() {
Object x;
f(Object x) {
return [if (x is String) x.length];
}
''');
@ -82,8 +78,7 @@ f() {
test_ifElement_inMap_notPromoted() async {
await assertErrorsInCode('''
f() {
int x;
f(int x) {
return {if (x is String) x : x.length};
}
''', [StaticTypeWarningCode.UNDEFINED_GETTER], verify: false);
@ -91,8 +86,7 @@ f() {
test_ifElement_inMap_promoted() async {
await assertNoErrorsInCode('''
f() {
Object x;
f(Object x) {
return {if (x is String) x : x.length};
}
''');
@ -100,8 +94,7 @@ f() {
test_ifElement_inSet_notPromoted() async {
await assertErrorsInCode('''
f() {
int x;
f(int x) {
return {if (x is String) x.length};
}
''', [StaticTypeWarningCode.UNDEFINED_GETTER], verify: false);
@ -109,8 +102,7 @@ f() {
test_ifElement_inSet_promoted() async {
await assertNoErrorsInCode('''
f() {
Object x;
f(Object x) {
return {if (x is String) x.length};
}
''');

View file

@ -0,0 +1,31 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/src/error/codes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/driver_resolution.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedHiddenNameTest);
});
}
@reflectiveTest
class UndefinedHiddenNameTest extends DriverResolutionTest {
test_export() async {
newFile('/test/lib/lib1.dart');
await assertErrorsInCode(r'''
export 'lib1.dart' hide a;
''', [HintCode.UNDEFINED_HIDDEN_NAME]);
}
test_import() async {
newFile('/test/lib/lib1.dart');
await assertErrorsInCode(r'''
import 'lib1.dart' hide a;
''', [HintCode.UNUSED_IMPORT, HintCode.UNDEFINED_HIDDEN_NAME]);
}
}

View file

@ -0,0 +1,173 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/src/error/codes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/driver_resolution.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedOperatorTest);
});
}
@reflectiveTest
class UndefinedOperatorTest extends DriverResolutionTest {
test_binaryExpression() async {
await assertErrorsInCode(r'''
class A {}
f(var a) {
if (a is A) {
a + 1;
}
}
''', [StaticTypeWarningCode.UNDEFINED_OPERATOR]);
}
test_binaryExpression_inSubtype() async {
await assertErrorsInCode(r'''
class A {}
class B extends A {
operator +(B b) {}
}
f(var a) {
if (a is A) {
a + 1;
}
}
''', [StaticTypeWarningCode.UNDEFINED_OPERATOR]);
}
test_indexBoth() async {
await assertErrorsInCode(r'''
class A {}
f(var a) {
if (a is A) {
a[0]++;
}
}
''', [
StaticTypeWarningCode.UNDEFINED_OPERATOR,
StaticTypeWarningCode.UNDEFINED_OPERATOR,
]);
}
test_indexBoth_inSubtype() async {
await assertErrorsInCode(r'''
class A {}
class B extends A {
operator [](int index) {}
}
f(var a) {
if (a is A) {
a[0]++;
}
}
''', [
StaticTypeWarningCode.UNDEFINED_OPERATOR,
StaticTypeWarningCode.UNDEFINED_OPERATOR,
]);
}
test_indexGetter() async {
await assertErrorsInCode(r'''
class A {}
f(var a) {
if (a is A) {
a[0];
}
}
''', [StaticTypeWarningCode.UNDEFINED_OPERATOR]);
}
test_indexGetter_inSubtype() async {
await assertErrorsInCode(r'''
class A {}
class B extends A {
operator [](int index) {}
}
f(var a) {
if (a is A) {
a[0];
}
}
''', [StaticTypeWarningCode.UNDEFINED_OPERATOR]);
}
test_indexSetter() async {
await assertErrorsInCode(r'''
class A {}
f(var a) {
if (a is A) {
a[0] = 1;
}
}
''', [StaticTypeWarningCode.UNDEFINED_OPERATOR]);
}
test_indexSetter_inSubtype() async {
await assertErrorsInCode(r'''
class A {}
class B extends A {
operator []=(i, v) {}
}
f(var a) {
if (a is A) {
a[0] = 1;
}
}
''', [StaticTypeWarningCode.UNDEFINED_OPERATOR]);
}
test_postfixExpression() async {
await assertNoErrorsInCode(r'''
class A {}
f(var a) {
if (a is A) {
a++;
}
}
''');
}
test_postfixExpression_inSubtype() async {
await assertNoErrorsInCode(r'''
class A {}
class B extends A {
operator +(B b) {return new B();}
}
f(var a) {
if (a is A) {
a++;
}
}
''');
}
test_prefixExpression() async {
await assertNoErrorsInCode(r'''
class A {}
f(var a) {
if (a is A) {
++a;
}
}
''');
}
test_prefixExpression_inSubtype() async {
await assertNoErrorsInCode(r'''
class A {}
class B extends A {
operator +(B b) {return new B();}
}
f(var a) {
if (a is A) {
++a;
}
}
''');
}
}

View file

@ -0,0 +1,42 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/src/error/codes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/driver_resolution.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedSetterTest);
});
}
@reflectiveTest
class UndefinedSetterTest extends DriverResolutionTest {
test_inSubtype() async {
await assertErrorsInCode(r'''
class A {}
class B extends A {
set b(x) {}
}
f(var a) {
if (a is A) {
a.b = 0;
}
}
''', [StaticTypeWarningCode.UNDEFINED_SETTER]);
}
test_inType() async {
await assertErrorsInCode(r'''
class A {}
f(var a) {
if(a is A) {
a.m = 0;
}
}
''', [StaticTypeWarningCode.UNDEFINED_SETTER]);
}
}

View file

@ -0,0 +1,31 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/src/error/codes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/driver_resolution.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedShownNameTest);
});
}
@reflectiveTest
class UndefinedShownNameTest extends DriverResolutionTest {
test_export() async {
newFile('/test/lib/lib1.dart');
await assertErrorsInCode(r'''
export 'lib1.dart' show a;
''', [HintCode.UNDEFINED_SHOWN_NAME]);
}
test_import() async {
newFile('/test/lib/lib1.dart');
await assertErrorsInCode(r'''
import 'lib1.dart' show a;
''', [HintCode.UNUSED_IMPORT, HintCode.UNDEFINED_SHOWN_NAME]);
}
}

View file

@ -0,0 +1,96 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/src/dart/error/hint_codes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/driver_resolution.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnnecessaryNoSuchMethodTest);
});
}
@reflectiveTest
class UnnecessaryNoSuchMethodTest extends DriverResolutionTest {
test_blockBody() async {
await assertErrorsInCode(r'''
class A {
noSuchMethod(x) => super.noSuchMethod(x);
}
class B extends A {
mmm();
noSuchMethod(y) {
return super.noSuchMethod(y);
}
}
''', [HintCode.UNNECESSARY_NO_SUCH_METHOD]);
}
test_blockBody_notReturnStatement() async {
await assertNoErrorsInCode(r'''
class A {
noSuchMethod(x) => super.noSuchMethod(x);
}
class B extends A {
mmm();
noSuchMethod(y) {
print(y);
}
}
''');
}
test_blockBody_notSingleStatement() async {
await assertNoErrorsInCode(r'''
class A {
noSuchMethod(x) => super.noSuchMethod(x);
}
class B extends A {
mmm();
noSuchMethod(y) {
print(y);
return super.noSuchMethod(y);
}
}
''');
}
test_expressionBody() async {
await assertErrorsInCode(r'''
class A {
noSuchMethod(x) => super.noSuchMethod(x);
}
class B extends A {
mmm();
noSuchMethod(y) => super.noSuchMethod(y);
}
''', [HintCode.UNNECESSARY_NO_SUCH_METHOD]);
}
test_expressionBody_notNoSuchMethod() async {
await assertNoErrorsInCode(r'''
class A {
noSuchMethod(x) => super.noSuchMethod(x);
}
class B extends A {
mmm();
noSuchMethod(y) => super.hashCode;
}
''');
}
test_expressionBody_notSuper() async {
await assertNoErrorsInCode(r'''
class A {
noSuchMethod(x) => super.noSuchMethod(x);
}
class B extends A {
mmm();
noSuchMethod(y) => 42;
}
''');
}
}

View file

@ -0,0 +1,39 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/src/dart/error/hint_codes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/driver_resolution.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnnecessaryTypeCheckFalseTest);
});
}
@reflectiveTest
class UnnecessaryTypeCheckFalseTest extends DriverResolutionTest {
test_null_not_Null() async {
await assertErrorsInCode(r'''
bool b = null is! Null;
''', [HintCode.UNNECESSARY_TYPE_CHECK_FALSE]);
}
test_type_not_dynamic() async {
await assertErrorsInCode(r'''
m(i) {
bool b = i is! dynamic;
}
''', [HintCode.UNNECESSARY_TYPE_CHECK_FALSE]);
}
test_type_not_object() async {
await assertErrorsInCode(r'''
m(i) {
bool b = i is! Object;
}
''', [HintCode.UNNECESSARY_TYPE_CHECK_FALSE]);
}
}

View file

@ -0,0 +1,39 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/src/dart/error/hint_codes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/driver_resolution.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnnecessaryTypeCheckTrueTest);
});
}
@reflectiveTest
class UnnecessaryTypeCheckTrueTest extends DriverResolutionTest {
test_null_is_Null() async {
await assertErrorsInCode(r'''
bool b = null is Null;
''', [HintCode.UNNECESSARY_TYPE_CHECK_TRUE]);
}
test_type_is_dynamic() async {
await assertErrorsInCode(r'''
m(i) {
bool b = i is dynamic;
}
''', [HintCode.UNNECESSARY_TYPE_CHECK_TRUE]);
}
test_type_is_object() async {
await assertErrorsInCode(r'''
m(i) {
bool b = i is Object;
}
''', [HintCode.UNNECESSARY_TYPE_CHECK_TRUE]);
}
}

View file

@ -0,0 +1,62 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/src/dart/error/hint_codes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/driver_resolution.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnusedCatchClauseTest);
});
}
@reflectiveTest
class UnusedCatchClauseTest extends DriverResolutionTest {
@override
bool get enableUnusedLocalVariable => true;
test_on_unusedException() async {
await assertErrorsInCode(r'''
main() {
try {
} on String catch (exception) {
}
}
''', [HintCode.UNUSED_CATCH_CLAUSE]);
}
test_on_usedException() async {
await assertNoErrorsInCode(r'''
main() {
try {
} on String catch (exception) {
print(exception);
}
}
''');
}
test_unusedException() async {
await assertNoErrorsInCode(r'''
main() {
try {
} catch (exception) {
}
}
''');
}
test_usedException() async {
await assertNoErrorsInCode(r'''
main() {
try {
} catch (exception) {
print(exception);
}
}
''');
}
}

View file

@ -0,0 +1,66 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/src/dart/error/hint_codes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../../generated/resolver_test_case.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnusedCatchStackTest);
});
}
@reflectiveTest
class UnusedCatchStackTest extends ResolverTestCase {
@override
bool get enableNewAnalysisDriver => true;
test_on_unusedStack() async {
enableUnusedLocalVariable = true;
await assertErrorsInCode(r'''
main() {
try {
} on String catch (exception, stackTrace) {
}
}
''', [HintCode.UNUSED_CATCH_STACK]);
}
test_on_usedStack() async {
enableUnusedLocalVariable = true;
await assertNoErrorsInCode(r'''
main() {
try {
} on String catch (exception, stackTrace) {
print(stackTrace);
}
}
''');
}
test_unusedStack() async {
enableUnusedLocalVariable = true;
await assertErrorsInCode(r'''
main() {
try {
} catch (exception, stackTrace) {
}
}
''', [HintCode.UNUSED_CATCH_STACK]);
}
test_usedStack() async {
enableUnusedLocalVariable = true;
await assertNoErrorsInCode(r'''
main() {
try {
} catch (exception, stackTrace) {
print(stackTrace);
}
}
''');
}
}

View file

@ -0,0 +1,577 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/src/dart/error/hint_codes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/driver_resolution.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnusedElementTest);
});
}
@reflectiveTest
class UnusedElementTest extends DriverResolutionTest {
@override
bool get enableUnusedElement => true;
test_class_isUsed_extends() async {
await assertNoErrorsInCode(r'''
class _A {}
class B extends _A {}
''');
}
test_class_isUsed_fieldDeclaration() async {
await assertNoErrorsInCode(r'''
class Foo {
_Bar x;
}
class _Bar {
}
''');
}
test_class_isUsed_implements() async {
await assertNoErrorsInCode(r'''
class _A {}
class B implements _A {}
''');
}
test_class_isUsed_instanceCreation() async {
await assertNoErrorsInCode(r'''
class _A {}
main() {
new _A();
}
''');
}
test_class_isUsed_staticFieldAccess() async {
await assertNoErrorsInCode(r'''
class _A {
static const F = 42;
}
main() {
_A.F;
}
''');
}
test_class_isUsed_staticMethodInvocation() async {
await assertNoErrorsInCode(r'''
class _A {
static m() {}
}
main() {
_A.m();
}
''');
}
test_class_isUsed_typeArgument() async {
await assertNoErrorsInCode(r'''
class _A {}
main() {
var v = new List<_A>();
print(v);
}
''');
}
test_class_notUsed_inClassMember() async {
await assertErrorsInCode(r'''
class _A {
static staticMethod() {
new _A();
}
instanceMethod() {
new _A();
}
}
''', [HintCode.UNUSED_ELEMENT]);
}
test_class_notUsed_inConstructorName() async {
await assertErrorsInCode(r'''
class _A {
_A() {}
_A.named() {}
}
''', [HintCode.UNUSED_ELEMENT]);
}
test_class_notUsed_isExpression() async {
await assertErrorsInCode(r'''
class _A {}
main(p) {
if (p is _A) {
}
}
''', [HintCode.UNUSED_ELEMENT]);
}
test_class_notUsed_noReference() async {
await assertErrorsInCode(r'''
class _A {}
main() {
}
''', [HintCode.UNUSED_ELEMENT]);
}
test_class_notUsed_variableDeclaration() async {
await assertErrorsInCode(r'''
class _A {}
main() {
_A v;
print(v);
}
print(x) {}
''', [HintCode.UNUSED_ELEMENT]);
}
test_enum_isUsed_fieldReference() async {
await assertNoErrorsInCode(r'''
enum _MyEnum {A, B, C}
main() {
print(_MyEnum.B);
}
''');
}
test_enum_notUsed_noReference() async {
await assertErrorsInCode(r'''
enum _MyEnum {A, B, C}
main() {
}
''', [HintCode.UNUSED_ELEMENT]);
}
test_functionLocal_isUsed_closure() async {
await assertNoErrorsInCode(r'''
main() {
print(() {});
}
print(x) {}
''');
}
test_functionLocal_isUsed_invocation() async {
await assertNoErrorsInCode(r'''
main() {
f() {}
f();
}
''');
}
test_functionLocal_isUsed_reference() async {
await assertNoErrorsInCode(r'''
main() {
f() {}
print(f);
}
print(x) {}
''');
}
test_functionLocal_notUsed_noReference() async {
await assertErrorsInCode(r'''
main() {
f() {}
}
''', [HintCode.UNUSED_ELEMENT]);
}
test_functionLocal_notUsed_referenceFromItself() async {
await assertErrorsInCode(r'''
main() {
_f(int p) {
_f(p - 1);
}
}
''', [HintCode.UNUSED_ELEMENT]);
}
test_functionTop_isUsed_invocation() async {
await assertNoErrorsInCode(r'''
_f() {}
main() {
_f();
}
''');
}
test_functionTop_isUsed_reference() async {
await assertNoErrorsInCode(r'''
_f() {}
main() {
print(_f);
}
print(x) {}
''');
}
test_functionTop_notUsed_noReference() async {
await assertErrorsInCode(r'''
_f() {}
main() {
}
''', [HintCode.UNUSED_ELEMENT]);
}
test_functionTop_notUsed_referenceFromItself() async {
await assertErrorsInCode(r'''
_f(int p) {
_f(p - 1);
}
main() {
}
''', [HintCode.UNUSED_ELEMENT]);
}
test_functionTypeAlias_isUsed_isExpression() async {
await assertNoErrorsInCode(r'''
typedef _F(a, b);
main(f) {
if (f is _F) {
print('F');
}
}
''');
}
test_functionTypeAlias_isUsed_reference() async {
await assertNoErrorsInCode(r'''
typedef _F(a, b);
main(_F f) {
}
''');
}
test_functionTypeAlias_isUsed_typeArgument() async {
await assertNoErrorsInCode(r'''
typedef _F(a, b);
main() {
var v = new List<_F>();
print(v);
}
''');
}
test_functionTypeAlias_isUsed_variableDeclaration() async {
await assertNoErrorsInCode(r'''
typedef _F(a, b);
class A {
_F f;
}
''');
}
test_functionTypeAlias_notUsed_noReference() async {
await assertErrorsInCode(r'''
typedef _F(a, b);
main() {
}
''', [HintCode.UNUSED_ELEMENT]);
}
test_getter_isUsed_invocation_implicitThis() async {
await assertNoErrorsInCode(r'''
class A {
get _g => null;
useGetter() {
var v = _g;
}
}
''');
}
test_getter_isUsed_invocation_PrefixedIdentifier() async {
await assertNoErrorsInCode(r'''
class A {
get _g => null;
}
main(A a) {
var v = a._g;
}
''');
}
test_getter_isUsed_invocation_PropertyAccess() async {
await assertNoErrorsInCode(r'''
class A {
get _g => null;
}
main() {
var v = new A()._g;
}
''');
}
test_getter_notUsed_noReference() async {
await assertErrorsInCode(r'''
class A {
get _g => null;
}
''', [HintCode.UNUSED_ELEMENT]);
}
test_getter_notUsed_referenceFromItself() async {
await assertErrorsInCode(r'''
class A {
get _g {
return _g;
}
}
''', [HintCode.UNUSED_ELEMENT]);
}
test_method_isUsed_hasReference_implicitThis() async {
await assertNoErrorsInCode(r'''
class A {
_m() {}
useMethod() {
print(_m);
}
}
print(x) {}
''');
}
test_method_isUsed_hasReference_implicitThis_subclass() async {
await assertNoErrorsInCode(r'''
class A {
_m() {}
useMethod() {
print(_m);
}
}
class B extends A {
_m() {}
}
print(x) {}
''');
}
test_method_isUsed_hasReference_PrefixedIdentifier() async {
await assertNoErrorsInCode(r'''
class A {
_m() {}
}
main(A a) {
a._m;
}
''');
}
test_method_isUsed_hasReference_PropertyAccess() async {
await assertNoErrorsInCode(r'''
class A {
_m() {}
}
main() {
new A()._m;
}
''');
}
test_method_isUsed_invocation_implicitThis() async {
await assertNoErrorsInCode(r'''
class A {
_m() {}
useMethod() {
_m();
}
}
''');
}
test_method_isUsed_invocation_implicitThis_subclass() async {
await assertNoErrorsInCode(r'''
class A {
_m() {}
useMethod() {
_m();
}
}
class B extends A {
_m() {}
}
''');
}
test_method_isUsed_invocation_MemberElement() async {
await assertNoErrorsInCode(r'''
class A<T> {
_m(T t) {}
}
main(A<int> a) {
a._m(0);
}
''');
}
test_method_isUsed_invocation_propagated() async {
await assertNoErrorsInCode(r'''
class A {
_m() {}
}
main() {
var a = new A();
a._m();
}
''');
}
test_method_isUsed_invocation_static() async {
await assertNoErrorsInCode(r'''
class A {
_m() {}
}
main() {
A a = new A();
a._m();
}
''');
}
test_method_isUsed_invocation_subclass() async {
await assertNoErrorsInCode(r'''
class A {
_m() {}
}
class B extends A {
_m() {}
}
main(A a) {
a._m();
}
''');
}
test_method_isUsed_notPrivate() async {
await assertNoErrorsInCode(r'''
class A {
m() {}
}
main() {
}
''');
}
test_method_isUsed_staticInvocation() async {
await assertNoErrorsInCode(r'''
class A {
static _m() {}
}
main() {
A._m();
}
''');
}
test_method_notUsed_noReference() async {
await assertErrorsInCode(r'''
class A {
static _m() {}
}
''', [HintCode.UNUSED_ELEMENT]);
}
test_method_notUsed_referenceFromItself() async {
await assertErrorsInCode(r'''
class A {
static _m(int p) {
_m(p - 1);
}
}
''', [HintCode.UNUSED_ELEMENT]);
}
test_setter_isUsed_invocation_implicitThis() async {
await assertNoErrorsInCode(r'''
class A {
set _s(x) {}
useSetter() {
_s = 42;
}
}
''');
}
test_setter_isUsed_invocation_PrefixedIdentifier() async {
await assertNoErrorsInCode(r'''
class A {
set _s(x) {}
}
main(A a) {
a._s = 42;
}
''');
}
test_setter_isUsed_invocation_PropertyAccess() async {
await assertNoErrorsInCode(r'''
class A {
set _s(x) {}
}
main() {
new A()._s = 42;
}
''');
}
test_setter_notUsed_noReference() async {
await assertErrorsInCode(r'''
class A {
set _s(x) {}
}
''', [HintCode.UNUSED_ELEMENT]);
}
test_setter_notUsed_referenceFromItself() async {
await assertErrorsInCode(r'''
class A {
set _s(int x) {
if (x > 5) {
_s = x - 1;
}
}
}
''', [HintCode.UNUSED_ELEMENT]);
}
test_topLevelVariable_isUsed() async {
await assertNoErrorsInCode(r'''
int _a = 1;
main() {
_a;
}
''');
}
test_topLevelVariable_isUsed_plusPlus() async {
await assertNoErrorsInCode(r'''
int _a = 0;
main() {
var b = _a++;
b;
}
''');
}
test_topLevelVariable_notUsed() async {
await assertErrorsInCode(r'''
int _a = 1;
main() {
_a = 2;
}
''', [HintCode.UNUSED_ELEMENT]);
}
}

View file

@ -0,0 +1,132 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/src/dart/error/hint_codes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../../generated/resolver_test_case.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnusedLocalVariableTest);
});
}
@reflectiveTest
class UnusedLocalVariableTest extends ResolverTestCase {
@override
bool get enableNewAnalysisDriver => true;
test_inFor_underscore_ignored() async {
enableUnusedLocalVariable = true;
await assertNoErrorsInCode(r'''
main() {
for (var _ in [1,2,3]) {
for (var __ in [4,5,6]) {
// do something
}
}
}
''');
}
test_inFunction() async {
enableUnusedLocalVariable = true;
await assertErrorsInCode(r'''
main() {
var v = 1;
v = 2;
}
''', [HintCode.UNUSED_LOCAL_VARIABLE]);
}
test_inMethod() async {
enableUnusedLocalVariable = true;
await assertErrorsInCode(r'''
class A {
foo() {
var v = 1;
v = 2;
}
}
''', [HintCode.UNUSED_LOCAL_VARIABLE]);
}
test_isInvoked() async {
enableUnusedLocalVariable = true;
await assertNoErrorsInCode(r'''
typedef Foo();
main() {
Foo foo;
foo();
}
''');
}
test_isNullAssigned() async {
enableUnusedLocalVariable = true;
await assertNoErrorsInCode(r'''
typedef Foo();
main() {
var v;
v ??= doSomething();
}
doSomething() => 42;
''');
}
test_isRead_notUsed_compoundAssign() async {
enableUnusedLocalVariable = true;
await assertErrorsInCode(r'''
main() {
var v = 1;
v += 2;
}
''', [HintCode.UNUSED_LOCAL_VARIABLE]);
}
test_isRead_notUsed_postfixExpr() async {
enableUnusedLocalVariable = true;
await assertErrorsInCode(r'''
main() {
var v = 1;
v++;
}
''', [HintCode.UNUSED_LOCAL_VARIABLE]);
}
test_isRead_notUsed_prefixExpr() async {
enableUnusedLocalVariable = true;
await assertErrorsInCode(r'''
main() {
var v = 1;
++v;
}
''', [HintCode.UNUSED_LOCAL_VARIABLE]);
}
test_isRead_usedArgument() async {
enableUnusedLocalVariable = true;
await assertNoErrorsInCode(r'''
main() {
var v = 1;
print(++v);
}
print(x) {}
''');
}
test_isRead_usedInvocationTarget() async {
enableUnusedLocalVariable = true;
await assertNoErrorsInCode(r'''
class A {
foo() {}
}
main() {
var a = new A();
a.foo();
}
''');
}
}