CQ. Migrate diagnostics tests to resolveTestCodeWithDiagnostics. UVWY.

Change-Id: I6d64591613d6755f5a37c303e794985688888ef5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/503440
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov
2026-05-13 12:01:16 -07:00
committed by dart-scoped@luci-project-accounts.iam.gserviceaccount.com
parent a6a403c0b9
commit 426137ba6a
74 changed files with 5090 additions and 6436 deletions
@@ -2,21 +2,22 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedAnnotationTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UndefinedAnnotationTest extends PubPackageResolutionTest {
test_identifier1_localVariable_const() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
main() {
const a = 0;
g(@a x) {}
@@ -26,70 +27,58 @@ main() {
}
test_unresolved_identifier() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
@unresolved
// [diag.undefinedAnnotation][column 1][length 11] Undefined name 'unresolved' used as an annotation.
main() {
}
''',
[error(diag.undefinedAnnotation, 0, 11)],
);
''');
}
test_unresolved_invocation() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
@Unresolved()
// [diag.undefinedAnnotation][column 1][length 13] Undefined name 'Unresolved' used as an annotation.
main() {
}
''',
[error(diag.undefinedAnnotation, 0, 13)],
);
''');
}
test_unresolved_prefix() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
@p.A(0)
// [diag.undefinedAnnotation][column 1][length 7] Undefined name 'p' used as an annotation.
class B {}
''',
[error(diag.undefinedAnnotation, 0, 7)],
);
''');
}
test_unresolved_prefixed() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'dart:math' as p;
@p.A(0)
// [diag.undefinedAnnotation][column 1][length 7] Undefined name 'A' used as an annotation.
class B {}
''',
[error(diag.undefinedAnnotation, 26, 7)],
);
''');
}
test_unresolved_prefixedIdentifier() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'dart:math' as p;
@p.unresolved
// [diag.undefinedAnnotation][column 1][length 13] Undefined name 'unresolved' used as an annotation.
main() {
}
''',
[error(diag.undefinedAnnotation, 25, 13)],
);
''');
}
test_useLibraryScope() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
@foo
// [diag.undefinedAnnotation][column 1][length 4] Undefined name 'foo' used as an annotation.
class A {
static const foo = null;
}
''',
[error(diag.undefinedAnnotation, 0, 4)],
);
''');
}
}
@@ -2,28 +2,27 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedClassBooleanTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UndefinedClassBooleanTest extends PubPackageResolutionTest {
test_variableDeclaration() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f() { boolean v; }
''',
[
error(diag.undefinedClassBoolean, 6, 7),
error(diag.unusedLocalVariable, 14, 1),
],
);
// ^^^^^^^
// [diag.undefinedClassBoolean] Undefined class 'boolean'.
// ^
// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used.
''');
}
}
@@ -2,72 +2,68 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedClassTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UndefinedClassTest extends PubPackageResolutionTest {
test_const() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
f() {
return const A();
// ^
// [diag.constWithNonType] The name 'A' isn't a class.
}
''',
[error(diag.constWithNonType, 21, 1)],
);
''');
}
test_dynamic_coreWithPrefix() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
import 'dart:core' as core;
dynamic x;
''',
[error(diag.undefinedClass, 29, 7)],
);
// [diag.undefinedClass][column 1][length 7] Undefined class 'dynamic'.
''');
}
test_ignore_libraryImport_prefix() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'a.dart' as p;
// ^^^^^^^^
// [diag.uriDoesNotExist] Target of URI doesn't exist: 'a.dart'.
p.A a;
''',
[error(diag.uriDoesNotExist, 7, 8)],
);
''');
}
test_ignore_libraryImport_show_it() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'a.dart' show A;
// ^^^^^^^^
// [diag.uriDoesNotExist] Target of URI doesn't exist: 'a.dart'.
A a;
''',
[error(diag.uriDoesNotExist, 7, 8)],
);
''');
}
test_ignore_libraryImport_show_other() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'a.dart' show B;
// ^^^^^^^^
// [diag.uriDoesNotExist] Target of URI doesn't exist: 'a.dart'.
A a;
''',
[error(diag.uriDoesNotExist, 7, 8), error(diag.undefinedClass, 25, 1)],
);
// [diag.undefinedClass][column 1][length 1] Undefined class 'A'.
''');
}
test_ignore_part_exists_uriGenerated_nameIgnorable() async {
@@ -75,108 +71,97 @@ A a;
part of 'test.dart';
''');
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
part 'a.g.dart';
_$A a;
''',
[error(diag.undefinedClass, 18, 3)],
);
// [diag.undefinedClass][column 1][length 3] Undefined class '_$A'.
''');
}
test_ignore_part_notExist_uriGenerated2_nameIgnorable() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
part 'a.template.dart';
// ^^^^^^^^^^^^^^^^^
// [diag.uriHasNotBeenGenerated] Target of URI hasn't been generated: 'package:test/a.template.dart'.
_$A a;
''',
[error(diag.uriHasNotBeenGenerated, 5, 17)],
);
''');
}
test_ignore_part_notExist_uriGenerated_nameIgnorable() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
part 'a.g.dart';
// ^^^^^^^^^^
// [diag.uriHasNotBeenGenerated] Target of URI hasn't been generated: 'package:test/a.g.dart'.
_$A a;
''',
[error(diag.uriHasNotBeenGenerated, 5, 10)],
);
''');
}
test_ignore_part_notExist_uriGenerated_nameNotIgnorable() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
part 'a.g.dart';
// ^^^^^^^^^^
// [diag.uriHasNotBeenGenerated] Target of URI hasn't been generated: 'package:test/a.g.dart'.
A a;
''',
[
error(diag.uriHasNotBeenGenerated, 5, 10),
error(diag.undefinedClass, 18, 1),
],
);
// [diag.undefinedClass][column 1][length 1] Undefined class 'A'.
''');
}
test_ignore_part_notExist_uriNotGenerated_nameIgnorable() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
part 'a.dart';
// ^^^^^^^^
// [diag.uriDoesNotExist] Target of URI doesn't exist: 'package:test/a.dart'.
_$A a;
''',
[error(diag.uriDoesNotExist, 5, 8), error(diag.undefinedClass, 16, 3)],
);
// [diag.undefinedClass][column 1][length 3] Undefined class '_$A'.
''');
}
test_ignore_part_notExist_uriNotGenerated_nameNotIgnorable() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
part 'a.dart';
// ^^^^^^^^
// [diag.uriDoesNotExist] Target of URI doesn't exist: 'package:test/a.dart'.
A a;
''',
[error(diag.uriDoesNotExist, 5, 8), error(diag.undefinedClass, 16, 1)],
);
// [diag.undefinedClass][column 1][length 1] Undefined class 'A'.
''');
}
test_import_exists_prefixed() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'dart:math' as p;
p.A a;
''',
[error(diag.undefinedClass, 26, 3)],
);
// [diag.undefinedClass][column 1][length 3] Undefined class 'A'.
''');
}
test_instanceCreation() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f() { new C(); }
''',
[error(diag.newWithNonType, 10, 1)],
);
// ^
// [diag.newWithNonType] The name 'C' isn't a class.
''');
}
test_Record() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
void f(Record r) {}
''');
}
test_Record_language219() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
// @dart = 2.19
void f(Record r) {}
''',
[error(diag.undefinedClass, 23, 6)],
);
// ^^^^^^
// [diag.undefinedClass] Undefined class 'Record'.
''');
}
test_Record_language219_exported() async {
@@ -184,7 +169,7 @@ void f(Record r) {}
export 'dart:core' show Record;
''');
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
// @dart = 2.19
import 'a.dart';
void f(Record r) {}
@@ -192,11 +177,12 @@ void f(Record r) {}
}
test_variableDeclaration() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f() { C c; }
''',
[error(diag.undefinedClass, 6, 1), error(diag.unusedLocalVariable, 8, 1)],
);
// ^
// [diag.undefinedClass] Undefined class 'C'.
// ^
// [diag.unusedLocalVariable] The value of the local variable 'c' isn't used.
''');
}
}
@@ -2,14 +2,15 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedConstructorInInitializerDefaultTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@@ -17,7 +18,7 @@ main() {
class UndefinedConstructorInInitializerDefaultTest
extends PubPackageResolutionTest {
test_hasOptionalParameters_defined() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
A([p]) {}
}
@@ -28,7 +29,7 @@ class B extends A {
}
test_implicit_defined_constructor() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
A();
}
@@ -39,7 +40,7 @@ class B extends A {
}
test_implicit_defined_primaryConstructor_hasBody() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
A();
}
@@ -50,7 +51,7 @@ class B() extends A {
}
test_implicit_defined_primaryConstructor_noBody() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
A();
}
@@ -59,56 +60,52 @@ class B() extends A;
}
test_implicit_notDefined_constructor_newHead() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
A.named();
}
class B extends A {
new foo();
//^^^^^^^
// [diag.undefinedConstructorInInitializerDefault] The class 'A' doesn't have an unnamed constructor.
}
''',
[error(diag.undefinedConstructorInInitializerDefault, 47, 7)],
);
''');
}
test_implicit_notDefined_constructor_typeName() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
A.named();
}
class B extends A {
B.foo();
//^^^^^
// [diag.undefinedConstructorInInitializerDefault] The class 'A' doesn't have an unnamed constructor.
}
''',
[error(diag.undefinedConstructorInInitializerDefault, 47, 5)],
);
''');
}
test_implicit_notDefined_primaryConstructor_hasBody() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
A.named();
}
class B.foo() extends A {
this;
//^^^^
// [diag.undefinedConstructorInInitializerDefault] The class 'A' doesn't have an unnamed constructor.
}
''',
[error(diag.undefinedConstructorInInitializerDefault, 53, 4)],
);
''');
}
test_implicit_notDefined_primaryConstructor_noBody() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
A.named();
}
class B.foo() extends A;
''',
[error(diag.undefinedConstructorInInitializerDefault, 31, 5)],
);
// ^^^^^
// [diag.undefinedConstructorInInitializerDefault] The class 'A' doesn't have an unnamed constructor.
''');
}
}
@@ -2,21 +2,22 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedConstructorInInitializerTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UndefinedConstructorInInitializerTest extends PubPackageResolutionTest {
test_explicit_named_defined_constructor() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
A.named() {}
}
@@ -27,7 +28,7 @@ class B extends A {
}
test_explicit_named_defined_primaryConstructorBody() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
A.named() {}
}
@@ -38,45 +39,29 @@ class B() extends A {
}
test_explicit_named_notDefined_constructor() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
class B extends A {
B() : super.named();
// ^^^^^^^^^^^^^
// [diag.undefinedConstructorInInitializer] The class 'A' doesn't have a constructor named 'named'.
}
''',
[
error(
diag.undefinedConstructorInInitializer,
39,
13,
messageContains: ["class 'A'", "named 'named'"],
),
],
);
''');
}
test_explicit_named_notDefined_primateConstructorBody() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
class B() extends A {
this : super.named();
// ^^^^^^^^^^^^^
// [diag.undefinedConstructorInInitializer] The class 'A' doesn't have a constructor named 'named'.
}
''',
[
error(
diag.undefinedConstructorInInitializer,
42,
13,
messageContains: ["class 'A'", "named 'named'"],
),
],
);
''');
}
test_explicit_unnamed_defined_constructor() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
A() {}
}
@@ -87,7 +72,7 @@ class B extends A {
}
test_explicit_unnamed_defined_primaryConstructorBody() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
A() {}
}
@@ -98,30 +83,28 @@ class B() extends A {
}
test_explicit_unnamed_notDefined_constructor() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
A.named() {}
}
class B extends A {
B() : super();
// ^^^^^^^
// [diag.undefinedConstructorInInitializerDefault] The class 'A' doesn't have an unnamed constructor.
}
''',
[error(diag.undefinedConstructorInInitializerDefault, 55, 7)],
);
''');
}
test_explicit_unnamed_notDefined_primaryConstructorBody() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
A.named() {}
}
class B() extends A {
this : super();
// ^^^^^^^
// [diag.undefinedConstructorInInitializerDefault] The class 'A' doesn't have an unnamed constructor.
}
''',
[error(diag.undefinedConstructorInInitializerDefault, 58, 7)],
);
''');
}
}
@@ -2,21 +2,22 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedEnumConstantTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UndefinedEnumConstantTest extends PubPackageResolutionTest {
test_defined() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
enum E { ONE }
E e() {
return E.ONE;
@@ -25,14 +26,13 @@ E e() {
}
test_undefined() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E { ONE }
E e() {
return E.TWO;
// ^^^
// [diag.undefinedEnumConstant] There's no constant named 'TWO' in 'E'.
}
''',
[error(diag.undefinedEnumConstant, 34, 3)],
);
''');
}
}
@@ -2,27 +2,27 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedEnumConstructorNamedTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UndefinedEnumConstructorNamedTest extends PubPackageResolutionTest {
test_it() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E {
v.named()
// ^^^^^
// [diag.undefinedEnumConstructorNamed] The enum doesn't have a constructor named 'named'.
}
''',
[error(diag.undefinedEnumConstructorNamed, 13, 5)],
);
''');
}
}
@@ -2,40 +2,39 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedEnumConstructorUnnamedTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UndefinedEnumConstructorUnnamedTest extends PubPackageResolutionTest {
test_withArguments() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E {
v();
//^
// [diag.undefinedEnumConstructorUnnamed] The enum doesn't have an unnamed constructor.
const E.named();
}
''',
[error(diag.undefinedEnumConstructorUnnamed, 11, 1)],
);
''');
}
test_withoutArguments() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E {
v;
//^
// [diag.undefinedEnumConstructorUnnamed] The enum doesn't have an unnamed constructor.
const E.named();
}
''',
[error(diag.undefinedEnumConstructorUnnamed, 11, 1)],
);
''');
}
}
@@ -2,21 +2,22 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedExtensionGetterTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UndefinedExtensionGetterTest extends PubPackageResolutionTest {
test_override_defined() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
extension E on String {
int get g => 0;
}
@@ -27,64 +28,59 @@ f() {
}
test_override_undefined() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
extension E on String {}
f() {
E('a').g;
// ^
// [diag.undefinedExtensionGetter] The getter 'g' isn't defined for the extension 'E'.
}
''',
[error(diag.undefinedExtensionGetter, 40, 1)],
);
''');
}
test_override_undefined_hasSetter() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
extension E on int {
set foo(int _) {}
}
f() {
E(0).foo;
// ^^^
// [diag.undefinedExtensionGetter] The getter 'foo' isn't defined for the extension 'E'.
}
''',
[error(diag.undefinedExtensionGetter, 56, 3)],
);
''');
}
test_override_undefined_hasSetter_plusEq() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
extension E on int {
set foo(int _) {}
}
f() {
E(0).foo += 1;
// ^^^
// [diag.undefinedExtensionGetter] The getter 'foo' isn't defined for the extension 'E'.
}
''',
[error(diag.undefinedExtensionGetter, 56, 3)],
);
''');
}
test_static_withInference() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
extension E on Object {}
var a = E.v;
''',
[error(diag.undefinedExtensionGetter, 35, 1)],
);
// ^
// [diag.undefinedExtensionGetter] The getter 'v' isn't defined for the extension 'E'.
''');
}
test_static_withoutInference() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
extension E on Object {}
void f() {
E.v;
// ^
// [diag.undefinedExtensionGetter] The getter 'v' isn't defined for the extension 'E'.
}
''',
[error(diag.undefinedExtensionGetter, 40, 1)],
);
''');
}
}
@@ -2,21 +2,22 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedExtensionMethodTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UndefinedExtensionMethodTest extends PubPackageResolutionTest {
test_method_defined() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
extension E on String {
int m() => 0;
}
@@ -27,15 +28,14 @@ f() {
}
test_method_undefined() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
extension E on String {}
f() {
E('a').m();
// ^
// [diag.undefinedExtensionMethod] The method 'm' isn't defined for the extension 'E'.
}
''',
[error(diag.undefinedExtensionMethod, 40, 1)],
);
''');
var node = findNode.methodInvocation('m();');
assertResolvedNodeText(node, r'''
@@ -65,24 +65,22 @@ MethodInvocation
}
test_static_withInference() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
extension E on Object {}
var a = E.m();
''',
[error(diag.undefinedExtensionMethod, 35, 1)],
);
// ^
// [diag.undefinedExtensionMethod] The method 'm' isn't defined for the extension 'E'.
''');
}
test_static_withoutInference() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
extension E on Object {}
void f() {
E.m();
// ^
// [diag.undefinedExtensionMethod] The method 'm' isn't defined for the extension 'E'.
}
''',
[error(diag.undefinedExtensionMethod, 40, 1)],
);
''');
}
}
@@ -2,21 +2,22 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedExtensionOperatorTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UndefinedExtensionOperatorTest extends PubPackageResolutionTest {
test_binary_defined() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
extension E on String {
void operator +(int offset) {}
}
@@ -27,15 +28,14 @@ f() {
}
test_binary_undefined() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
extension E on String {}
f() {
E('a') + 1;
// ^
// [diag.undefinedExtensionOperator] The operator '+' isn't defined for the extension 'E'.
}
''',
[error(diag.undefinedExtensionOperator, 40, 1)],
);
''');
var node = findNode.binary('+ 1');
assertResolvedNodeText(node, r'''
@@ -63,7 +63,7 @@ BinaryExpression
}
test_index_get_hasGetter() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
extension E on A {
@@ -77,23 +77,21 @@ f(A a) {
}
test_index_get_hasNone() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
extension E on A {}
f(A a) {
E(a)[0];
// ^^^
// [diag.undefinedExtensionOperator] The operator '[]' isn't defined for the extension 'E'.
}
''',
[error(diag.undefinedExtensionOperator, 48, 3)],
);
''');
}
test_index_get_hasSetter() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
extension E on A {
@@ -102,14 +100,14 @@ extension E on A {
f(A a) {
E(a)[0];
// ^^^
// [diag.undefinedExtensionOperator] The operator '[]' isn't defined for the extension 'E'.
}
''',
[error(diag.undefinedExtensionOperator, 93, 3)],
);
''');
}
test_index_getSet_hasBoth() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
extension E on A {
@@ -124,8 +122,7 @@ f(A a) {
}
test_index_getSet_hasGetter() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
extension E on A {
@@ -134,33 +131,29 @@ extension E on A {
f(A a) {
E(a)[0] += 1;
// ^^^
// [diag.undefinedExtensionOperator] The operator '[]=' isn't defined for the extension 'E'.
}
''',
[error(diag.undefinedExtensionOperator, 83, 3)],
);
''');
}
test_index_getSet_hasNone() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
extension E on A {}
f(A a) {
E(a)[0] += 1;
// ^^^
// [diag.undefinedExtensionOperator] The operator '[]' isn't defined for the extension 'E'.
// [diag.undefinedExtensionOperator] The operator '[]=' isn't defined for the extension 'E'.
}
''',
[
error(diag.undefinedExtensionOperator, 48, 3),
error(diag.undefinedExtensionOperator, 48, 3),
],
);
''');
}
test_index_getSet_hasSetter() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
extension E on A {
@@ -169,15 +162,14 @@ extension E on A {
f(A a) {
E(a)[0] += 1;
// ^^^
// [diag.undefinedExtensionOperator] The operator '[]' isn't defined for the extension 'E'.
}
''',
[error(diag.undefinedExtensionOperator, 93, 3)],
);
''');
}
test_index_set_hasGetter() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
extension E on A {
@@ -186,29 +178,28 @@ extension E on A {
f(A a) {
E(a)[0] = 1;
// ^^^
// [diag.undefinedExtensionOperator] The operator '[]=' isn't defined for the extension 'E'.
}
''',
[error(diag.undefinedExtensionOperator, 83, 3)],
);
''');
}
test_index_set_hasNone() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
extension E on A {}
f(A a) {
E(a)[0] = 1;
// ^^^
// [diag.undefinedExtensionOperator] The operator '[]=' isn't defined for the extension 'E'.
}
''',
[error(diag.undefinedExtensionOperator, 48, 3)],
);
''');
}
test_index_set_hasSetter() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
extension E on A {
@@ -222,7 +213,7 @@ f(A a) {
}
test_prefix_minus_defined() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
extension E on String {
String operator -() => substring(1);
}
@@ -233,14 +224,13 @@ f() {
}
test_prefix_minus_undefined() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
extension E on String {}
f() {
-E('a');
//^
// [diag.undefinedExtensionOperator] The operator 'unary-' isn't defined for the extension 'E'.
}
''',
[error(diag.undefinedExtensionOperator, 33, 1)],
);
''');
}
}
@@ -2,21 +2,22 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedExtensionSetterTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UndefinedExtensionSetterTest extends PubPackageResolutionTest {
test_override_defined() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
extension E on int {
void set foo(int _) {}
}
@@ -27,48 +28,44 @@ f() {
}
test_override_undefined() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
extension E on int {}
f() {
E(0).foo = 1;
// ^^^
// [diag.undefinedExtensionSetter] The setter 'foo' isn't defined for the extension 'E'.
}
''',
[error(diag.undefinedExtensionSetter, 35, 3)],
);
''');
}
test_override_undefined_hasGetter_eq() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
extension E on int {
int get foo => 0;
}
f() {
E(0).foo = 1;
// ^^^
// [diag.undefinedExtensionSetter] The setter 'foo' isn't defined for the extension 'E'.
}
''',
[error(diag.undefinedExtensionSetter, 56, 3)],
);
''');
}
test_override_undefined_hasGetter_plusEq() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
extension E on int {
int get foo => 0;
}
f() {
E(0).foo += 1;
// ^^^
// [diag.undefinedExtensionSetter] The setter 'foo' isn't defined for the extension 'E'.
}
''',
[error(diag.undefinedExtensionSetter, 56, 3)],
);
''');
}
test_override_undefined_hasGetterAndNonExtensionSetter() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class C {
int get id => 0;
void set id(int v) {}
@@ -80,21 +77,20 @@ extension Ext on C {
f(C c) {
Ext(c).id++;
// ^^
// [diag.undefinedExtensionSetter] The setter 'id' isn't defined for the extension 'Ext'.
}
''',
[error(diag.undefinedExtensionSetter, 117, 2)],
);
''');
}
test_static_undefined() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
extension E on int {}
void f() {
E.foo = 3;
// ^^^
// [diag.undefinedExtensionSetter] The setter 'foo' isn't defined for the extension 'E'.
}
''',
[error(diag.undefinedExtensionSetter, 37, 3)],
);
''');
}
}
@@ -2,65 +2,62 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedGetterTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UndefinedGetterTest extends PubPackageResolutionTest {
test_compoundAssignment_hasSetter_instance() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class C {
set foo(int _) {}
}
f(C c) {
c.foo += 1;
// ^^^
// [diag.undefinedGetter] The getter 'foo' isn't defined for the type 'C'.
}
''',
[error(diag.undefinedGetter, 46, 3)],
);
''');
}
test_compoundAssignment_hasSetter_static() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class C {
static set foo(int _) {}
}
f() {
C.foo += 1;
// ^^^
// [diag.undefinedGetter] The getter 'foo' isn't defined for the type 'C'.
}
''',
[error(diag.undefinedGetter, 50, 3)],
);
''');
}
test_emptyName() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class A {
}
main() {
print(A().);
// ^
// [diag.missingIdentifier] Expected an identifier.
}
''',
[error(diag.missingIdentifier, 33, 1)],
);
''');
}
test_extension_instance_extendedHasSetter_extensionHasGetter() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class C {
void set foo(int _) {}
}
@@ -70,55 +67,51 @@ extension E on C {
f() {
this.foo;
// ^^^
// [diag.undefinedGetter] The getter 'foo' isn't defined for the type 'C'.
}
}
''',
[error(diag.undefinedGetter, 95, 3)],
);
''');
}
test_extension_instance_undefined_hasSetter() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
extension E on int {
void set foo(int _) {}
}
f() {
0.foo;
// ^^^
// [diag.undefinedGetter] The getter 'foo' isn't defined for the type 'int'.
}
''',
[error(diag.undefinedGetter, 58, 3)],
);
''');
}
test_extension_instance_withInference() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
extension E on int {}
var a = 3.v;
''',
[error(diag.undefinedGetter, 32, 1)],
);
// ^
// [diag.undefinedGetter] The getter 'v' isn't defined for the type 'int'.
''');
}
test_extension_instance_withoutInference() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class C {}
extension E on C {}
f(C c) {
c.a;
// ^
// [diag.undefinedGetter] The getter 'a' isn't defined for the type 'C'.
}
''',
[error(diag.undefinedGetter, 46, 1)],
);
''');
}
test_extension_this_extendedHasSetter_extensionHasGetter() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class C {
void set foo(int _) {}
}
@@ -129,31 +122,30 @@ extension E on C {
f(C c) {
c.foo;
// ^^^
// [diag.undefinedGetter] The getter 'foo' isn't defined for the type 'C'.
}
''',
[error(diag.undefinedGetter, 93, 3)],
);
''');
}
test_functionAlias_typeInstantiated_getter() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
typedef Fn<T> = void Function(T);
void bar() {
Fn<int>.foo;
// ^^^
// [diag.undefinedGetterOnFunctionType] The getter 'foo' isn't defined for the 'Fn' function type.
}
extension E on Type {
int get foo => 1;
}
''',
[error(diag.undefinedGetterOnFunctionType, 58, 3)],
);
''');
}
test_functionAlias_typeInstantiated_getter_parenthesized() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
typedef Fn<T> = void Function(T);
void bar() {
@@ -170,7 +162,7 @@ extension E on Type {
// Referencing `.call` on a `Function` type works similarly to referencing
// it on `dynamic`--the reference is accepted at compile time, and all type
// checking is deferred until runtime.
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
f(Function f) {
return f.call;
}
@@ -178,7 +170,7 @@ f(Function f) {
}
test_get_from_abstract_field_final_valid() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
abstract class A {
abstract final int x;
}
@@ -187,7 +179,7 @@ int f(A a) => a.x;
}
test_get_from_abstract_field_valid() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
abstract class A {
abstract int x;
}
@@ -196,7 +188,7 @@ int f(A a) => a.x;
}
test_get_from_external_field_final_valid() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
class A {
external final int x;
}
@@ -205,7 +197,7 @@ int f(A a) => a.x;
}
test_get_from_external_field_valid() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
class A {
external int x;
}
@@ -214,7 +206,7 @@ int f(A a) => a.x;
}
test_get_from_external_static_field_final_valid() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
class A {
external static final int x;
}
@@ -223,7 +215,7 @@ int f() => A.x;
}
test_get_from_external_static_field_valid() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
class A {
external static int x;
}
@@ -232,18 +224,17 @@ int f() => A.x;
}
test_ifElement_inList_notPromoted() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f(int x) {
return [if (x is String) x.length];
// ^^^^^^
// [diag.undefinedGetter] The getter 'length' isn't defined for the type 'int'.
}
''',
[error(diag.undefinedGetter, 40, 6)],
);
''');
}
test_ifElement_inList_promoted() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
f(Object x) {
return [if (x is String) x.length];
}
@@ -251,18 +242,17 @@ f(Object x) {
}
test_ifElement_inMap_notPromoted() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f(int x) {
return {if (x is String) x : x.length};
// ^^^^^^
// [diag.undefinedGetter] The getter 'length' isn't defined for the type 'int'.
}
''',
[error(diag.undefinedGetter, 44, 6)],
);
''');
}
test_ifElement_inMap_promoted() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
f(Object x) {
return {if (x is String) x : x.length};
}
@@ -270,18 +260,17 @@ f(Object x) {
}
test_ifElement_inSet_notPromoted() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f(int x) {
return {if (x is String) x.length};
// ^^^^^^
// [diag.undefinedGetter] The getter 'length' isn't defined for the type 'int'.
}
''',
[error(diag.undefinedGetter, 40, 6)],
);
''');
}
test_ifElement_inSet_promoted() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
f(Object x) {
return {if (x is String) x.length};
}
@@ -289,20 +278,19 @@ f(Object x) {
}
test_ifStatement_notPromoted() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f(int x) {
if (x is String) {
x.length;
// ^^^^^^
// [diag.undefinedGetter] The getter 'length' isn't defined for the type 'int'.
}
}
''',
[error(diag.undefinedGetter, 38, 6)],
);
''');
}
test_ifStatement_promoted() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
f(Object x) {
if (x is String) {
x.length;
@@ -312,79 +300,72 @@ f(Object x) {
}
test_instance_undefined() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class T {}
f(T e) { return e.m; }
''',
[error(diag.undefinedGetter, 29, 1)],
);
// ^
// [diag.undefinedGetter] The getter 'm' isn't defined for the type 'T'.
''');
}
test_instance_undefined_mixin() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
mixin M {
f() { return this.m; }
// ^
// [diag.undefinedGetter] The getter 'm' isn't defined for the type 'M'.
}
''',
[error(diag.undefinedGetter, 30, 1)],
);
''');
}
test_new_cascade() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class C {}
f(C? c) {
c..new;
// ^^^
// [diag.undefinedGetter] The getter 'new' isn't defined for the type 'C?'.
}
''',
[error(diag.undefinedGetter, 27, 3)],
);
''');
}
test_new_dynamic() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f(dynamic d) {
d.new;
// ^^^
// [diag.undefinedGetter] The getter 'new' isn't defined for the type 'dynamic'.
}
''',
[error(diag.undefinedGetter, 19, 3)],
);
''');
}
test_new_expression() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class C {}
f(C? c1, C c2) {
(c1 ?? c2).new;
// ^^^
// [diag.undefinedGetter] The getter 'new' isn't defined for the type 'C'.
}
''',
[error(diag.undefinedGetter, 42, 3)],
);
''');
}
test_new_nullAware() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class C {}
f(C? c) {
c?.new;
// ^^^
// [diag.undefinedGetter] The getter 'new' isn't defined for the type 'C'.
}
''',
[error(diag.undefinedGetter, 27, 3)],
);
''');
}
test_new_prefixedIdentifier() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class C {}
abstract class D {
@@ -393,85 +374,79 @@ abstract class D {
f(D d) {
d.c.new;
// ^^^
// [diag.undefinedGetter] The getter 'new' isn't defined for the type 'C'.
}
''',
[error(diag.undefinedGetter, 60, 3)],
);
''');
}
test_new_simpleIdentifier() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class C {}
f(C c) {
c.new;
// ^^^
// [diag.undefinedGetter] The getter 'new' isn't defined for the type 'C'.
}
''',
[error(diag.undefinedGetter, 25, 3)],
);
''');
}
test_new_typeVariable() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f<T>(T t) {
t.new;
// ^^^
// [diag.undefinedGetter] The getter 'new' isn't defined for the type 'T'.
}
''',
[error(diag.undefinedGetter, 16, 3)],
);
''');
}
test_nullMember_undefined() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
m() {
Null _null;
_null.foo;
// ^^^
// [diag.invalidUseOfNullValue] An expression whose value is always 'null' can't be dereferenced.
}
''',
[error(diag.invalidUseOfNullValue, 28, 3)],
);
''');
}
test_object_call() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f(Object o) {
return o.call;
// ^^^^
// [diag.undefinedGetter] The getter 'call' isn't defined for the type 'Object'.
}
''',
[error(diag.undefinedGetter, 25, 4)],
);
''');
}
test_promotedTypeParameter_regress35305() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f<X extends num, Y extends X>(Y y) {
if (y is int) {
y.isEven;
// ^^^^^^
// [diag.undefinedGetter] The getter 'isEven' isn't defined for the type 'Y'.
}
}
''',
[error(diag.undefinedGetter, 66, 6)],
);
''');
}
test_propertyAccess_functionClass_call() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
void f(Function a) {
return (a).call;
// ^^^^^^^^
// [diag.returnOfInvalidTypeFromFunction] A value of type 'Function' can't be returned from the function 'f' because it has a return type of 'void'.
}
''',
[error(diag.returnOfInvalidTypeFromFunction, 30, 8)],
);
''');
}
test_propertyAccess_functionType_call() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
class A {
void staticMethod() {}
}
@@ -483,34 +458,31 @@ void f(A a) {
}
test_static_conditionalAccess_defined() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class A {
static var x;
}
var a = A?.x;
''',
[error(diag.invalidNullAwareOperator, 37, 2)],
);
// ^^
// [diag.invalidNullAwareOperator] The receiver can't be null, so the null-aware operator '?.' is unnecessary.
''');
}
test_static_definedInSuperclass() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class S {
static int get g => 0;
}
class C extends S {}
f(p) {
f(C.g);
}''',
[error(diag.undefinedGetter, 71, 1)],
);
// ^
// [diag.undefinedGetter] The getter 'g' isn't defined for the type 'C'.
}''');
}
test_static_extension_InstanceAccess() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class C {}
extension E on C {
@@ -520,52 +492,48 @@ extension E on C {
C g(C c) => C();
f(C c) {
g(c).a;
// ^
// [diag.undefinedGetter] The getter 'a' isn't defined for the type 'C'.
}
''',
[error(diag.undefinedGetter, 92, 1)],
);
''');
}
test_static_undefined() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class C {}
f(p) {
f(C.m);
}''',
[error(diag.undefinedGetter, 24, 1)],
);
// ^
// [diag.undefinedGetter] The getter 'm' isn't defined for the type 'C'.
}''');
}
test_typeLiteral_cascadeTarget() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class T {
static int get foo => 42;
}
main() {
T..foo;
// ^^^
// [diag.undefinedGetter] The getter 'foo' isn't defined for the type 'Type'.
}
''',
[error(diag.undefinedGetter, 54, 3)],
);
''');
}
test_typeLiteral_conditionalAccess() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class A {}
f() => A?.hashCode;
''',
[
error(diag.invalidNullAwareOperator, 19, 2),
error(diag.undefinedGetter, 21, 8),
],
);
// ^^
// [diag.invalidNullAwareOperator] The receiver can't be null, so the null-aware operator '?.' is unnecessary.
// ^^^^^^^^
// [diag.undefinedGetter] The getter 'hashCode' isn't defined for the type 'A'.
''');
}
test_typeSubstitution_defined() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A<E> {
E element;
A(this.element);
@@ -2,14 +2,15 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedHiddenNameTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@@ -17,21 +18,21 @@ main() {
class UndefinedHiddenNameTest extends PubPackageResolutionTest {
test_export() async {
newFile('$testPackageLibPath/lib1.dart', '');
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
export 'lib1.dart' hide a;
''',
[error(diag.undefinedHiddenName, 24, 1)],
);
// ^
// [diag.undefinedHiddenName] The library 'package:test/lib1.dart' doesn't export a member with the hidden name 'a'.
''');
}
test_import() async {
newFile('$testPackageLibPath/lib1.dart', '');
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib1.dart' hide a;
''',
[error(diag.unusedImport, 7, 11), error(diag.undefinedHiddenName, 24, 1)],
);
// ^^^^^^^^^^^
// [diag.unusedImport] Unused import: 'lib1.dart'.
// ^
// [diag.undefinedHiddenName] The library 'package:test/lib1.dart' doesn't export a member with the hidden name 'a'.
''');
}
}
@@ -2,25 +2,25 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedIdentifierAwaitTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UndefinedIdentifierAwaitTest extends PubPackageResolutionTest {
test_function() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
void a() { await; }
''',
[error(diag.undefinedIdentifierAwait, 11, 5)],
);
// ^^^^^
// [diag.undefinedIdentifierAwait] Undefined name 'await' in function body not marked with 'async'.
''');
}
}
@@ -2,137 +2,122 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedIdentifierTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UndefinedIdentifierTest extends PubPackageResolutionTest {
test_annotation_references_static_method_in_class() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
@Annotation(foo)
// ^^^
// [diag.undefinedIdentifier] Undefined name 'foo'.
// [diag.constWithNonConstantArgument] Arguments of a constant creation must be constant expressions.
class C {
static void foo() {}
}
class Annotation {
const Annotation(dynamic d);
}
''',
[
error(diag.undefinedIdentifier, 12, 3),
error(diag.constWithNonConstantArgument, 12, 3),
],
);
''');
}
test_annotation_references_static_method_in_class_from_type_parameter() async {
// It not is allowed for an annotation of a class type parameter to refer to
// a method in a class.
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class C<@Annotation(foo) T> {
// ^^^
// [diag.undefinedIdentifier] Undefined name 'foo'.
// [diag.constWithNonConstantArgument] Arguments of a constant creation must be constant expressions.
static void foo() {}
}
class Annotation {
const Annotation(dynamic d);
}
''',
[
error(diag.undefinedIdentifier, 20, 3),
error(diag.constWithNonConstantArgument, 20, 3),
],
);
''');
}
test_annotation_references_static_method_in_extension() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
@Annotation(foo)
// ^^^
// [diag.undefinedIdentifier] Undefined name 'foo'.
// [diag.constWithNonConstantArgument] Arguments of a constant creation must be constant expressions.
extension E on int {
static void foo() {}
}
class Annotation {
const Annotation(dynamic d);
}
''',
[
error(diag.undefinedIdentifier, 12, 3),
error(diag.constWithNonConstantArgument, 12, 3),
],
);
''');
}
test_annotation_references_static_method_in_extension_from_type_parameter() async {
// It is not allowed for an annotation of an extension type parameter to
// refer to a method in a class.
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
extension E<@Annotation(foo) T> on T {
// ^^^
// [diag.undefinedIdentifier] Undefined name 'foo'.
// [diag.constWithNonConstantArgument] Arguments of a constant creation must be constant expressions.
static void foo() {}
}
class Annotation {
const Annotation(dynamic d);
}
''',
[
error(diag.constWithNonConstantArgument, 24, 3),
error(diag.undefinedIdentifier, 24, 3),
],
);
''');
}
test_annotation_references_static_method_in_mixin() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
@Annotation(foo)
// ^^^
// [diag.undefinedIdentifier] Undefined name 'foo'.
// [diag.constWithNonConstantArgument] Arguments of a constant creation must be constant expressions.
mixin M {
static void foo() {}
}
class Annotation {
const Annotation(dynamic d);
}
''',
[
error(diag.undefinedIdentifier, 12, 3),
error(diag.constWithNonConstantArgument, 12, 3),
],
);
''');
}
test_annotation_references_static_method_in_mixin_from_type_parameter() async {
// It is not allowed for an annotation of a mixin type parameter to refer to
// a method in a class.
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
mixin M<@Annotation(foo) T> {
// ^^^
// [diag.undefinedIdentifier] Undefined name 'foo'.
// [diag.constWithNonConstantArgument] Arguments of a constant creation must be constant expressions.
static void foo() {}
}
class Annotation {
const Annotation(dynamic d);
}
''',
[
error(diag.undefinedIdentifier, 20, 3),
error(diag.constWithNonConstantArgument, 20, 3),
],
);
''');
}
test_annotation_uses_scope_resolution_class() async {
// If an annotation on a class type parameter cannot be resolved using the
// normal scope resolution mechanism, it is not resolved via implicit
// `this`.
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class C<@Annotation.function(foo) @Annotation.type(B) T> {
// ^^^
// [diag.undefinedIdentifier] Undefined name 'foo'.
// [diag.constWithNonConstantArgument] Arguments of a constant creation must be constant expressions.
static void foo() {}
static void B() {}
}
@@ -141,21 +126,18 @@ class Annotation {
const Annotation.function(void Function() f);
const Annotation.type(Type t);
}
''',
[
error(diag.undefinedIdentifier, 29, 3),
error(diag.constWithNonConstantArgument, 29, 3),
],
);
''');
}
test_annotation_uses_scope_resolution_extension() async {
// If an annotation on an extension type parameter cannot be resolved using
// the normal scope resolution mechanism, it is not resolved via implicit
// `this`.
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
extension E<@Annotation.function(foo) @Annotation.type(B) T> on C {}
// ^^^
// [diag.undefinedIdentifier] Undefined name 'foo'.
// [diag.constWithNonConstantArgument] Arguments of a constant creation must be constant expressions.
class C {
static void foo() {}
static void B() {}
@@ -165,21 +147,18 @@ class Annotation {
const Annotation.function(void Function() f);
const Annotation.type(Type t);
}
''',
[
error(diag.constWithNonConstantArgument, 33, 3),
error(diag.undefinedIdentifier, 33, 3),
],
);
''');
}
test_annotation_uses_scope_resolution_mixin() async {
// If an annotation on a mixin type parameter cannot be resolved using the
// normal scope resolution mechanism, it is not resolved via implicit
// `this`.
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
mixin M<@Annotation.function(foo) @Annotation.type(B) T> {
// ^^^
// [diag.undefinedIdentifier] Undefined name 'foo'.
// [diag.constWithNonConstantArgument] Arguments of a constant creation must be constant expressions.
static void foo() {}
static void B() {}
}
@@ -188,76 +167,65 @@ class Annotation {
const Annotation.function(void Function() f);
const Annotation.type(Type t);
}
''',
[
error(diag.undefinedIdentifier, 29, 3),
error(diag.constWithNonConstantArgument, 29, 3),
],
);
''');
}
test_assignedPatternVariable() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
void f() {
(x) = 0;
// ^
// [diag.undefinedIdentifier] Undefined name 'x'.
}
''',
[error(diag.undefinedIdentifier, 14, 1)],
);
''');
}
@failingTest
@SkippedTest() // TODO(scheglov): review this
test_commentReference() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
/** [m] xxx [new B.c] */
// ^
// [diag.undefinedIdentifier] Undefined name 'm'.
// ^
// [diag.undefinedIdentifier] Undefined name 'B'.
class A {
}''',
[
error(diag.undefinedIdentifier, 5, 1),
error(diag.undefinedIdentifier, 17, 1),
],
);
}''');
}
test_compoundAssignment_noGetter_hasSetter() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
set foo(int _) {}
void f() {
foo += 0;
//^^^
// [diag.undefinedIdentifier] Undefined name 'foo'.
}
''',
[error(diag.undefinedIdentifier, 32, 3)],
);
''');
}
test_compoundAssignment_noGetter_noSetter() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
void f() {
foo += 0;
//^^^
// [diag.undefinedIdentifier] Undefined name 'foo'.
}
''',
[error(diag.undefinedIdentifier, 13, 3)],
);
''');
}
test_for() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f(l) {
for (e in l) {
// ^
// [diag.undefinedIdentifier] Undefined name 'e'.
}
}''',
[error(diag.undefinedIdentifier, 14, 1)],
);
}''');
}
test_forElement_inList_insideElement() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
f(Object x) {
return [for(int x in []) x, null];
}
@@ -265,21 +233,19 @@ f(Object x) {
}
test_forElement_inList_outsideElement() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f() {
return [for (int x in []) null, x];
// ^
// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used.
// ^
// [diag.undefinedIdentifier] Undefined name 'x'.
}
''',
[
error(diag.unusedLocalVariable, 25, 1),
error(diag.undefinedIdentifier, 40, 1),
],
);
''');
}
test_forStatement_inBody() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
f() {
for (int x in []) {
x;
@@ -289,58 +255,53 @@ f() {
}
test_forStatement_outsideBody() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f() {
for (int x in []) {}
// ^
// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used.
x;
//^
// [diag.undefinedIdentifier] Undefined name 'x'.
}
''',
[
error(diag.unusedLocalVariable, 17, 1),
error(diag.undefinedIdentifier, 31, 1),
],
);
''');
}
test_function() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
int a() => b;
''',
[error(diag.undefinedIdentifier, 11, 1)],
);
// ^
// [diag.undefinedIdentifier] Undefined name 'b'.
''');
}
test_get_from_external_variable_final_valid() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
external final int x;
int f() => x;
''');
}
test_get_from_external_variable_valid() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
external int x;
int f() => x;
''');
}
test_importCore_withShow() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
import 'dart:core' show List;
main() {
List;
String;
}''',
[error(diag.undefinedIdentifier, 49, 6)],
);
//^^^^^^
// [diag.undefinedIdentifier] Undefined name 'String'.
}''');
}
test_inheritedGetter_shadowedBy_topLevelSetter() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class A {
int get foo => 0;
}
@@ -350,55 +311,51 @@ void set foo(int _) {}
class B extends A {
void bar() {
foo;
// ^^^
// [diag.undefinedIdentifier] Undefined name 'foo'.
}
}
''',
[error(diag.undefinedIdentifier, 96, 3)],
);
''');
}
test_initializer() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
var a = b;
''',
[error(diag.undefinedIdentifier, 8, 1)],
);
// ^
// [diag.undefinedIdentifier] Undefined name 'b'.
''');
}
test_methodInvocation() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f() { C.m(); }
''',
[error(diag.undefinedIdentifier, 6, 1)],
);
// ^
// [diag.undefinedIdentifier] Undefined name 'C'.
''');
}
test_postfixExpression_increment_noGetter_hasSetter() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
set foo(int _) {}
void f() {
foo++;
//^^^
// [diag.undefinedIdentifier] Undefined name 'foo'.
}
''',
[error(diag.undefinedIdentifier, 32, 3)],
);
''');
}
test_prefixExpression_increment_noGetter_hasSetter() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
set foo(int _) {}
void f() {
++foo;
// ^^^
// [diag.undefinedIdentifier] Undefined name 'foo'.
}
''',
[error(diag.undefinedIdentifier, 34, 3)],
);
''');
}
test_private_getter() async {
@@ -407,19 +364,17 @@ library lib;
class A {
var _foo;
}''');
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
import 'lib.dart';
class B extends A {
test() {
var v = _foo;
// ^
// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used.
// ^^^^
// [diag.undefinedIdentifier] Undefined name '_foo'.
}
}''',
[
error(diag.unusedLocalVariable, 58, 1),
error(diag.undefinedIdentifier, 62, 4),
],
);
}''');
}
test_private_setter() async {
@@ -428,20 +383,19 @@ library lib;
class A {
var _foo;
}''');
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
import 'lib.dart';
class B extends A {
test() {
_foo = 42;
// ^^^^
// [diag.undefinedIdentifier] Undefined name '_foo'.
}
}''',
[error(diag.undefinedIdentifier, 54, 4)],
);
}''');
}
test_set_external_variable_valid() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
external int x;
void f(int value) {
x = value;
@@ -450,29 +404,25 @@ void f(int value) {
}
test_synthetic_whenExpression_defined() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
print(x) {}
main() {
print(is String);
// ^^
// [diag.missingIdentifier] Expected an identifier.
}
''',
[error(diag.missingIdentifier, 29, 2)],
);
''');
}
test_synthetic_whenMethodName_defined() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
print(x) {}
void f(int p) {
p.();
// ^
// [diag.missingIdentifier] Expected an identifier.
// [diag.undefinedGetter] The getter '(' isn't defined for the type 'int'.
}
''',
[
error(diag.missingIdentifier, 32, 1),
error(diag.undefinedGetter, 32, 1),
],
);
''');
}
}
@@ -2,36 +2,34 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedMethodTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UndefinedMethodTest extends PubPackageResolutionTest {
test_conditional_expression_condition_context() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
T castObject<T>(Object value) => value as T;
main() {
(castObject(true)..whatever()) ? 1 : 2;
// ^^^^^^^^
// [diag.undefinedMethod] The method 'whatever' isn't defined for the type 'bool'.
}
''',
[
error(diag.undefinedMethod, 76, 8, messageContains: ["type 'bool'"]),
],
);
''');
}
test_constructor_defined() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class C {
C.m();
}
@@ -47,16 +45,15 @@ extension _ on B {
void a() {}
}
''');
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib.dart';
f(B b) {
b.a();
// ^
// [diag.undefinedMethod] The method 'a' isn't defined for the type 'B'.
}
''',
[error(diag.undefinedMethod, 33, 1)],
);
''');
}
test_definedInUnnamedExtension() async {
@@ -67,24 +64,24 @@ extension on C {
void a() {}
}
''');
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib.dart';
f(C c) {
c.a();
// ^
// [diag.undefinedMethod] The method 'a' isn't defined for the type 'C'.
}
''',
[error(diag.undefinedMethod, 33, 1)],
);
''');
}
test_extensionMethodHiddenByStaticSetter() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class C {
void f() {
foo();
// ^^^
// [diag.undefinedMethod] The method 'foo' isn't defined for the type 'C'.
}
static set foo(int x) {}
}
@@ -93,17 +90,16 @@ extension E on C {
int foo() => 1;
}
''',
[error(diag.undefinedMethod, 27, 3)],
);
''');
}
test_extensionMethodShadowingTopLevelSetter() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class C {
void f() {
foo();
// ^^^
// [diag.undefinedMethod] The method 'foo' isn't defined for the type 'C'.
}
}
@@ -112,13 +108,11 @@ extension E on C {
}
set foo(int x) {}
''',
[error(diag.undefinedMethod, 27, 3)],
);
''');
}
test_functionAlias_notInstantiated() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
typedef Fn<T> = void Function(T);
void bar() {
@@ -132,24 +126,23 @@ extension E on Type {
}
test_functionAlias_typeInstantiated() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
typedef Fn<T> = void Function(T);
void bar() {
Fn<int>.foo();
// ^^^
// [diag.undefinedMethodOnFunctionType] The method 'foo' isn't defined for the 'Fn' function type.
}
extension E on Type {
void foo() {}
}
''',
[error(diag.undefinedMethodOnFunctionType, 58, 3)],
);
''');
}
test_functionAlias_typeInstantiated_parenthesized() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
typedef Fn<T> = void Function(T);
void bar() {
@@ -163,7 +156,7 @@ extension E on Type {
}
test_functionExpression_callMethod_defined() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
main() {
(() => null).call();
}
@@ -171,7 +164,7 @@ main() {
}
test_functionExpression_directCall_defined() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
main() {
(() => null)();
}
@@ -179,8 +172,7 @@ main() {
}
test_ignoreTypePropagation() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
class B extends A {
m() {}
@@ -189,16 +181,15 @@ class C {
f() {
A a = new B();
a.m();
// ^
// [diag.undefinedMethod] The method 'm' isn't defined for the type 'A'.
}
}
''',
[error(diag.undefinedMethod, 85, 1)],
);
''');
}
test_localSetterShadowingExtensionMethod() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class C {}
extension E1 on C {
@@ -210,87 +201,80 @@ extension E2 on C {
void f() {
foo();
// ^^^
// [diag.undefinedMethod] The method 'foo' isn't defined for the type 'C'.
}
}
''',
[error(diag.undefinedMethod, 123, 3)],
);
''');
}
test_method_undefined() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class C {
f() {
abs();
// ^^^
// [diag.undefinedMethod] The method 'abs' isn't defined for the type 'C'.
}
}
''',
[error(diag.undefinedMethod, 22, 3)],
);
''');
}
test_method_undefined_cascade() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class C {}
f(C c) {
c..abs();
// ^^^
// [diag.undefinedMethod] The method 'abs' isn't defined for the type 'C'.
}
''',
[error(diag.undefinedMethod, 25, 3)],
);
''');
}
test_method_undefined_enum() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E { A }
f() => E.abs();
''',
[error(diag.undefinedMethod, 22, 3)],
);
// ^^^
// [diag.undefinedMethod] The method 'abs' isn't defined for the type 'E'.
''');
}
test_method_undefined_mixin() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
mixin M {}
f(M m) {
m.abs();
// ^^^
// [diag.undefinedMethod] The method 'abs' isn't defined for the type 'M'.
}
''',
[error(diag.undefinedMethod, 24, 3)],
);
''');
}
test_method_undefined_mixin_cascade() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
mixin M {}
f(M m) {
m..abs();
// ^^^
// [diag.undefinedMethod] The method 'abs' isn't defined for the type 'M'.
}
''',
[error(diag.undefinedMethod, 25, 3)],
);
''');
}
test_static_conditionalAccess_defined() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class A {
static void m() {}
}
f() { A?.m(); }
''',
[error(diag.invalidNullAwareOperator, 40, 2)],
);
// ^^
// [diag.invalidNullAwareOperator] The receiver can't be null, so the null-aware operator '?.' is unnecessary.
''');
}
test_static_extension_instanceAccess() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class C {}
extension E on C {
@@ -299,15 +283,14 @@ extension E on C {
f(C c) {
c.a();
// ^
// [diag.undefinedMethod] The method 'a' isn't defined for the type 'C'.
}
''',
[error(diag.undefinedMethod, 68, 1)],
);
''');
}
test_static_mixinApplication_superConstructorIsFactory() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
mixin M {}
class A {
@@ -319,41 +302,38 @@ class B = A with M;
void main() {
B.named();
// ^^^^^
// [diag.undefinedMethod] The method 'named' isn't defined for the type 'B'.
}
''',
[error(diag.undefinedMethod, 96, 5)],
);
''');
}
test_typeAlias_functionType() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
typedef A = void Function();
void f() {
A.foo();
// ^^^
// [diag.undefinedMethod] The method 'foo' isn't defined for the type 'Type'.
}
''',
[error(diag.undefinedMethod, 45, 3)],
);
''');
}
test_typeAlias_interfaceType() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
typedef A = List<int>;
void f() {
A.foo();
// ^^^
// [diag.undefinedMethod] The method 'foo' isn't defined for the type 'List'.
}
''',
[error(diag.undefinedMethod, 39, 3)],
);
''');
}
test_withExtension() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class C {}
extension E on C {
@@ -362,9 +342,9 @@ extension E on C {
f(C c) {
c.c();
// ^
// [diag.undefinedMethod] The method 'c' isn't defined for the type 'C'.
}
''',
[error(diag.undefinedMethod, 61, 1)],
);
''');
}
}
@@ -2,81 +2,77 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedNamedParameterTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UndefinedNamedParameterTest extends PubPackageResolutionTest {
test_constConstructor() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
const A();
}
main() {
const A(p: 0);
// ^
// [diag.undefinedNamedParameter] The named parameter 'p' isn't defined.
}
''',
[error(diag.undefinedNamedParameter, 44, 1)],
);
''');
}
test_constructor() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
A();
}
main() {
A(p: 0);
// ^
// [diag.undefinedNamedParameter] The named parameter 'p' isn't defined.
}
''',
[error(diag.undefinedNamedParameter, 32, 1)],
);
''');
}
test_enumConstant() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E {
v(a: 0);
// ^
// [diag.undefinedNamedParameter] The named parameter 'a' isn't defined.
const E();
}
''',
[error(diag.undefinedNamedParameter, 13, 1)],
);
''');
}
test_function() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f({a, b}) {}
main() {
f(c: 1);
}''',
[error(diag.undefinedNamedParameter, 26, 1)],
);
// ^
// [diag.undefinedNamedParameter] The named parameter 'c' isn't defined.
}''');
}
test_method() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
m() {}
}
main() {
A().m(p: 0);
// ^
// [diag.undefinedNamedParameter] The named parameter 'p' isn't defined.
}
''',
[error(diag.undefinedNamedParameter, 38, 1)],
);
''');
}
}
@@ -2,61 +2,58 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedOperatorTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UndefinedOperatorTest extends PubPackageResolutionTest {
test_assignmentExpression_undefined() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
class B {
f(A a) {
A a2 = new A();
a += a2;
// ^^
// [diag.undefinedOperator] The operator '+' isn't defined for the type 'A'.
}
}
''',
[error(diag.undefinedOperator, 58, 2)],
);
''');
}
test_binaryExpression() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
f(a) {
if (a is A) {
a + 1;
// ^
// [diag.undefinedOperator] The operator '+' isn't defined for the type 'A'.
}
}
''',
[error(diag.undefinedOperator, 40, 1)],
);
''');
}
test_binaryExpression_enum() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E { A }
f(E e) => e + 1;
''',
[error(diag.undefinedOperator, 25, 1)],
);
// ^
// [diag.undefinedOperator] The operator '+' isn't defined for the type 'E'.
''');
}
test_binaryExpression_inSubtype() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
class B extends A {
operator +(B b) {}
@@ -64,41 +61,37 @@ class B extends A {
f(a) {
if (a is A) {
a + 1;
// ^
// [diag.undefinedOperator] The operator '+' isn't defined for the type 'A'.
}
}
''',
[error(diag.undefinedOperator, 83, 1)],
);
''');
}
test_binaryExpression_mixin() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
mixin M {}
f(M m) => m + 1;
''',
[error(diag.undefinedOperator, 23, 1)],
);
// ^
// [diag.undefinedOperator] The operator '+' isn't defined for the type 'M'.
''');
}
test_index_both() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
f(A a) {
a[0]++;
// ^^^
// [diag.undefinedOperator] The operator '[]' isn't defined for the type 'A'.
// [diag.undefinedOperator] The operator '[]=' isn't defined for the type 'A'.
}
''',
[
error(diag.undefinedOperator, 24, 3),
error(diag.undefinedOperator, 24, 3),
],
);
''');
}
test_index_defined() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
operator [](a) {}
operator []=(a, b) {}
@@ -111,19 +104,18 @@ f(A a) {
}
test_index_enum() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E { A }
f(E e) {
e[0];
// ^^^
// [diag.undefinedOperator] The operator '[]' isn't defined for the type 'E'.
}
''',
[error(diag.undefinedOperator, 25, 3)],
);
''');
}
test_index_get_extendedHasNone_extensionHasGetter() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
extension E on A {
@@ -137,8 +129,7 @@ f(A a) {
}
test_index_get_extendedHasSetter_extensionHasGetter() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
void operator[]=(int index, int value) {}
}
@@ -149,51 +140,47 @@ extension E on A {
f(A a) {
a[0];
// ^^^
// [diag.undefinedOperator] The operator '[]' isn't defined for the type 'A'.
}
''',
[error(diag.undefinedOperator, 125, 3)],
);
''');
}
test_index_getter() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
f(A a) {
a[0];
// ^^^
// [diag.undefinedOperator] The operator '[]' isn't defined for the type 'A'.
}
''',
[error(diag.undefinedOperator, 24, 3)],
);
''');
}
test_index_mixin() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
mixin M {}
f(M m) {
m[0];
// ^^^
// [diag.undefinedOperator] The operator '[]' isn't defined for the type 'M'.
}
''',
[error(diag.undefinedOperator, 23, 3)],
);
''');
}
test_index_null() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
f(Null x) {
x[0];
// ^
// [diag.invalidUseOfNullValue] An expression whose value is always 'null' can't be dereferenced.
}
''',
[error(diag.invalidUseOfNullValue, 15, 1)],
);
''');
}
test_index_set_extendedHasGetter_extensionHasSetter() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
int operator[](int index) => 0;
}
@@ -204,14 +191,14 @@ extension E on A {
f(A a) {
a[0] = 1;
// ^^^
// [diag.undefinedOperator] The operator '[]=' isn't defined for the type 'A'.
}
''',
[error(diag.undefinedOperator, 125, 3)],
);
''');
}
test_index_set_extendedHasNone_extensionHasSetter() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
extension E on A {
@@ -225,155 +212,138 @@ f(A a) {
}
test_index_setter() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
f(A a) {
a[0] = 1;
// ^^^
// [diag.undefinedOperator] The operator '[]=' isn't defined for the type 'A'.
}
''',
[error(diag.undefinedOperator, 24, 3)],
);
''');
}
test_indexBoth_undefined() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
f(A a) {
a[0]++;
// ^^^
// [diag.undefinedOperator] The operator '[]' isn't defined for the type 'A'.
// [diag.undefinedOperator] The operator '[]=' isn't defined for the type 'A'.
}
''',
[
error(diag.undefinedOperator, 23, 3),
error(diag.undefinedOperator, 23, 3),
],
);
''');
}
test_indexGetter_undefined() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
f(A a) {
a[0];
// ^^^
// [diag.undefinedOperator] The operator '[]' isn't defined for the type 'A'.
}
''',
[error(diag.undefinedOperator, 23, 3)],
);
''');
}
test_indexSetter_undefined() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
f(A a) {
a[0] = 1;
// ^^^
// [diag.undefinedOperator] The operator '[]=' isn't defined for the type 'A'.
}
''',
[error(diag.undefinedOperator, 23, 3)],
);
''');
}
test_minus_null() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
m() {
Null x;
x - 3;
// ^
// [diag.invalidUseOfNullValue] An expression whose value is always 'null' can't be dereferenced.
}
''',
[error(diag.invalidUseOfNullValue, 20, 1)],
);
''');
}
test_minusEq_null() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
m() {
Null x;
// ^
// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used.
x -= 1;
// ^^
// [diag.invalidUseOfNullValue] An expression whose value is always 'null' can't be dereferenced.
}
''',
[
error(diag.unusedLocalVariable, 13, 1),
error(diag.invalidUseOfNullValue, 20, 2),
],
);
''');
}
test_plus_null() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
m() {
Null x;
x + 3;
// ^
// [diag.invalidUseOfNullValue] An expression whose value is always 'null' can't be dereferenced.
}
''',
[error(diag.invalidUseOfNullValue, 20, 1)],
);
''');
}
test_plus_undefined() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
f(A a) {
a + 1;
// ^
// [diag.undefinedOperator] The operator '+' isn't defined for the type 'A'.
}
''',
[error(diag.undefinedOperator, 24, 1)],
);
''');
}
test_plusEq_null() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
m() {
Null x;
// ^
// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used.
x += 1;
// ^^
// [diag.invalidUseOfNullValue] An expression whose value is always 'null' can't be dereferenced.
}
''',
[
error(diag.unusedLocalVariable, 13, 1),
error(diag.invalidUseOfNullValue, 20, 2),
],
);
''');
}
test_postfixDec_null() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
m() {
Null x;
// ^
// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used.
x--;
// ^^
// [diag.invalidUseOfNullValue] An expression whose value is always 'null' can't be dereferenced.
}
''',
[
error(diag.unusedLocalVariable, 13, 1),
error(diag.invalidUseOfNullValue, 19, 2),
],
);
''');
}
test_postfixExpression() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
f(a) {
if (a is A) {
a++;
// ^^
// [diag.undefinedOperator] The operator '+' isn't defined for the type 'A'.
}
}
''',
[error(diag.undefinedOperator, 39, 2)],
);
''');
}
test_postfixExpression_inSubtype() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
class B extends A {
operator +(B b) {return new B();}
@@ -381,84 +351,76 @@ class B extends A {
f(a) {
if (a is A) {
a++;
// ^^
// [diag.undefinedOperator] The operator '+' isn't defined for the type 'A'.
}
}
''',
[error(diag.undefinedOperator, 97, 2)],
);
''');
}
test_postfixExpression_mixin() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
mixin M {}
f(M m) {
m++;
// ^^
// [diag.undefinedOperator] The operator '+' isn't defined for the type 'M'.
}
''',
[error(diag.undefinedOperator, 23, 2)],
);
''');
}
test_postfixExpression_undefined() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
f(A a) {
a++;
// ^^
// [diag.undefinedOperator] The operator '+' isn't defined for the type 'A'.
}
''',
[error(diag.undefinedOperator, 23, 2)],
);
''');
}
test_postfixInc_null() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
m() {
Null x;
// ^
// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used.
x++;
// ^^
// [diag.invalidUseOfNullValue] An expression whose value is always 'null' can't be dereferenced.
}
''',
[
error(diag.unusedLocalVariable, 13, 1),
error(diag.invalidUseOfNullValue, 19, 2),
],
);
''');
}
test_prefixDec_null() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
m() {
Null x;
// ^
// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used.
--x;
//^^
// [diag.invalidUseOfNullValue] An expression whose value is always 'null' can't be dereferenced.
}
''',
[
error(diag.unusedLocalVariable, 13, 1),
error(diag.invalidUseOfNullValue, 18, 2),
],
);
''');
}
test_prefixExpression() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
f(a) {
if (a is A) {
++a;
// ^^
// [diag.undefinedOperator] The operator '+' isn't defined for the type 'A'.
}
}
''',
[error(diag.undefinedOperator, 38, 2)],
);
''');
}
test_prefixExpression_inSubtype() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
class B extends A {
operator +(B b) {return new B();}
@@ -466,71 +428,65 @@ class B extends A {
f(a) {
if (a is A) {
++a;
// ^^
// [diag.undefinedOperator] The operator '+' isn't defined for the type 'A'.
}
}
''',
[error(diag.undefinedOperator, 96, 2)],
);
''');
}
test_prefixExpression_mixin() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
mixin M {}
f(M m) {
-m;
//^
// [diag.undefinedOperator] The operator 'unary-' isn't defined for the type 'M'.
}
''',
[error(diag.undefinedOperator, 22, 1)],
);
''');
}
test_prefixExpression_undefined() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
f(A a) {
++a;
//^^
// [diag.undefinedOperator] The operator '+' isn't defined for the type 'A'.
}
''',
[error(diag.undefinedOperator, 22, 2)],
);
''');
}
test_prefixInc_null() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
m() {
Null x;
// ^
// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used.
++x;
//^^
// [diag.invalidUseOfNullValue] An expression whose value is always 'null' can't be dereferenced.
}
''',
[
error(diag.unusedLocalVariable, 13, 1),
error(diag.invalidUseOfNullValue, 18, 2),
],
);
''');
}
test_tilde_defined() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
const A = 3;
const B = ~((1 << A) - 1);
''');
}
test_unaryMinus_null() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
m() {
Null x;
// ^
// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used.
-x;
//^
// [diag.invalidUseOfNullValue] An expression whose value is always 'null' can't be dereferenced.
}
''',
[
error(diag.unusedLocalVariable, 13, 1),
error(diag.invalidUseOfNullValue, 18, 1),
],
);
''');
}
}
@@ -2,14 +2,15 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedPrefixedNameTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@@ -17,38 +18,35 @@ main() {
class UndefinedPrefixedNameTest extends PubPackageResolutionTest {
test_getterContext() async {
newFile('$testPackageLibPath/lib.dart', '');
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
import 'lib.dart' as p;
f() => p.c;
''',
[error(diag.undefinedPrefixedName, 33, 1)],
);
// ^
// [diag.undefinedPrefixedName] The name 'c' is being referenced through the prefix 'p', but it isn't defined in any of the libraries imported using that prefix.
''');
}
test_new() async {
newFile('$testPackageLibPath/lib.dart', '');
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib.dart' as p;
void f() {
p.new;
// ^^^
// [diag.undefinedPrefixedName] The name 'new' is being referenced through the prefix 'p', but it isn't defined in any of the libraries imported using that prefix.
}
''',
[error(diag.undefinedPrefixedName, 39, 3)],
);
''');
}
test_setterContext() async {
newFile('$testPackageLibPath/lib.dart', '');
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
import 'lib.dart' as p;
f() {
p.c = 0;
// ^
// [diag.undefinedPrefixedName] The name 'c' is being referenced through the prefix 'p', but it isn't defined in any of the libraries imported using that prefix.
}
''',
[error(diag.undefinedPrefixedName, 34, 1)],
);
''');
}
}
@@ -2,14 +2,15 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedReferencedParameterTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@@ -22,65 +23,48 @@ class UndefinedReferencedParameterTest extends PubPackageResolutionTest {
}
test_messageText_literalString() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'package:meta/meta.dart';
class Foo {
@UseResult.unless(parameterDefined: 'undef')
// ^^^^^^^
// [diag.undefinedReferencedParameter] The parameter 'undef' isn't defined by 'foo'.
int foo([int? value]) => value ?? 0;
}
''',
[
error(
diag.undefinedReferencedParameter,
84,
7,
messageContains: ["The parameter 'undef'", "isn't defined by 'foo'"],
),
],
);
''');
}
test_messageText_stringReference() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'package:meta/meta.dart';
const s = 'undef';
class Foo {
@UseResult.unless(parameterDefined: s)
// ^
// [diag.undefinedReferencedParameter] The parameter 's' isn't defined by 'foo'.
int foo([int? value]) => value ?? 0;
}
''',
[
error(
diag.undefinedReferencedParameter,
104,
1,
messageContains: ["The parameter 's'", "isn't defined by 'foo'"],
),
],
);
''');
}
test_method() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'package:meta/meta.dart';
class Foo {
@UseResult.unless(parameterDefined: 'undef')
// ^^^^^^^
// [diag.undefinedReferencedParameter] The parameter 'undef' isn't defined by 'foo'.
int foo([int? value]) => value ?? 0;
}
''',
[error(diag.undefinedReferencedParameter, 84, 7)],
);
''');
}
test_method_parameterDefined() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'package:meta/meta.dart';
class Foo {
@@ -91,14 +75,13 @@ class Foo {
}
test_topLevelFunction() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'package:meta/meta.dart';
@UseResult.unless(parameterDefined: 'undef')
// ^^^^^^^
// [diag.undefinedReferencedParameter] The parameter 'undef' isn't defined by 'foo'.
int foo([int? value]) => value ?? 0;
''',
[error(diag.undefinedReferencedParameter, 70, 7)],
);
''');
}
}
@@ -2,38 +2,38 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedSetterTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UndefinedSetterTest extends PubPackageResolutionTest {
test_functionAlias_typeInstantiated() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
typedef Fn<T> = void Function(T);
void bar() {
Fn<int>.foo = 7;
// ^^^
// [diag.undefinedSetterOnFunctionType] The setter 'foo' isn't defined for the 'Fn' function type.
}
extension E on Type {
set foo(int value) {}
}
''',
[error(diag.undefinedSetterOnFunctionType, 58, 3)],
);
''');
}
test_functionAlias_typeInstantiated_parenthesized() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
typedef Fn<T> = void Function(T);
void bar() {
@@ -50,7 +50,7 @@ extension E on Type {
newFile('$testPackageLibPath/lib.dart', r'''
library lib;
set y(int value) {}''');
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib.dart' as x;
main() {
x.y = 0;
@@ -59,31 +59,26 @@ main() {
}
test_instance_undefined() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class T {}
f(T e1) { e1.m = 0; }
''',
[
error(diag.undefinedSetter, 24, 1, messageContains: ["the type 'T'"]),
],
);
// ^
// [diag.undefinedSetter] The setter 'm' isn't defined for the type 'T'.
''');
}
test_instance_undefined_mixin() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
mixin M {
f() { this.m = 0; }
// ^
// [diag.undefinedSetter] The setter 'm' isn't defined for the type 'M'.
}
''',
[error(diag.undefinedSetter, 23, 1)],
);
''');
}
test_inSubtype() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
class B extends A {
set b(x) {}
@@ -91,103 +86,96 @@ class B extends A {
f(a) {
if (a is A) {
a.b = 0;
// ^
// [diag.undefinedSetter] The setter 'b' isn't defined for the type 'A'.
}
}
''',
[error(diag.undefinedSetter, 76, 1)],
);
''');
}
test_inType() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
f(a) {
if(a is A) {
a.m = 0;
// ^
// [diag.undefinedSetter] The setter 'm' isn't defined for the type 'A'.
}
}
''',
[error(diag.undefinedSetter, 39, 1)],
);
''');
}
test_new_cascade() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class C {}
f(C? c) {
c..new = 1;
// ^^^
// [diag.undefinedSetter] The setter 'new' isn't defined for the type 'C?'.
}
''',
[error(diag.undefinedSetter, 27, 3)],
);
''');
}
test_new_dynamic() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f(dynamic d) {
d.new = 1;
// ^^^
// [diag.undefinedSetter] The setter 'new' isn't defined for the type 'dynamic'.
}
''',
[error(diag.undefinedSetter, 19, 3)],
);
''');
}
test_new_instance() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class C {}
f(C c) {
c.new = 1;
// ^^^
// [diag.undefinedSetter] The setter 'new' isn't defined for the type 'C'.
}
''',
[error(diag.undefinedSetter, 25, 3)],
);
''');
}
test_new_interfaceType() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class C {}
f() {
C.new = 1;
// ^^^
// [diag.undefinedSetter] The setter 'new' isn't defined for the type 'C'.
}
''',
[error(diag.undefinedSetter, 22, 3)],
);
''');
}
test_new_nullAware() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class C {}
f(C? c) {
c?.new = 1;
// ^^^
// [diag.undefinedSetter] The setter 'new' isn't defined for the type 'C'.
}
''',
[error(diag.undefinedSetter, 27, 3)],
);
''');
}
test_new_typeVariable() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f<T>(T t) {
t.new = 1;
// ^^^
// [diag.undefinedSetter] The setter 'new' isn't defined for the type 'T'.
}
''',
[error(diag.undefinedSetter, 16, 3)],
);
''');
}
test_set_abstract_field_valid() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
abstract class A {
abstract int x;
}
@@ -198,7 +186,7 @@ void f(A a, int x) {
}
test_set_external_field_valid() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
class A {
external int x;
}
@@ -209,7 +197,7 @@ void f(A a, int x) {
}
test_set_external_static_field_valid() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
class A {
external static int x;
}
@@ -220,36 +208,31 @@ void f(int x) {
}
test_static_conditionalAccess_defined() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class A {
static var x;
}
f() { A?.x = 1; }
''',
[error(diag.invalidNullAwareOperator, 35, 2)],
);
// ^^
// [diag.invalidNullAwareOperator] The receiver can't be null, so the null-aware operator '?.' is unnecessary.
''');
}
test_static_definedInSuperclass() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class S {
static set s(int i) {}
}
class C extends S {}
f(p) {
f(C.s = 1);
}''',
[
error(diag.undefinedSetter, 71, 1, messageContains: ["type 'C'"]),
],
);
// ^
// [diag.undefinedSetter] The setter 's' isn't defined for the type 'C'.
}''');
}
test_static_extension_instanceAccess() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class C {}
extension E on C {
@@ -258,10 +241,10 @@ extension E on C {
f(C c) {
c.a = 2;
// ^
// [diag.undefinedSetter] The setter 'a' isn't defined for the type 'C'.
}
''',
[error(diag.undefinedSetter, 72, 1)],
);
''');
assertResolvedNodeText(findNode.assignment('a ='), r'''
AssignmentExpression
@@ -292,41 +275,38 @@ AssignmentExpression
}
test_static_undefined() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
f() { A.B = 0;}
''',
[error(diag.undefinedSetter, 19, 1)],
);
// ^
// [diag.undefinedSetter] The setter 'B' isn't defined for the type 'A'.
''');
}
test_typeLiteral_cascadeTarget() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class T {
static void set foo(_) {}
}
main() {
T..foo = 42;
// ^^^
// [diag.undefinedSetter] The setter 'foo' isn't defined for the type 'Type'.
}
''',
[error(diag.undefinedSetter, 54, 3)],
);
''');
}
test_withExtension() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class C {}
extension E on C {}
f(C c) {
c.a = 1;
// ^
// [diag.undefinedSetter] The setter 'a' isn't defined for the type 'C'.
}
''',
[error(diag.undefinedSetter, 46, 1)],
);
''');
}
}
@@ -2,14 +2,15 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedShownNameTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@@ -17,21 +18,21 @@ main() {
class UndefinedShownNameTest extends PubPackageResolutionTest {
test_export() async {
newFile('$testPackageLibPath/lib1.dart', '');
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
export 'lib1.dart' show a;
''',
[error(diag.undefinedShownName, 24, 1)],
);
// ^
// [diag.undefinedShownName] The library 'package:test/lib1.dart' doesn't export a member with the shown name 'a'.
''');
}
test_import() async {
newFile('$testPackageLibPath/lib1.dart', '');
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib1.dart' show a;
''',
[error(diag.unusedImport, 7, 11), error(diag.undefinedShownName, 24, 1)],
);
// ^^^^^^^^^^^
// [diag.unusedImport] Unused import: 'lib1.dart'.
// ^
// [diag.undefinedShownName] The library 'package:test/lib1.dart' doesn't export a member with the shown name 'a'.
''');
}
}
@@ -2,49 +2,48 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedSuperGetterTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UndefinedSuperGetterTest extends PubPackageResolutionTest {
test_class() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
class B extends A {
get g {
return super.g;
// ^
// [diag.undefinedSuperGetter] The getter 'g' isn't defined in a superclass of 'B'.
}
}
''',
[error(diag.undefinedSuperGetter, 58, 1)],
);
''');
}
test_enum() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E {
v;
void f() {
super.foo;
// ^^^
// [diag.undefinedSuperGetter] The getter 'foo' isn't defined in a superclass of 'E'.
}
}
''',
[error(diag.undefinedSuperGetter, 37, 3)],
);
''');
}
test_enum_OK() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
mixin M {
int get foo => 0;
}
@@ -2,94 +2,88 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedSuperOperatorTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UndefinedSuperOperatorTest extends PubPackageResolutionTest {
test_class_binaryExpression() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
class B extends A {
operator +(value) {
return super + value;
// ^
// [diag.undefinedSuperOperator] The operator '+' isn't defined in a superclass of 'B'.
}
}
''',
[error(diag.undefinedSuperOperator, 70, 1)],
);
''');
}
test_class_indexBoth() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
class B extends A {
operator [](index) {
return super[index]++;
// ^^^^^^^
// [diag.undefinedSuperOperator] The operator '[]' isn't defined in a superclass of 'B'.
// [diag.undefinedSuperOperator] The operator '[]=' isn't defined in a superclass of 'B'.
}
}
''',
[
error(diag.undefinedSuperOperator, 70, 7),
error(diag.undefinedSuperOperator, 70, 7),
],
);
''');
}
test_class_indexGetter() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
class B extends A {
operator [](index) {
return super[index + 1];
// ^^^^^^^^^^^
// [diag.undefinedSuperOperator] The operator '[]' isn't defined in a superclass of 'B'.
}
}
''',
[error(diag.undefinedSuperOperator, 70, 11)],
);
''');
}
test_class_indexSetter() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
class B extends A {
operator []=(index, value) {
super[index] = 0;
// ^^^^^^^
// [diag.undefinedSuperOperator] The operator '[]=' isn't defined in a superclass of 'B'.
}
}
''',
[error(diag.undefinedSuperOperator, 71, 7)],
);
''');
}
test_enum_binaryExpression() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E {
v;
void f() {
super + 0;
// ^
// [diag.undefinedSuperOperator] The operator '+' isn't defined in a superclass of 'E'.
}
}
''',
[error(diag.undefinedSuperOperator, 37, 1)],
);
''');
}
test_enum_binaryExpression_OK() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
mixin M {
int operator +(int a) => 0;
}
@@ -104,24 +98,21 @@ enum E with M {
}
test_enum_indexBoth() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E {
v;
void f() {
super[0]++;
// ^^^
// [diag.undefinedSuperOperator] The operator '[]' isn't defined in a superclass of 'E'.
// [diag.undefinedSuperOperator] The operator '[]=' isn't defined in a superclass of 'E'.
}
}
''',
[
error(diag.undefinedSuperOperator, 36, 3),
error(diag.undefinedSuperOperator, 36, 3),
],
);
''');
}
test_enum_indexBoth_OK() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
mixin M {
int operator [](int index) => 0;
void operator []=(int index, int value) {}
@@ -137,30 +128,28 @@ enum E with M {
}
test_enum_indexGetter() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E {
v;
void f() {
super[0];
// ^^^
// [diag.undefinedSuperOperator] The operator '[]' isn't defined in a superclass of 'E'.
}
}
''',
[error(diag.undefinedSuperOperator, 36, 3)],
);
''');
}
test_enum_indexSetter() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E {
v;
void f() {
super[0] = 0;
// ^^^
// [diag.undefinedSuperOperator] The operator '[]=' isn't defined in a superclass of 'E'.
}
}
''',
[error(diag.undefinedSuperOperator, 36, 3)],
);
''');
}
}
@@ -2,49 +2,48 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UndefinedSuperSetterTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UndefinedSuperSetterTest extends PubPackageResolutionTest {
test_class() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
class B extends A {
f() {
super.m = 0;
// ^
// [diag.undefinedSuperSetter] The setter 'm' isn't defined in a superclass of 'B'.
}
}
''',
[error(diag.undefinedSuperSetter, 49, 1)],
);
''');
}
test_enum() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E {
v;
void f() {
super.foo = 0;
// ^^^
// [diag.undefinedSuperSetter] The setter 'foo' isn't defined in a superclass of 'E'.
}
}
''',
[error(diag.undefinedSuperSetter, 37, 3)],
);
''');
}
test_enum_OK() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
mixin M {
set foo(int _) {}
}
@@ -14,10 +14,12 @@ import 'package:analyzer_testing/utilities/utilities.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnignorableIgnoreTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@@ -34,48 +36,39 @@ class UnignorableIgnoreTest extends PubPackageResolutionTest
writeTestPackageAnalysisOptionsFile(
analysisOptionsContent(unignorableNames: ['undefined_annotation']),
);
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
// ignore_for_file: undefined_annotation
// ^^^^^^^^^^^^^^^^^^^^
// [diag.unignorableIgnore] The diagnostic 'undefined_annotation' can't be ignored.
@x int a = 0;
''',
[
error(diag.unignorableIgnore, 20, 20),
error(diag.undefinedAnnotation, 41, 2),
],
);
// [diag.undefinedAnnotation][column 1][length 2] Undefined name 'x' used as an annotation.
''');
}
test_file_upperCase() async {
writeTestPackageAnalysisOptionsFile(
analysisOptionsContent(unignorableNames: ['UNDEFINED_ANNOTATION']),
);
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
// ignore_for_file: UNDEFINED_ANNOTATION
// ^^^^^^^^^^^^^^^^^^^^
// [diag.unignorableIgnore] The diagnostic 'undefined_annotation' can't be ignored.
@x int a = 0;
''',
[
error(diag.unignorableIgnore, 20, 20),
error(diag.undefinedAnnotation, 41, 2),
],
);
// [diag.undefinedAnnotation][column 1][length 2] Undefined name 'x' used as an annotation.
''');
}
test_line() async {
writeTestPackageAnalysisOptionsFile(
analysisOptionsContent(unignorableNames: ['undefined_annotation']),
);
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
// ignore: undefined_annotation
// ^^^^^^^^^^^^^^^^^^^^
// [diag.unignorableIgnore] The diagnostic 'undefined_annotation' can't be ignored.
@x int a = 0;
''',
[
error(diag.unignorableIgnore, 11, 20),
error(diag.undefinedAnnotation, 32, 2),
],
);
// [diag.undefinedAnnotation][column 1][length 2] Undefined name 'x' used as an annotation.
''');
}
test_lint() async {
@@ -2,69 +2,64 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnnecessaryCastPatternTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UnnecessaryCastPatternTest extends PubPackageResolutionTest {
test_matchedIsSameAsRequired() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(int x) {
if (x case var z as int) {}
// ^
// [diag.unusedLocalVariable] The value of the local variable 'z' isn't used.
// ^^
// [diag.unnecessaryCastPattern] Unnecessary cast pattern.
}
''',
[
error(diag.unusedLocalVariable, 33, 1),
error(diag.unnecessaryCastPattern, 35, 2),
],
);
''');
}
test_matchedIsSubtypeOfRequired() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(int x) {
if (x case var z as num) {}
// ^
// [diag.unusedLocalVariable] The value of the local variable 'z' isn't used.
// ^^
// [diag.unnecessaryCastPattern] Unnecessary cast pattern.
}
''',
[
error(diag.unusedLocalVariable, 33, 1),
error(diag.unnecessaryCastPattern, 35, 2),
],
);
''');
}
test_matchedIsSupertypeOfRequired() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(num x) {
if (x case var z as int) {}
// ^
// [diag.unusedLocalVariable] The value of the local variable 'z' isn't used.
}
''',
[error(diag.unusedLocalVariable, 33, 1)],
);
''');
}
test_matchedIsUnrelatedToRequired() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
class B {}
void f(A x) {
if (x case var z as B) {}
// ^
// [diag.unusedLocalVariable] The value of the local variable 'z' isn't used.
}
''',
[error(diag.unusedLocalVariable, 54, 1)],
);
''');
}
}
@@ -2,21 +2,22 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnnecessaryCastTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UnnecessaryCastTest extends PubPackageResolutionTest {
test_conditionalExpression_changesResultType_left() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
class B extends A {}
@@ -28,7 +29,7 @@ dynamic f(bool c, B x, B y) {
}
test_conditionalExpression_changesResultType_right() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {}
class B extends A {}
@@ -39,62 +40,59 @@ dynamic f(bool c, B x, B y) {
}
test_conditionalExpression_leftDynamic_rightUnnecessary() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
dynamic f(bool c, int a, int b) {
return c ? a : b as int;
// ^^^^^^^^
// [diag.unnecessaryCast] Unnecessary cast.
}
''',
[error(diag.unnecessaryCast, 51, 8)],
);
''');
}
test_conditionalExpression_leftUnnecessary() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
dynamic f(bool c, int a, int b) {
return c ? a as int : b;
// ^^^^^^^^
// [diag.unnecessaryCast] Unnecessary cast.
}
''',
[error(diag.unnecessaryCast, 47, 8)],
);
''');
}
test_conditionalExpression_leftUnnecessary_rightDynamic() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
dynamic f(bool c, int a, dynamic b) {
return c ? a as int : b;
// ^^^^^^^^
// [diag.unnecessaryCast] Unnecessary cast.
}
''',
[error(diag.unnecessaryCast, 51, 8)],
);
''');
}
test_conditionalExpression_leftUnnecessary_rightUnnecessary() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
dynamic f(bool c, int a, int b) {
return c ? a as int : b as int;
// ^^^^^^^^
// [diag.unnecessaryCast] Unnecessary cast.
// ^^^^^^^^
// [diag.unnecessaryCast] Unnecessary cast.
}
''',
[error(diag.unnecessaryCast, 47, 8), error(diag.unnecessaryCast, 58, 8)],
);
''');
}
test_conditionalExpression_rightUnnecessary() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
dynamic f(bool c, int a, int b) {
return c ? a : b as int;
// ^^^^^^^^
// [diag.unnecessaryCast] Unnecessary cast.
}
''',
[error(diag.unnecessaryCast, 51, 8)],
);
''');
}
test_dynamic_type() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(a) {
a as Object;
}
@@ -102,18 +100,17 @@ void f(a) {
}
test_expression_invalidType() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f() {
x as int;
//^
// [diag.undefinedIdentifier] Undefined name 'x'.
}
''',
[error(diag.undefinedIdentifier, 13, 1)],
);
''');
}
test_function_toSubtype_viaParameter() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(void Function(int) a) {
(a as void Function(num))(3);
}
@@ -121,7 +118,7 @@ void f(void Function(int) a) {
}
test_function_toSubtype_viaReturnType() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(num Function() a) {
(a as int Function())();
}
@@ -129,7 +126,7 @@ void f(num Function() a) {
}
test_function_toSupertype_viaParameter() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(void Function(num) a) {
(a as void Function(int))(3);
}
@@ -137,7 +134,7 @@ void f(void Function(num) a) {
}
test_function_toSupertype_viaReturnType() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(int Function() a) {
(a as num Function())();
}
@@ -145,7 +142,7 @@ void f(int Function() a) {
}
test_function_toUnrelated() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(num Function(num) a) {
(a as int Function(int))(3);
}
@@ -153,7 +150,7 @@ void f(num Function(num) a) {
}
test_function_toUnrelated_generic() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f<T extends num>(T Function(T) a) {
(a as int Function(int))(3);
}
@@ -161,7 +158,7 @@ void f<T extends num>(T Function(T) a) {
}
test_type_dynamic() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f() {
Object as dynamic;
}
@@ -169,7 +166,7 @@ void f() {
}
test_type_function() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Function f) {
f as Function;
}
@@ -177,7 +174,7 @@ void f(Function f) {
}
test_type_supertype() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(int a) {
a as Object;
}
@@ -185,30 +182,28 @@ void f(int a) {
}
test_type_type() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(num a) {
a as num;
//^^^^^^^^
// [diag.unnecessaryCast] Unnecessary cast.
}
''',
[error(diag.unnecessaryCast, 18, 8)],
);
''');
}
test_type_type_asInterfaceTypeTypedef() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
typedef N = num;
void f(num a) {
a as N;
//^^^^^^
// [diag.unnecessaryCast] Unnecessary cast.
}
''',
[error(diag.unnecessaryCast, 35, 6)],
);
''');
}
test_typeParameter_hasBound_same() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f<T extends num>(T a) {
a as num;
}
@@ -216,7 +211,7 @@ void f<T extends num>(T a) {
}
test_typeParameter_hasBound_subtype() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f<T extends int>(T a) {
a as num;
}
@@ -224,7 +219,7 @@ void f<T extends int>(T a) {
}
test_typeParameter_hasBound_unrelated() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f<T extends num>(T a) {
a as String;
}
@@ -232,7 +227,7 @@ void f<T extends num>(T a) {
}
test_typeParameter_noBound() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f<T>(T a) {
a as num;
}
@@ -2,21 +2,22 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnnecessaryFinalTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UnnecessaryFinalTest extends PubPackageResolutionTest {
test_final() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
// @dart = 3.10
class C {
C(final int value);
@@ -25,27 +26,25 @@ class C {
}
test_positional() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
// @dart = 3.10
class C {
C([final this.value = 0]);
// ^^^^^
// [diag.unnecessaryFinal] The keyword 'final' isn't necessary because the parameter is implicitly 'final'.
int value;
}
''',
[error(diag.unnecessaryFinal, 31, 5)],
);
''');
}
test_primaryConstructor_parameter() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
class C(final int x);
''');
}
test_super() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
// @dart = 3.10
class A {
A(this.value);
@@ -54,22 +53,21 @@ class A {
class B extends A {
B(final super.value);
// ^^^^^
// [diag.unnecessaryFinal] The keyword 'final' isn't necessary because the parameter is implicitly 'final'.
}
''',
[error(diag.unnecessaryFinal, 83, 5)],
);
''');
}
test_this() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
// @dart = 3.10
class C {
C(final this.value);
// ^^^^^
// [diag.unnecessaryFinal] The keyword 'final' isn't necessary because the parameter is implicitly 'final'.
int value;
}
''',
[error(diag.unnecessaryFinal, 30, 5)],
);
''');
}
}
@@ -7,10 +7,12 @@ import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnnecessaryImportTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@@ -22,7 +24,7 @@ class A {
const A() {}
}
''');
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
@A()
import 'lib1.dart';
''');
@@ -36,7 +38,7 @@ class A {}
export 'lib1.dart';
class B {}
''');
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
import 'lib1.dart';
import 'lib2.dart' as two;
f(A a, two.B b) {}
@@ -51,7 +53,7 @@ class A {}
export 'lib1.dart';
class B {}
''');
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
import 'lib1.dart' as one;
import 'lib2.dart' as two;
f(one.A a, two.B b) {}
@@ -65,7 +67,7 @@ class A {}
newFile('$testPackageLibPath/lib2.dart', r'''
class B {}
''');
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib1.dart' as one;
import 'lib2.dart' as one;
f(one.A a, one.B b) {}
@@ -82,7 +84,7 @@ class B {}
newFile('$testPackageLibPath/lib3.dart', r'''
export 'lib2.dart';
''');
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib1.dart' as one;
import 'lib3.dart' as one;
f(one.A a, one.B b) {}
@@ -96,7 +98,7 @@ class A {}
newFile('$testPackageLibPath/lib2.dart', r'''
class B {}
''');
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib1.dart' as one;
import 'lib2.dart' as one; // ignore: unused_import
f(one.A a) {}
@@ -108,7 +110,7 @@ f(one.A a) {}
class A {}
class B {}
''');
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib1.dart' as one show A, B;
f(one.A a, one.B b) {}
''');
@@ -119,7 +121,7 @@ f(one.A a, one.B b) {}
class One {}
topLevelFunction() {}
''');
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib1.dart' hide topLevelFunction;
import 'lib1.dart' as one show topLevelFunction;
f(One o) {
@@ -133,7 +135,7 @@ f(One o) {
class One {}
topLevelFunction() {}
''');
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib1.dart' hide topLevelFunction;
import 'lib1.dart' as one show topLevelFunction;
import 'lib1.dart' as two show topLevelFunction;
@@ -148,7 +150,7 @@ f(One o) {
newFile('$testPackageLibPath/lib1.dart', '''
class File {}
''');
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
import 'dart:io' as io;
import 'lib1.dart' as io;
g(io.Directory d, io.File f) {}
@@ -163,14 +165,13 @@ class A {}
export 'lib1.dart';
class B {}
''');
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
import 'lib1.dart' as p;
// ^^^^^^^^^^^
// [diag.unnecessaryImport] The import of 'lib1.dart' is unnecessary because all of the used elements are also provided by the import of 'lib2.dart'.
import 'lib2.dart' as p;
f(p.A a, p.B b) {}
''',
[error(diag.unnecessaryImport, 7, 11)],
);
''');
}
test_library_duplicateImport_differentPrefix() async {
@@ -178,7 +179,7 @@ f(p.A a, p.B b) {}
class A {}
class B {}
''');
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
import 'lib1.dart';
import 'lib1.dart' as p;
f(A a1, p.A a2, B b) {}
@@ -226,17 +227,16 @@ extension E2 on int {
void bar() {}
}
''');
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
import 'lib1.dart' as prefix;
// ^^^^^^^^^^^
// [diag.unnecessaryImport] The import of 'lib1.dart' is unnecessary because all of the used elements are also provided by the import of 'lib2.dart'.
import 'lib2.dart' as prefix;
void f() {
0.foo();
0.bar();
}
''',
[error(diag.unnecessaryImport, 7, 11)],
);
''');
}
test_library_extension_noPrefixes_necessary() async {
@@ -250,7 +250,7 @@ extension E2 on int {
void bar() {}
}
''');
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
import 'lib1.dart';
import 'lib2.dart';
void f() {
@@ -272,17 +272,16 @@ extension E2 on int {
void bar() {}
}
''');
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
import 'lib1.dart';
// ^^^^^^^^^^^
// [diag.unnecessaryImport] The import of 'lib1.dart' is unnecessary because all of the used elements are also provided by the import of 'lib2.dart'.
import 'lib2.dart';
void f() {
0.foo();
0.bar();
}
''',
[error(diag.unnecessaryImport, 7, 11)],
);
''');
}
test_library_hasDeprecatedExport_hasNotDeprecatedImport_hasOtherClass() async {
@@ -301,7 +300,7 @@ class B {}
// `import b` is not reported because provides used `B`.
// `A` is from both `a.dart` and `b.dart`, so not reported.
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
import 'a.dart';
import 'b.dart';
@@ -331,16 +330,15 @@ class C {}
// But the export of `B` from `c.dart` is deprecated.
// We can get `B` from `import b`, in a not deprecated way.
// It also declares `C`, but we don't use it.
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
import 'a.dart';
import 'b.dart';
import 'c.dart';
// ^^^^^^^^
// [diag.unnecessaryImport] The import of 'c.dart' is unnecessary because all of the used elements are also provided by the import of 'a.dart'.
void f(A _, B _) {}
''',
[error(diag.unnecessaryImport, 41, 8)],
);
''');
}
test_library_hasDeprecatedExport_noNotDeprecatedImport() async {
@@ -361,15 +359,14 @@ export 'b.dart';
// `import c` is not marked as unnecessary because of there is
// `DEPRECATED_EXPORT_USE` already reported.
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
import 'a.dart';
import 'c.dart';
void f(A _, B _) {}
''',
[error(diag.deprecatedExportUse, 47, 1)],
);
// ^
// [diag.deprecatedExportUse] The ability to import 'B' indirectly is deprecated.
''');
}
test_library_hide() async {
@@ -380,7 +377,7 @@ class A {}
export 'lib1.dart' hide A;
class B {}
''');
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
import 'lib1.dart';
import 'lib2.dart';
f(A a, B b) {}
@@ -415,7 +412,7 @@ method() => C();
newFile('$testPackageLibPath/lib1.dart', '''
class File {}
''');
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
import 'dart:io';
import 'lib1.dart';
g(Directory d, File f) {}
@@ -432,14 +429,13 @@ export 'a.dart';
class B {}
''');
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
import 'a.dart';
import 'b.dart';
void f(A _, B _, C _) {}
''',
[error(diag.undefinedClass, 51, 1)],
);
// ^
// [diag.undefinedClass] Undefined class 'C'.
''');
}
test_library_unnecessaryImport() async {
@@ -450,14 +446,13 @@ class A {}
export 'lib1.dart';
class B {}
''');
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
import 'lib1.dart';
// ^^^^^^^^^^^
// [diag.unnecessaryImport] The import of 'lib1.dart' is unnecessary because all of the used elements are also provided by the import of 'lib2.dart'.
import 'lib2.dart';
f(A a, B b) {}
''',
[error(diag.unnecessaryImport, 7, 11)],
);
''');
}
test_library_unnecessaryImport_sameUri() async {
@@ -468,14 +463,13 @@ class A {}
export 'lib1.dart';
class B {}
''');
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
import 'dart:async';
import 'dart:async' show Completer;
// ^^^^^^^^^^^^
// [diag.unnecessaryImport] The import of 'dart:async' is unnecessary because all of the used elements are also provided by the import of 'dart:async'.
f(FutureOr<int> a, Completer<int> b) {}
''',
[error(diag.unnecessaryImport, 28, 12)],
);
''');
}
test_library_uriDoesNotExist() async {
@@ -483,14 +477,13 @@ f(FutureOr<int> a, Completer<int> b) {}
class A {}
''');
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
import 'a.dart';
import 'b.dart';
// ^^^^^^^^
// [diag.uriDoesNotExist] Target of URI doesn't exist: 'b.dart'.
void f(A _) {}
''',
[error(diag.uriDoesNotExist, 24, 8)],
);
''');
}
test_part_inside_unnecessary() async {
@@ -2,73 +2,69 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnnecessaryNanComparisonTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UnnecessaryNanComparisonTest extends PubPackageResolutionTest {
test_constantPattern() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
void f(List<double> list) {
switch (list) {
case [double.nan]:
// ^^^^^^^^^^
// [diag.unnecessaryNanComparisonFalse] A double can't equal 'double.nan', so the condition is always 'false'.
}
}
''',
[error(diag.unnecessaryNanComparisonFalse, 56, 10)],
);
''');
}
test_equal() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
void f(double d) {
d == double.nan;
// ^^^^^^^^^^^^^
// [diag.unnecessaryNanComparisonFalse] A double can't equal 'double.nan', so the condition is always 'false'.
}
''',
[error(diag.unnecessaryNanComparisonFalse, 23, 13)],
);
''');
}
test_equal_nanFirst() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
void f(double d) {
double.nan == d;
//^^^^^^^^^^^^^
// [diag.unnecessaryNanComparisonFalse] A double can't equal 'double.nan', so the condition is always 'false'.
}
''',
[error(diag.unnecessaryNanComparisonFalse, 21, 13)],
);
''');
}
test_notEqual() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
void f(double d) {
d != double.nan;
// ^^^^^^^^^^^^^
// [diag.unnecessaryNanComparisonTrue] A double can't equal 'double.nan', so the condition is always 'true'.
}
''',
[error(diag.unnecessaryNanComparisonTrue, 23, 13)],
);
''');
}
test_notEqual_nanFirst() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
void f(double d) {
double.nan != d;
//^^^^^^^^^^^^^
// [diag.unnecessaryNanComparisonTrue] A double can't equal 'double.nan', so the condition is always 'true'.
}
''',
[error(diag.unnecessaryNanComparisonTrue, 21, 13)],
);
''');
}
}
@@ -2,38 +2,38 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnnecessaryNoSuchMethodTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UnnecessaryNoSuchMethodTest extends PubPackageResolutionTest {
test_blockBody() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
noSuchMethod(x) => super.noSuchMethod(x);
}
class B extends A {
mmm();
noSuchMethod(y) {
//^^^^^^^^^^^^
// [diag.unnecessaryNoSuchMethod] Unnecessary 'noSuchMethod' declaration.
return super.noSuchMethod(y);
}
}
''',
[error(diag.unnecessaryNoSuchMethod, 87, 12)],
);
''');
}
test_blockBody_notReturnStatement() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
noSuchMethod(x) => super.noSuchMethod(x);
}
@@ -47,7 +47,7 @@ class B extends A {
}
test_blockBody_notSingleStatement() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
noSuchMethod(x) => super.noSuchMethod(x);
}
@@ -62,22 +62,21 @@ class B extends A {
}
test_expressionBody() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
noSuchMethod(x) => super.noSuchMethod(x);
}
class B extends A {
mmm();
noSuchMethod(y) => super.noSuchMethod(y);
//^^^^^^^^^^^^
// [diag.unnecessaryNoSuchMethod] Unnecessary 'noSuchMethod' declaration.
}
''',
[error(diag.unnecessaryNoSuchMethod, 87, 12)],
);
''');
}
test_expressionBody_notNoSuchMethod() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
noSuchMethod(x) => super.noSuchMethod(x);
}
@@ -89,7 +88,7 @@ class B extends A {
}
test_expressionBody_notSuper() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
noSuchMethod(x) => super.noSuchMethod(x);
}
@@ -2,60 +2,58 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnnecessaryNonNullAssertionTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UnnecessaryNonNullAssertionTest extends PubPackageResolutionTest {
test_nonNull_function() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
void g() {}
void f() {
g!();
// ^
// [diag.unnecessaryNonNullAssertion] The '!' will have no effect because the receiver can't be null.
}
''',
[error(diag.unnecessaryNonNullAssertion, 27, 1)],
);
''');
}
test_nonNull_method() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class A {
static void foo() {}
}
void f() {
A.foo!();
// ^
// [diag.unnecessaryNonNullAssertion] The '!' will have no effect because the receiver can't be null.
}
''',
[error(diag.unnecessaryNonNullAssertion, 54, 1)],
);
''');
}
test_nonNull_parameter() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f(int x) {
x!;
// ^
// [diag.unnecessaryNonNullAssertion] The '!' will have no effect because the receiver can't be null.
}
''',
[error(diag.unnecessaryNonNullAssertion, 14, 1)],
);
''');
}
test_nullable() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
f(int? x) {
x!;
}
@@ -2,101 +2,91 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnnecessaryNullAssertPatternTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UnnecessaryNullAssertPatternTest extends PubPackageResolutionTest {
Future<void> test_interfaceType_nonNullable() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
void f(int x) {
if (x case var a!) {}
// ^
// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used.
// ^
// [diag.unnecessaryNullAssertPattern] The null-assert pattern will have no effect because the matched type isn't nullable.
}
''',
[
error(diag.unusedLocalVariable, 33, 1),
error(diag.unnecessaryNullAssertPattern, 34, 1),
],
);
''');
}
Future<void> test_interfaceType_nullable() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
void f(int? x) {
if (x case var a!) {}
// ^
// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used.
}
''',
[error(diag.unusedLocalVariable, 34, 1)],
);
''');
}
Future<void> test_invalidType_nonNullable() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
UnknownType getValue() => UnknownType();
// [diag.undefinedClass][column 1][length 11] Undefined class 'UnknownType'.
// ^^^^^^^^^^^
// [diag.undefinedFunction] The function 'UnknownType' isn't defined.
void f() {
if (getValue() case final valueX!) {
print(valueX);
}
}
''',
[
error(diag.undefinedClass, 0, 11),
error(diag.undefinedFunction, 26, 11),
],
);
''');
}
Future<void> test_invalidType_nullable() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
UnknownType? getValue() => null;
// [diag.undefinedClass][column 1][length 11] Undefined class 'UnknownType'.
void f() {
if (getValue() case final valueX!) {
print(valueX);
}
}
''',
[error(diag.undefinedClass, 0, 11)],
);
''');
}
Future<void> test_typeParameter_nonNullable() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class A<T extends num> {
void f(T x) {
if (x case var a!) {}
// ^
// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used.
// ^
// [diag.unnecessaryNullAssertPattern] The null-assert pattern will have no effect because the matched type isn't nullable.
}
}
''',
[
error(diag.unusedLocalVariable, 60, 1),
error(diag.unnecessaryNullAssertPattern, 61, 1),
],
);
''');
}
Future<void> test_typeParameter_nullable() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class A<T> {
void f(T x) {
if (x case var a!) {}
// ^
// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used.
}
}
''',
[error(diag.unusedLocalVariable, 48, 1)],
);
''');
}
}
@@ -2,101 +2,91 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnnecessaryNullCheckPatternTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UnnecessaryNullCheckPatternTest extends PubPackageResolutionTest {
Future<void> test_interfaceType_nonNullable() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
void f(int x) {
if (x case var a?) {}
// ^
// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used.
// ^
// [diag.unnecessaryNullCheckPattern] The null-check pattern will have no effect because the matched type isn't nullable.
}
''',
[
error(diag.unusedLocalVariable, 33, 1),
error(diag.unnecessaryNullCheckPattern, 34, 1),
],
);
''');
}
Future<void> test_interfaceType_nullable() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
void f(int? x) {
if (x case var a?) {}
// ^
// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used.
}
''',
[error(diag.unusedLocalVariable, 34, 1)],
);
''');
}
Future<void> test_invalidType_nonNullable() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
UnknownType getValue() => UnknownType();
// [diag.undefinedClass][column 1][length 11] Undefined class 'UnknownType'.
// ^^^^^^^^^^^
// [diag.undefinedFunction] The function 'UnknownType' isn't defined.
void f() {
if (getValue() case final valueX?) {
print(valueX);
}
}
''',
[
error(diag.undefinedClass, 0, 11),
error(diag.undefinedFunction, 26, 11),
],
);
''');
}
Future<void> test_invalidType_nullable() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
UnknownType? getValue() => null;
// [diag.undefinedClass][column 1][length 11] Undefined class 'UnknownType'.
void f() {
if (getValue() case final valueX?) {
print(valueX);
}
}
''',
[error(diag.undefinedClass, 0, 11)],
);
''');
}
Future<void> test_typeParameter_nonNullable() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class A<T extends num> {
void f(T x) {
if (x case var a?) {}
// ^
// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used.
// ^
// [diag.unnecessaryNullCheckPattern] The null-check pattern will have no effect because the matched type isn't nullable.
}
}
''',
[
error(diag.unusedLocalVariable, 60, 1),
error(diag.unnecessaryNullCheckPattern, 61, 1),
],
);
''');
}
Future<void> test_typeParameter_nullable() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class A<T> {
void f(T x) {
if (x case var a?) {}
// ^
// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used.
}
}
''',
[error(diag.unusedLocalVariable, 48, 1)],
);
''');
}
}
@@ -2,22 +2,23 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnnecessaryNullComparisonFalseTest);
defineReflectiveTests(UnnecessaryNullComparisonTrueTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UnnecessaryNullComparisonFalseTest extends PubPackageResolutionTest {
test_equal_intLiteral() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
f(int a, int? b) {
a == 0;
0 == a;
@@ -28,22 +29,20 @@ f(int a, int? b) {
}
test_equal_notNullable() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f(int a) {
a == null;
// ^^^^^^^
// [diag.unnecessaryNullComparisonNeverNullFalse] The operand can't be 'null', so the condition is always 'false'.
null == a;
//^^^^^^^
// [diag.unnecessaryNullComparisonNeverNullFalse] The operand can't be 'null', so the condition is always 'false'.
}
''',
[
error(diag.unnecessaryNullComparisonNeverNullFalse, 15, 7),
error(diag.unnecessaryNullComparisonNeverNullFalse, 26, 7),
],
);
''');
}
test_equal_nullable() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
f(int? a) {
a == null;
null == a;
@@ -52,68 +51,62 @@ f(int? a) {
}
test_implicitlyAssigned_false() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f() {
int? i;
i != null;
//^^^^
// [diag.unnecessaryNullComparisonAlwaysNullFalse] The operand must be 'null', so the condition is always 'false'.
null != i;
// ^^^^
// [diag.unnecessaryNullComparisonAlwaysNullFalse] The operand must be 'null', so the condition is always 'false'.
}
''',
[
error(diag.unnecessaryNullComparisonAlwaysNullFalse, 18, 4),
error(diag.unnecessaryNullComparisonAlwaysNullFalse, 36, 4),
],
);
''');
}
test_implicitlyAssigned_true() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f() {
int? i;
i == null;
//^^^^
// [diag.unnecessaryNullComparisonAlwaysNullTrue] The operand must be 'null', so the condition is always 'true'.
null == i;
// ^^^^
// [diag.unnecessaryNullComparisonAlwaysNullTrue] The operand must be 'null', so the condition is always 'true'.
}
''',
[
error(diag.unnecessaryNullComparisonAlwaysNullTrue, 18, 4),
error(diag.unnecessaryNullComparisonAlwaysNullTrue, 36, 4),
],
);
''');
}
}
@reflectiveTest
class UnnecessaryNullComparisonTrueTest extends PubPackageResolutionTest {
test_equal_invalid_nonNull() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f(Unresolved o) {
//^^^^^^^^^^
// [diag.undefinedClass] Undefined class 'Unresolved'.
int? i = o.nonNull;
i == null;
null == i;
}
''',
[error(diag.undefinedClass, 2, 10)],
);
''');
}
test_equal_invalid_nullable() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f(Unresolved o) {
//^^^^^^^^^^
// [diag.undefinedClass] Undefined class 'Unresolved'.
int? i = o.nullable;
i == null;
null == i;
}
''',
[error(diag.undefinedClass, 2, 10)],
);
''');
}
test_notEqual_intLiteral() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
f(int a, int? b) {
a != 0;
0 != a;
@@ -124,48 +117,44 @@ f(int a, int? b) {
}
test_notEqual_invalid_nonNull() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f(Unresolved o) {
//^^^^^^^^^^
// [diag.undefinedClass] Undefined class 'Unresolved'.
int? i = o.nonNull;
i != null;
null != i;
}
''',
[error(diag.undefinedClass, 2, 10)],
);
''');
}
test_notEqual_invalid_nullable() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f(Unresolved o) {
//^^^^^^^^^^
// [diag.undefinedClass] Undefined class 'Unresolved'.
int? i = o.nullable;
i != null;
null != i;
}
''',
[error(diag.undefinedClass, 2, 10)],
);
''');
}
test_notEqual_notNullable() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f(int a) {
a != null;
// ^^^^^^^
// [diag.unnecessaryNullComparisonNeverNullTrue] The operand can't be 'null', so the condition is always 'true'.
null != a;
//^^^^^^^
// [diag.unnecessaryNullComparisonNeverNullTrue] The operand can't be 'null', so the condition is always 'true'.
}
''',
[
error(diag.unnecessaryNullComparisonNeverNullTrue, 15, 7),
error(diag.unnecessaryNullComparisonNeverNullTrue, 26, 7),
],
);
''');
}
test_notEqual_nullable() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
f(int? a) {
a != null;
null != a;
@@ -2,78 +2,74 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnnecessaryQuestionMarkTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UnnecessaryQuestionMarkTest extends PubPackageResolutionTest {
test_dynamic() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
dynamic a;
''');
}
test_dynamicQuestionMark() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
dynamic? a;
''',
[error(diag.unnecessaryQuestionMark, 7, 1)],
);
// ^
// [diag.unnecessaryQuestionMark] The '?' is unnecessary because 'dynamic' is nullable without it.
''');
}
test_dynamicQuestionMark_inVariableDeclarationPattern() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
void f(List<Object> a) {
var [dynamic? _] = a;
// ^
// [diag.unnecessaryQuestionMark] The '?' is unnecessary because 'dynamic' is nullable without it.
}
''',
[error(diag.unnecessaryQuestionMark, 39, 1)],
);
''');
}
test_Null() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
Null a;
''');
}
test_NullQuestionMark() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
Null? a;
''',
[error(diag.unnecessaryQuestionMark, 4, 1)],
);
// ^
// [diag.unnecessaryQuestionMark] The '?' is unnecessary because 'Null' is nullable without it.
''');
}
test_NullQuestionMark_inCastPattern() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
void f(Object a) {
switch (a) {
case var _ as Null?:
// ^^^^^
// [diag.patternNeverMatchesValueType] The matched value type 'Object' can never match the required type 'Null'.
// ^
// [diag.unnecessaryQuestionMark] The '?' is unnecessary because 'Null' is nullable without it.
}
}
''',
[
error(diag.patternNeverMatchesValueType, 52, 5),
error(diag.unnecessaryQuestionMark, 56, 1),
],
);
''');
}
test_typeAliasQuestionMark() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
typedef n = Null;
n? a;
''');
@@ -2,21 +2,22 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnnecessarySetLiteralTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UnnecessarySetLiteralTest extends PubPackageResolutionTest {
test_blockFunctionBody() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void g(void Function() fun) {}
void f() {
@@ -26,7 +27,7 @@ void f() {
}
test_expressionFunctionBody_dynamic() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void g(Function() fun) {}
void f() {
@@ -36,7 +37,7 @@ void f() {
}
test_expressionFunctionBody_future() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void g(Future Function() fun) {}
void f() {
@@ -46,7 +47,7 @@ void f() {
}
test_expressionFunctionBody_future_object() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void g(Future<Object> Function() fun) {}
void f() {
@@ -56,20 +57,19 @@ void f() {
}
test_expressionFunctionBody_future_void() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void g(Future<void> Function() fun) {}
void f() {
g(() async => {1});
// ^^^
// [diag.unnecessarySetLiteral] Braces unnecessarily wrap this expression in a set literal.
}
''',
[error(diag.unnecessarySetLiteral, 67, 3)],
);
''');
}
test_expressionFunctionBody_futureOr() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'dart:async';
void g(FutureOr Function() fun) {}
@@ -81,7 +81,7 @@ void f() {
}
test_expressionFunctionBody_futureOr_object() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'dart:async';
void g(FutureOr<Object> Function() fun) {}
@@ -93,22 +93,21 @@ void f() {
}
test_expressionFunctionBody_futureOr_void() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'dart:async';
void g(FutureOr<void> Function() fun) {}
void f() {
g(() async => {1});
// ^^^
// [diag.unnecessarySetLiteral] Braces unnecessarily wrap this expression in a set literal.
}
''',
[error(diag.unnecessarySetLiteral, 91, 3)],
);
''');
}
test_expressionFunctionBody_map() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void g(void Function() fun) {}
void f() {
@@ -118,33 +117,31 @@ void f() {
}
test_expressionFunctionBody_multipleElements() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void g(void Function() fun) {}
void f() {
g(() => {1, 2});
// ^^^^^^
// [diag.unnecessarySetLiteral] Braces unnecessarily wrap this expression in a set literal.
}
''',
[error(diag.unnecessarySetLiteral, 53, 6)],
);
''');
}
test_expressionFunctionBody_multipleElements_statements() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void g(void Function() fun) {}
void f(bool b) {
g(() => {1, if (b) 2 else 3, 4, for (;;) 5},);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// [diag.unnecessarySetLiteral] Braces unnecessarily wrap this expression in a set literal.
}
''',
[error(diag.unnecessarySetLiteral, 59, 35)],
);
''');
}
test_expressionFunctionBody_object() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void g(Object Function() fun) {}
void f() {
@@ -154,33 +151,31 @@ void f() {
}
test_expressionFunctionBody_statement() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void g(void Function(bool) fun) {}
void f() {
g((value) => {if (value) print('')});
// ^^^^^^^^^^^^^^^^^^^^^^
// [diag.unnecessarySetLiteral] Braces unnecessarily wrap this expression in a set literal.
}
''',
[error(diag.unnecessarySetLiteral, 62, 22)],
);
''');
}
test_expressionFunctionBody_void() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void g(void Function() fun) {}
void f() {
g(() => {1});
// ^^^
// [diag.unnecessarySetLiteral] Braces unnecessarily wrap this expression in a set literal.
}
''',
[error(diag.unnecessarySetLiteral, 53, 3)],
);
''');
}
test_expressionFunctionBody_void_empty() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void g(void Function() fun) {}
void f() {
@@ -190,34 +185,33 @@ void f() {
}
test_functionDeclaration_dynamic() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
f() => {1};
''');
}
test_functionDeclaration_future() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
Future f() async => {1};
''');
}
test_functionDeclaration_future_object() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
Future<Object> f() async => {1};
''');
}
test_functionDeclaration_future_void() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
Future<void> f() async => {1};
''',
[error(diag.unnecessarySetLiteral, 26, 3)],
);
// ^^^
// [diag.unnecessarySetLiteral] Braces unnecessarily wrap this expression in a set literal.
''');
}
test_functionDeclaration_futureOr() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'dart:async';
FutureOr f() async => {1};
@@ -225,7 +219,7 @@ FutureOr f() async => {1};
}
test_functionDeclaration_futureOr_object() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'dart:async';
FutureOr<Object> f() async => {1};
@@ -233,61 +227,56 @@ FutureOr<Object> f() async => {1};
}
test_functionDeclaration_futureOr_void() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'dart:async';
FutureOr<void> f() async => {1};
''',
[error(diag.unnecessarySetLiteral, 50, 3)],
);
// ^^^
// [diag.unnecessarySetLiteral] Braces unnecessarily wrap this expression in a set literal.
''');
}
test_functionDeclaration_map() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f() => {1: 2};
''');
}
test_functionDeclaration_multipleElements() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f() => {1, 2};
''',
[error(diag.unnecessarySetLiteral, 12, 6)],
);
// ^^^^^^
// [diag.unnecessarySetLiteral] Braces unnecessarily wrap this expression in a set literal.
''');
}
test_functionDeclaration_multipleElements_statements() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(bool b) => {1, if (b) 2 else 3, 4, for (;;) 5};
''',
[error(diag.unnecessarySetLiteral, 18, 35)],
);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// [diag.unnecessarySetLiteral] Braces unnecessarily wrap this expression in a set literal.
''');
}
test_functionDeclaration_object() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
Object f() => {1};
''');
}
test_functionDeclaration_statement() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(bool value) => {if (value) print('')};
''',
[error(diag.unnecessarySetLiteral, 22, 22)],
);
// ^^^^^^^^^^^^^^^^^^^^^^
// [diag.unnecessarySetLiteral] Braces unnecessarily wrap this expression in a set literal.
''');
}
test_functionDeclaration_void() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f() => {1};
''',
[error(diag.unnecessarySetLiteral, 12, 3)],
);
// ^^^
// [diag.unnecessarySetLiteral] Braces unnecessarily wrap this expression in a set literal.
''');
}
}
@@ -2,42 +2,41 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnnecessaryTypeCheckFalseTest);
defineReflectiveTests(UnnecessaryTypeCheckTrueTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UnnecessaryTypeCheckFalseTest extends PubPackageResolutionTest {
test_null_isNot_Null() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
var b = null is! Null;
''',
[error(diag.unnecessaryTypeCheckFalse, 8, 13)],
);
// ^^^^^^^^^^^^^
// [diag.unnecessaryTypeCheckFalse] Unnecessary type check; the result is always 'false'.
''');
}
test_typeNonNullable_isNot_same() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(int a) {
a is! int;
//^^^^^^^^^
// [diag.unnecessaryTypeCheckFalse] Unnecessary type check; the result is always 'false'.
}
''',
[error(diag.unnecessaryTypeCheckFalse, 18, 9)],
);
''');
}
test_typeNonNullable_isNot_subtype() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(num a) {
a is! int;
}
@@ -45,29 +44,27 @@ void f(num a) {
}
test_typeNonNullable_isNot_supertype() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(int a) {
a is! num;
//^^^^^^^^^
// [diag.unnecessaryTypeCheckFalse] Unnecessary type check; the result is always 'false'.
}
''',
[error(diag.unnecessaryTypeCheckFalse, 18, 9)],
);
''');
}
test_typeNullable_isNot_same() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(int? a) {
a is! int?;
//^^^^^^^^^^
// [diag.unnecessaryTypeCheckFalse] Unnecessary type check; the result is always 'false'.
}
''',
[error(diag.unnecessaryTypeCheckFalse, 19, 10)],
);
''');
}
test_typeNullable_isNot_same_nonNullable() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(int? a) {
a is! int;
}
@@ -75,7 +72,7 @@ void f(int? a) {
}
test_typeNullable_isNot_subtype() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(num? a) {
a is! int?;
}
@@ -83,7 +80,7 @@ void f(num? a) {
}
test_typeNullable_isNot_subtype_nonNullable() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(num? a) {
a is! int;
}
@@ -91,18 +88,17 @@ void f(num? a) {
}
test_typeNullable_isNot_supertype() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(int? a) {
a is! num?;
//^^^^^^^^^^
// [diag.unnecessaryTypeCheckFalse] Unnecessary type check; the result is always 'false'.
}
''',
[error(diag.unnecessaryTypeCheckFalse, 19, 10)],
);
''');
}
test_typeNullable_isNot_supertype_nonNullable() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(int? a) {
a is! num;
}
@@ -110,18 +106,17 @@ void f(int? a) {
}
test_typeParameter_isNot_dynamic() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f<T>(T a) {
a is! dynamic;
//^^^^^^^^^^^^^
// [diag.unnecessaryTypeCheckFalse] Unnecessary type check; the result is always 'false'.
}
''',
[error(diag.unnecessaryTypeCheckFalse, 19, 13)],
);
''');
}
test_typeParameter_isNot_object() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f<T>(T a) {
a is! Object;
}
@@ -129,74 +124,68 @@ void f<T>(T a) {
}
test_typeParameter_isNot_objectQuestion() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f<T>(T a) {
a is! Object?;
//^^^^^^^^^^^^^
// [diag.unnecessaryTypeCheckFalse] Unnecessary type check; the result is always 'false'.
}
''',
[error(diag.unnecessaryTypeCheckFalse, 19, 13)],
);
''');
}
}
@reflectiveTest
class UnnecessaryTypeCheckTrueTest extends PubPackageResolutionTest {
test_expressionInvalidType() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(A a) {
// ^
// [diag.undefinedClass] Undefined class 'A'.
a is num;
}
''',
[error(diag.undefinedClass, 7, 1)],
);
''');
}
test_null_is_Null() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
var b = null is Null;
''',
[error(diag.unnecessaryTypeCheckTrue, 8, 12)],
);
// ^^^^^^^^^^^^
// [diag.unnecessaryTypeCheckTrue] Unnecessary type check; the result is always 'true'.
''');
}
test_type_is_dynamic() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(int a) {
a is dynamic;
//^^^^^^^^^^^^
// [diag.unnecessaryTypeCheckTrue] Unnecessary type check; the result is always 'true'.
}
''',
[error(diag.unnecessaryTypeCheckTrue, 18, 12)],
);
''');
}
test_type_is_unresolved() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(int a) {
a is Unresolved;
// ^^^^^^^^^^
// [diag.typeTestWithUndefinedName] The name 'Unresolved' isn't defined, so it can't be used in an 'is' expression.
}
''',
[error(diag.typeTestWithUndefinedName, 23, 10)],
);
''');
}
test_typeNonNullable_is_same() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(int a) {
a is int;
//^^^^^^^^
// [diag.unnecessaryTypeCheckTrue] Unnecessary type check; the result is always 'true'.
}
''',
[error(diag.unnecessaryTypeCheckTrue, 18, 8)],
);
''');
}
test_typeNonNullable_is_subtype() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(num a) {
a is int;
}
@@ -204,29 +193,27 @@ void f(num a) {
}
test_typeNonNullable_is_supertype() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(int a) {
a is num;
//^^^^^^^^
// [diag.unnecessaryTypeCheckTrue] Unnecessary type check; the result is always 'true'.
}
''',
[error(diag.unnecessaryTypeCheckTrue, 18, 8)],
);
''');
}
test_typeNullable_is_same() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(int? a) {
a is int?;
//^^^^^^^^^
// [diag.unnecessaryTypeCheckTrue] Unnecessary type check; the result is always 'true'.
}
''',
[error(diag.unnecessaryTypeCheckTrue, 19, 9)],
);
''');
}
test_typeNullable_is_same_nonNullable() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(int? a) {
a is int;
}
@@ -234,7 +221,7 @@ void f(int? a) {
}
test_typeNullable_is_subtype() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(num? a) {
a is int?;
}
@@ -242,7 +229,7 @@ void f(num? a) {
}
test_typeNullable_is_subtype_nonNullable() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(num? a) {
a is int;
}
@@ -250,18 +237,17 @@ void f(num? a) {
}
test_typeNullable_is_supertype() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(int? a) {
a is num?;
//^^^^^^^^^
// [diag.unnecessaryTypeCheckTrue] Unnecessary type check; the result is always 'true'.
}
''',
[error(diag.unnecessaryTypeCheckTrue, 19, 9)],
);
''');
}
test_typeNullable_is_supertype_nonNullable() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(int? a) {
a is num;
}
@@ -269,18 +255,17 @@ void f(int? a) {
}
test_typeParameter_is_dynamic() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f<T>(T a) {
a is dynamic;
//^^^^^^^^^^^^
// [diag.unnecessaryTypeCheckTrue] Unnecessary type check; the result is always 'true'.
}
''',
[error(diag.unnecessaryTypeCheckTrue, 19, 12)],
);
''');
}
test_typeParameter_is_object() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f<T>(T a) {
a is Object;
}
@@ -288,13 +273,12 @@ void f<T>(T a) {
}
test_typeParameter_is_objectQuestion() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f<T>(T a) {
a is Object?;
//^^^^^^^^^^^^
// [diag.unnecessaryTypeCheckTrue] Unnecessary type check; the result is always 'true'.
}
''',
[error(diag.unnecessaryTypeCheckTrue, 19, 12)],
);
''');
}
}
@@ -2,21 +2,22 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnnecessaryWildcardPatternTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UnnecessaryWildcardPatternTest extends PubPackageResolutionTest {
test_forStatement_ForEachPartsWithPattern() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(List<int> values) {
for (var (_) in values) {}
}
@@ -24,40 +25,37 @@ void f(List<int> values) {
}
test_ifCase_notRequired_logicalAnd_left() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
if (x case _ && 0) {}
// ^
// [diag.unnecessaryWildcardPattern] Unnecessary wildcard pattern.
}
''',
[error(diag.unnecessaryWildcardPattern, 33, 1)],
);
''');
}
test_ifCase_notRequired_logicalAnd_right() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
if (x case 0 && _) {}
// ^
// [diag.unnecessaryWildcardPattern] Unnecessary wildcard pattern.
}
''',
[error(diag.unnecessaryWildcardPattern, 38, 1)],
);
''');
}
test_ifCase_notRequired_parenthesizedPattern_logicalAnd() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
if (x case (_) && 0) {}
// ^
// [diag.unnecessaryWildcardPattern] Unnecessary wildcard pattern.
}
''',
[error(diag.unnecessaryWildcardPattern, 34, 1)],
);
''');
}
test_ifCase_notRequired_typed_promotes() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
if (x case int _ && > 0) {}
}
@@ -65,18 +63,17 @@ void f(Object? x) {
}
test_ifCase_notRequired_typed_sameMatchedType() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(int x) {
if (x case int _ && > 0) {}
// ^^^^^
// [diag.unnecessaryWildcardPattern] Unnecessary wildcard pattern.
}
''',
[error(diag.unnecessaryWildcardPattern, 29, 5)],
);
''');
}
test_ifCase_required_castPattern() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
if (x case _ as int) {}
}
@@ -84,7 +81,7 @@ void f(Object? x) {
}
test_ifCase_required_listPattern() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
if (x case [_, 0]) {}
}
@@ -92,19 +89,18 @@ void f(Object? x) {
}
test_ifCase_required_logicalOr_left() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
if (x case _ || 0) {}
// ^^^^
// [diag.deadCode] Dead code.
}
''',
[error(diag.deadCode, 35, 4)],
);
''');
}
/// Although this makes the _left_ side useless.
test_ifCase_required_logicalOr_right() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
if (x case 0 || _) {}
}
@@ -112,7 +108,7 @@ void f(Object? x) {
}
test_ifCase_required_mapPattern() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
if (x case {'foo': _, 'bar': 0}) {}
}
@@ -120,7 +116,7 @@ void f(Object? x) {
}
test_ifCase_required_nullAssert() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
if (x case _!) {}
}
@@ -128,7 +124,7 @@ void f(Object? x) {
}
test_ifCase_required_nullCheck() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
if (x case _?) {}
}
@@ -136,7 +132,7 @@ void f(Object? x) {
}
test_ifCase_required_objectPattern() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
if (x case A(foo: _, bar: _?)) {}
if (x case A(bar: _?, foo: _)) {}
@@ -150,7 +146,7 @@ class A {
}
test_ifCase_required_recordPattern() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
if (x case (_, 0)) {}
}
@@ -158,21 +154,20 @@ void f(Object? x) {
}
test_switchExpression_logicalAnd() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
(switch (x) {
0 && _ => 0,
// ^
// [diag.unnecessaryWildcardPattern] Unnecessary wildcard pattern.
_ => 1,
});
}
''',
[error(diag.unnecessaryWildcardPattern, 45, 1)],
);
''');
}
test_switchExpression_topPattern() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
(switch (x) {
_ => 0,
@@ -182,21 +177,20 @@ void f(Object? x) {
}
test_switchStatement_logicalAnd() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
switch (x) {
case 0 && _:
// ^
// [diag.unnecessaryWildcardPattern] Unnecessary wildcard pattern.
break;
}
}
''',
[error(diag.unnecessaryWildcardPattern, 49, 1)],
);
''');
}
test_switchStatement_topPattern() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
switch (x) {
case _:
@@ -2,43 +2,38 @@
// 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/error/error.dart';
import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnqualifiedReferenceToNonLocalStaticMemberTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UnqualifiedReferenceToNonLocalStaticMemberTest
extends PubPackageResolutionTest {
DiagnosticCode get _errorCode =>
diag.unqualifiedReferenceToNonLocalStaticMember;
test_getter() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
static int get a => 0;
}
class B extends A {
int b() {
return a;
// ^
// [diag.unqualifiedReferenceToNonLocalStaticMember] Static members from supertypes must be qualified by the name of the defining type.
}
}
''',
[error(_errorCode, 80, 1)],
);
''');
}
test_getter_invoke() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class A {
static void Function() get a => () {};
}
@@ -46,16 +41,15 @@ class A {
class B extends A {
void b() {
a();
// ^
// [diag.unqualifiedReferenceToNonLocalStaticMember] Static members from supertypes must be qualified by the name of the defining type.
}
}
''',
[error(_errorCode, 91, 1)],
);
''');
}
test_getter_invokeTarget() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
static int foo = 1;
}
@@ -63,48 +57,45 @@ class A {
class B extends A {
static bar() {
foo.abs();
// ^^^
// [diag.unqualifiedReferenceToNonLocalStaticMember] Static members from supertypes must be qualified by the name of the defining type.
}
}
''',
[error(_errorCode, 76, 3)],
);
''');
}
test_methodTearoff() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class A {
static void a<T>() {}
}
class B extends A {
void b() {
a<int>;
// ^
// [diag.unqualifiedReferenceToNonLocalStaticMember] Static members from supertypes must be qualified by the name of the defining type.
}
}
''',
[error(_errorCode, 73, 1)],
);
''');
}
test_methodTearoff_noTypeArguments() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class A {
static void a() {}
}
class B extends A {
void b() {
a;
// ^
// [diag.unqualifiedReferenceToNonLocalStaticMember] Static members from supertypes must be qualified by the name of the defining type.
}
}
''',
[error(_errorCode, 70, 1)],
);
''');
}
test_readWrite() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
static int get x => 0;
static set x(int _) {}
@@ -112,18 +103,19 @@ class A {
class B extends A {
void f() {
x = 0;
// ^
// [diag.unqualifiedReferenceToNonLocalStaticMember] Static members from supertypes must be qualified by the name of the defining type.
x += 1;
// ^
// [diag.unqualifiedReferenceToNonLocalStaticMember] Static members from supertypes must be qualified by the name of the defining type.
++x;
// ^
// [diag.unqualifiedReferenceToNonLocalStaticMember] Static members from supertypes must be qualified by the name of the defining type.
x++;
// ^
// [diag.unqualifiedReferenceToNonLocalStaticMember] Static members from supertypes must be qualified by the name of the defining type.
}
}
''',
[
error(_errorCode, 99, 1),
error(_errorCode, 110, 1),
error(_errorCode, 124, 1),
error(_errorCode, 131, 1),
],
);
''');
}
}
@@ -2,76 +2,68 @@
// 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/error/error.dart';
import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnqualifiedReferenceToStaticMemberOfExtendedTypeTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UnqualifiedReferenceToStaticMemberOfExtendedTypeTest
extends PubPackageResolutionTest {
DiagnosticCode get _errorCode {
return diag.unqualifiedReferenceToStaticMemberOfExtendedType;
}
test_getter() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class MyClass {
static int get zero => 0;
}
extension MyExtension on MyClass {
void m() {
zero;
// ^^^^
// [diag.unqualifiedReferenceToStaticMemberOfExtendedType] Static members from the extended type or one of its superclasses must be qualified by the name of the defining type.
}
}
''',
[error(_errorCode, 98, 4)],
);
''');
}
test_method() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class MyClass {
static void sm() {}
}
extension MyExtension on MyClass {
void m() {
sm();
// ^^
// [diag.unqualifiedReferenceToStaticMemberOfExtendedType] Static members from the extended type or one of its superclasses must be qualified by the name of the defining type.
}
}
''',
[error(_errorCode, 92, 2)],
);
''');
}
test_methodTearoff() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class MyClass {
static void sm<T>() {}
}
extension MyExtension on MyClass {
void m() {
sm<int>;
// ^^
// [diag.unqualifiedReferenceToStaticMemberOfExtendedType] Static members from the extended type or one of its superclasses must be qualified by the name of the defining type.
}
}
''',
[error(_errorCode, 95, 2)],
);
''');
}
test_readWrite() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class MyClass {
static int get x => 0;
static set x(int _) {}
@@ -80,18 +72,19 @@ class MyClass {
extension MyExtension on MyClass {
void f() {
x = 0;
// ^
// [diag.unqualifiedReferenceToStaticMemberOfExtendedType] Static members from the extended type or one of its superclasses must be qualified by the name of the defining type.
x += 1;
// ^
// [diag.unqualifiedReferenceToStaticMemberOfExtendedType] Static members from the extended type or one of its superclasses must be qualified by the name of the defining type.
++x;
// ^
// [diag.unqualifiedReferenceToStaticMemberOfExtendedType] Static members from the extended type or one of its superclasses must be qualified by the name of the defining type.
x++;
// ^
// [diag.unqualifiedReferenceToStaticMemberOfExtendedType] Static members from the extended type or one of its superclasses must be qualified by the name of the defining type.
}
}
''',
[
error(_errorCode, 121, 1),
error(_errorCode, 132, 1),
error(_errorCode, 146, 1),
error(_errorCode, 153, 1),
],
);
''');
}
}
@@ -2,15 +2,16 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnreachableSwitchCaseTest_SwitchExpression);
defineReflectiveTests(UnreachableSwitchCaseTest_SwitchStatement);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@@ -18,42 +19,41 @@ main() {
class UnreachableSwitchCaseTest_SwitchExpression
extends PubPackageResolutionTest {
test_bool_false_true_false() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
Object f(bool x) {
return switch (x) {
false => 0,
true => 1,
false => 2,
// ^^
// [diag.unreachableSwitchCase] This case is covered by the previous cases.
};
}
''',
[error(diag.unreachableSwitchCase, 82, 2)],
);
''');
}
test_bool_wildcard_true_false() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
Object f(bool x) {
return switch (x) {
_ => 0,
true => 1,
// ^^^^^^^^^
// [diag.deadCode] Dead code.
// ^^
// [diag.unreachableSwitchCase] This case is covered by the previous cases.
false => 2,
// ^^^^^^^^^^
// [diag.deadCode] Dead code.
// ^^
// [diag.unreachableSwitchCase] This case is covered by the previous cases.
};
}
''',
[
error(diag.deadCode, 57, 9),
error(diag.unreachableSwitchCase, 62, 2),
error(diag.deadCode, 72, 10),
error(diag.unreachableSwitchCase, 78, 2),
],
);
''');
}
test_guarded_reachable() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
enum E { e1, e2 }
Object f(E e, bool b) => switch (e) {
E.e1 when b => 0,
@@ -64,31 +64,29 @@ Object f(E e, bool b) => switch (e) {
}
test_guarded_unreachable() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E { e1, e2 }
Object f(E e, bool b) => switch (e) {
E.e1 => 0,
E.e2 => 1,
E.e1 when b => 2,
// ^^
// [diag.unreachableSwitchCase] This case is covered by the previous cases.
};
''',
[error(diag.unreachableSwitchCase, 96, 2)],
);
''');
}
test_unresolved_wildcard() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
int f(Object? x) {
return switch (x) {
Unresolved() => 0,
// ^^^^^^^^^^
// [diag.undefinedClass] Undefined class 'Unresolved'.
_ => -1,
};
}
''',
[error(diag.undefinedClass, 45, 10)],
);
''');
}
}
@@ -96,59 +94,56 @@ int f(Object? x) {
class UnreachableSwitchCaseTest_SwitchStatement
extends PubPackageResolutionTest {
test_bool() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(bool x) {
switch (x) {
case false:
case true:
case false:
// ^^^^
// [diag.unreachableSwitchCase] This case is covered by the previous cases.
break;
}
}
''',
[error(diag.unreachableSwitchCase, 67, 4)],
);
''');
}
test_const_unresolvedIdentifier_const() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
switch (x) {
case 0:
break;
case unresolved:
// ^^^^^^^^^^
// [diag.undefinedIdentifier] Undefined name 'unresolved'.
break;
case 2:
break;
};
}
''',
[error(diag.undefinedIdentifier, 69, 10)],
);
''');
}
test_const_unresolvedObject_const() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
switch (x) {
case 0:
break;
case Unresolved():
// ^^^^^^^^^^
// [diag.undefinedClass] Undefined class 'Unresolved'.
break;
case 2:
break;
};
}
''',
[error(diag.undefinedClass, 69, 10)],
);
''');
}
test_guarded_reachable() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
enum E { e1, e2 }
void f(E e, bool b) {
switch (e) {
@@ -164,8 +159,7 @@ void f(E e, bool b) {
}
test_guarded_unreachable() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E { e1, e2 }
void f(E e, bool b) {
switch (e) {
@@ -174,34 +168,33 @@ void f(E e, bool b) {
case E.e2:
break;
case E.e1 when b:
// ^^^^
// [diag.unreachableSwitchCase] This case is covered by the previous cases.
break;
}
}
''',
[error(diag.unreachableSwitchCase, 115, 4)],
);
''');
}
test_typeCheck_exact() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(int x) {
switch (x) {
case int():
break;
case int():
// ^^^^
// [diag.deadCode] Dead code.
// [diag.unreachableSwitchCase] This case is covered by the previous cases.
case int():
// ^^^^
// [diag.deadCode] Dead code.
// [diag.unreachableSwitchCase] This case is covered by the previous cases.
break;
// ^^^^^^
// [diag.deadCode] Dead code.
}
}
''',
[
error(diag.deadCode, 64, 4),
error(diag.unreachableSwitchCase, 64, 4),
error(diag.deadCode, 80, 4),
error(diag.unreachableSwitchCase, 80, 4),
error(diag.deadCode, 98, 6),
],
);
''');
}
}
@@ -2,38 +2,37 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnreachableSwitchDefaultTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UnreachableSwitchDefaultTest extends PubPackageResolutionTest {
test_bool() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(bool x) {
switch (x) {
case false:
case true:
default:
// ^^^^^^^
// [diag.unreachableSwitchDefault] This default clause is covered by the previous cases.
break;
}
}
''',
[error(diag.unreachableSwitchDefault, 67, 7)],
);
''');
}
test_enum() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E { e1, e2 }
String f(E e) {
@@ -43,12 +42,12 @@ String f(E e) {
case E.e2:
return 'e2';
default:
// ^^^^^^^
// [diag.unreachableSwitchDefault] This default clause is covered by the previous cases.
return 'Some other value of E (impossible)';
}
}
''',
[error(diag.unreachableSwitchDefault, 122, 7)],
);
''');
}
test_not_always_exhaustive() async {
@@ -56,7 +55,7 @@ String f(E e) {
// isn't reported, because flow analysis might not understand that the
// switch cases fully exhaust the switch, so removing the default clause
// might result in spurious errors.
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
String f(List x) {
switch (x) {
case []:
@@ -71,8 +70,7 @@ String f(List x) {
}
test_sealed_class() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
sealed class A {}
class B extends A {}
class C extends A {}
@@ -84,11 +82,11 @@ String f(A x) {
case C():
return 'C';
default:
// ^^^^^^^
// [diag.unreachableSwitchDefault] This default clause is covered by the previous cases.
return 'Some other subclass of A (impossible)';
}
}
''',
[error(diag.unreachableSwitchDefault, 160, 7)],
);
''');
}
}
@@ -2,47 +2,46 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnusedCatchClauseTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UnusedCatchClauseTest extends PubPackageResolutionTest {
test_on_unusedException() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
f() {
try {
} on String catch (exception) {
// ^^^^^^^^^
// [diag.unusedCatchClause] The exception variable 'exception' isn't used, so the 'catch' clause can be removed.
}
}
''',
[error(diag.unusedCatchClause, 35, 9)],
);
''');
}
test_on_unusedStack_underscores() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
f() {
try {
} on String catch (exception, __) {
// ^^
// [diag.unusedCatchStack] The stack trace variable '__' isn't used and can be removed.
}
}
''',
[error(diag.unusedCatchStack, 46, 2)],
);
''');
}
test_on_unusedStack_wildcard() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
f() {
try {
} on String catch (exception, _) {
@@ -52,7 +51,7 @@ f() {
}
test_on_usedException() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
f() {
try {
} on String catch (exception) {
@@ -63,7 +62,7 @@ f() {
}
test_unusedException() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
f() {
try {
} catch (exception) {
@@ -73,7 +72,7 @@ f() {
}
test_unusedException_underscores() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
f() {
try {
} catch (__) {
@@ -83,7 +82,7 @@ f() {
}
test_unusedException_wildcard() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
f() {
try {
} catch (_) {
@@ -93,7 +92,7 @@ f() {
}
test_usedException() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
f() {
try {
} catch (exception) {
@@ -2,34 +2,34 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnusedCatchStackTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UnusedCatchStackTest extends PubPackageResolutionTest {
test_on_unusedStack() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
main() {
try {
} on String catch (exception, stackTrace) {
// ^^^^^^^^^^
// [diag.unusedCatchStack] The stack trace variable 'stackTrace' isn't used and can be removed.
}
}
''',
[error(diag.unusedCatchStack, 49, 10)],
);
''');
}
test_on_usedStack() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
main() {
try {
} on String catch (exception, stackTrace) {
@@ -40,20 +40,19 @@ main() {
}
test_unusedStack() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
main() {
try {
} catch (exception, stackTrace) {
// ^^^^^^^^^^
// [diag.unusedCatchStack] The stack trace variable 'stackTrace' isn't used and can be removed.
}
}
''',
[error(diag.unusedCatchStack, 39, 10)],
);
''');
}
test_usedStack() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
main() {
try {
} catch (exception, stackTrace) {
File diff suppressed because it is too large Load Diff
@@ -2,21 +2,22 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnusedFieldFromPrimaryConstructorTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UnusedFieldFromPrimaryConstructorTest extends PubPackageResolutionTest {
test_isUsed_class_declaringFormal_requiredPositional() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A(final int _i) {
int get x => _i;
}
@@ -24,13 +25,13 @@ class A(final int _i) {
}
test_isUsed_class_declaringFormal_requiredPositional_public() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A(final int i) {}
''');
}
test_isUsed_class_fieldFormal() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A(this._f) {
int _f;
int get x => _f;
@@ -41,71 +42,65 @@ class A(this._f) {
test_isUsed_extensionType_declaringFormal_requiredPositional() async {
// The representation is not actually used, but we don't report unused
// extension type representation types.
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
extension type A(final int _i) {}
''');
}
test_isUsed_extensionType_declaringFormal_requiredPositional_underscore() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
extension type A(final int _) {}
''');
}
test_notUsed_class_declaringFormal_optionalNamed() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A({final int _i = 0}) {}
''',
[error(diag.unusedFieldFromPrimaryConstructor, 19, 2)],
);
// ^^
// [diag.unusedFieldFromPrimaryConstructor] The value of the field '_i' isn't used.
''');
}
test_notUsed_class_declaringFormal_optionalNamed_functionTyped() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A({final void _f() = _g}) {}
// ^^
// [diag.unusedFieldFromPrimaryConstructor] The value of the field '_f' isn't used.
void _g() {}
''',
[error(diag.unusedFieldFromPrimaryConstructor, 20, 2)],
);
''');
}
test_notUsed_class_declaringFormal_requiredPositional() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A(final int _i) {}
''',
[error(diag.unusedFieldFromPrimaryConstructor, 18, 2)],
);
// ^^
// [diag.unusedFieldFromPrimaryConstructor] The value of the field '_i' isn't used.
''');
}
test_notUsed_class_declaringFormal_requiredPositional_functionTyped() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A(final int _f()) {}
''',
[error(diag.unusedFieldFromPrimaryConstructor, 18, 2)],
);
// ^^
// [diag.unusedFieldFromPrimaryConstructor] The value of the field '_f' isn't used.
''');
}
test_notUsed_class_declaringFormal_requiredPositional_underscore() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A(final int _) {}
''',
[error(diag.unusedFieldFromPrimaryConstructor, 18, 1)],
);
// ^
// [diag.unusedFieldFromPrimaryConstructor] The value of the field '_' isn't used.
''');
}
test_notUsed_class_fieldFormal() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A(this._f) {
int _f;
// ^^
// [diag.unusedField] The value of the field '_f' isn't used.
}
''',
[error(diag.unusedField, 25, 2)],
);
''');
}
}
@@ -2,21 +2,22 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnusedFieldTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UnusedFieldTest extends PubPackageResolutionTest {
test_isUsed_argument() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
int _f = 0;
main() {
@@ -28,7 +29,7 @@ print(x) {}
}
test_isUsed_extensionOnClass() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class Foo {}
extension Bar on Foo {
int baz() => _baz;
@@ -38,7 +39,7 @@ extension Bar on Foo {
}
test_isUsed_extensionOnEnum() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
enum Foo {a, b}
extension Bar on Foo {
int baz() => _baz;
@@ -48,7 +49,7 @@ extension Bar on Foo {
}
test_isUsed_mixin() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
mixin M {
int _f = 0;
}
@@ -59,7 +60,7 @@ class Bar with M {
}
test_isUsed_mixinRestriction() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class Foo {
int _f = 0;
}
@@ -70,7 +71,7 @@ mixin M on Foo {
}
test_isUsed_parameterized_subclass() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A<T extends num> {
T _f;
A._(this._f);
@@ -86,7 +87,7 @@ void main() {
}
test_isUsed_publicStaticField_privateClass() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class _A {
static String f1 = "x";
}
@@ -95,7 +96,7 @@ void main() => print(_A.f1);
}
test_isUsed_publicStaticField_privateExtension() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
extension _A on String {
static String f1 = "x";
}
@@ -104,7 +105,7 @@ void main() => print(_A.f1);
}
test_isUsed_publicStaticField_privateMixin() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
mixin _A {
static String f1 = "x";
}
@@ -113,7 +114,7 @@ void main() => print(_A.f1);
}
test_isUsed_reference_implicitThis() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
int _f = 0;
main() {
@@ -125,7 +126,7 @@ print(x) {}
}
test_isUsed_reference_implicitThis_expressionFunctionBody() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
int _f = 0;
m() => _f;
@@ -134,7 +135,7 @@ class A {
}
test_isUsed_reference_implicitThis_subclass() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
int _f = 0;
main() {
@@ -149,7 +150,7 @@ print(x) {}
}
test_isUsed_reference_qualified_propagatedElement() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
int _f = 0;
}
@@ -162,7 +163,7 @@ print(x) {}
}
test_isUsed_reference_qualified_staticElement() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
int _f = 0;
}
@@ -175,7 +176,7 @@ print(x) {}
}
test_isUsed_reference_qualified_unresolved() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
int _f = 0;
}
@@ -187,7 +188,7 @@ print(x) {}
}
test_isUsed_underscoreField_shadowsLocal() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
var _ = 1;
void m() {
@@ -200,7 +201,7 @@ class A {
// See: https://github.com/dart-lang/sdk/issues/55862
test_isUsed_underscoreField_shadowsParameter() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
var _ = 1;
void m(int? _) {
@@ -211,117 +212,108 @@ class A {
}
test_notUsed_compoundAssign() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
int _f = 0;
// ^^
// [diag.unusedField] The value of the field '_f' isn't used.
main() {
_f += 2;
}
}
''',
[error(diag.unusedField, 16, 2)],
);
''');
}
test_notUsed_constructorFieldInitializers() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
int _f;
// ^^
// [diag.unusedField] The value of the field '_f' isn't used.
A() : _f = 0;
}
''',
[error(diag.unusedField, 16, 2)],
);
''');
}
test_notUsed_extensionOnClass() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class Foo {}
extension Bar on Foo {
static final _baz = 7;
// ^^^^
// [diag.unusedField] The value of the field '_baz' isn't used.
}
''',
[error(diag.unusedField, 51, 4)],
);
''');
}
test_notUsed_fieldFormalParameter() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
int _f;
// ^^
// [diag.unusedField] The value of the field '_f' isn't used.
A(this._f);
}
''',
[error(diag.unusedField, 16, 2)],
);
''');
}
test_notUsed_mixin() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
mixin M {
int _f = 0;
// ^^
// [diag.unusedField] The value of the field '_f' isn't used.
}
class Bar with M {}
''',
[error(diag.unusedField, 16, 2)],
);
''');
}
test_notUsed_mixinRestriction() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class Foo {
int _f = 0;
// ^^
// [diag.unusedField] The value of the field '_f' isn't used.
}
mixin M on Foo {}
''',
[error(diag.unusedField, 18, 2)],
);
''');
}
test_notUsed_noReference() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
int _f = 0;
// ^^
// [diag.unusedField] The value of the field '_f' isn't used.
}
''',
[error(diag.unusedField, 16, 2)],
);
''');
}
test_notUsed_noReference_wildcard() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
int _ = 0;
// ^
// [diag.unusedField] The value of the field '_' isn't used.
}
''',
[error(diag.unusedField, 16, 1)],
);
''');
}
test_notUsed_noReference_wildcard_preWildcards() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
// @dart = 3.4
// (pre wildcard-variables)
class A {
int _ = 0;
// ^
// [diag.unusedField] The value of the field '_' isn't used.
}
''',
[error(diag.unusedField, 60, 1)],
);
''');
}
test_notUsed_nullAssign() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
var _f;
m() {
@@ -333,85 +325,80 @@ doSomething() => 0;
}
test_notUsed_postfixExpr() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
int _f = 0;
// ^^
// [diag.unusedField] The value of the field '_f' isn't used.
main() {
_f++;
}
}
''',
[error(diag.unusedField, 16, 2)],
);
''');
}
test_notUsed_prefixExpr() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
int _f = 0;
// ^^
// [diag.unusedField] The value of the field '_f' isn't used.
main() {
++_f;
}
}
''',
[error(diag.unusedField, 16, 2)],
);
''');
}
test_notUsed_publicStaticField_privateClass() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class _A {
static String f1 = "x";
// ^^
// [diag.unusedField] The value of the field 'f1' isn't used.
}
void main() => print(_A);
''',
[error(diag.unusedField, 27, 2)],
);
''');
}
test_notUsed_publicStaticField_privateExtension() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
extension _A on String {
static String f1 = "x";
// ^^
// [diag.unusedField] The value of the field 'f1' isn't used.
}
''',
[error(diag.unusedField, 41, 2)],
);
''');
}
test_notUsed_publicStaticField_privateMixin() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
mixin _A {
static String f1 = "x";
// ^^
// [diag.unusedField] The value of the field 'f1' isn't used.
}
void main() => print(_A);
''',
[error(diag.unusedField, 27, 2)],
);
''');
}
test_notUsed_referenceInComment() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
/// [A._f] is great.
class A {
int _f = 0;
// ^^
// [diag.unusedField] The value of the field '_f' isn't used.
}
''',
[error(diag.unusedField, 37, 2)],
);
''');
}
test_notUsed_simpleAssignment() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
int _f = 0;
// ^^
// [diag.unusedField] The value of the field '_f' isn't used.
m() {
_f = 1;
}
@@ -419,13 +406,11 @@ class A {
f(A a) {
a._f = 2;
}
''',
[error(diag.unusedField, 16, 2)],
);
''');
}
test_privateEnum_publicConstant_isUsed() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
enum _E {
v;
}
@@ -437,22 +422,21 @@ void f() {
}
test_privateEnum_publicConstant_notUsed() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum _E {
v;
//^
// [diag.unusedField] The value of the field 'v' isn't used.
}
void f() {
_E;
}
''',
[error(diag.unusedField, 12, 1)],
);
''');
}
test_privateEnum_publicInstanceField_notUsed() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
enum _E {
v;
final int foo = 0;
@@ -465,7 +449,7 @@ void f() {
}
test_privateEnum_publicStaticField_isUsed() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
enum _E {
v;
static final int foo = 0;
@@ -479,23 +463,22 @@ void f() {
}
test_privateEnum_publicStaticField_notUsed() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum _E {
v;
static final int foo = 0;
// ^^^
// [diag.unusedField] The value of the field 'foo' isn't used.
}
void f() {
_E.v;
}
''',
[error(diag.unusedField, 34, 3)],
);
''');
}
test_privateEnum_values_isUsed() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
enum _E {
v
}
@@ -507,7 +490,7 @@ void f() {
}
test_privateEnum_values_isUsed_hasSetter() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
enum _E {
v;
set foo(int _) {}
@@ -520,7 +503,7 @@ void f() {
}
test_publicEnum_privateConstant_isUsed() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
enum E {
_v
}
@@ -532,18 +515,17 @@ void f() {
}
test_publicEnum_privateConstant_notUsed() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E {
_v
//^^
// [diag.unusedField] The value of the field '_v' isn't used.
}
''',
[error(diag.unusedField, 11, 2)],
);
''');
}
test_publicEnum_privateInstanceField_isUsed() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
enum E {
v;
final int _foo = 0;
@@ -556,14 +538,13 @@ void f() {
}
test_publicEnum_privateInstanceField_notUsed() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E {
v;
final int _foo = 0;
// ^^^^
// [diag.unusedField] The value of the field '_foo' isn't used.
}
''',
[error(diag.unusedField, 26, 4)],
);
''');
}
}
@@ -6,10 +6,12 @@ import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnusedImportTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@@ -21,14 +23,14 @@ class A {
const A() {}
}
''');
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
@A()
import 'lib1.dart';
''');
}
test_library_core_library() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'dart:core';
''');
}
@@ -41,7 +43,7 @@ class One {}
newFile('$testPackageLibPath/lib2.dart', r'''
class Two {}
''');
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib1.dart';
Two two = Two();
''');
@@ -59,7 +61,7 @@ class Two {}
newFile('$testPackageLibPath/lib3.dart', r'''
class Three {}
''');
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib1.dart';
Three? three;
''');
@@ -78,7 +80,7 @@ class Two {}
export 'lib2.dart';
class Three {}
''');
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib1.dart';
Two? two;
''');
@@ -90,7 +92,7 @@ extension E on int {
int call(int x) => 0;
}
''');
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
import 'lib1.dart';
f() {
@@ -105,7 +107,7 @@ extension E on String {
String get empty => '';
}
''');
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
import 'lib1.dart';
f() {
@@ -121,7 +123,7 @@ extension E on int {
}
''');
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
import 'a.dart';
void f(Object? x) {
@@ -136,7 +138,7 @@ extension E on int {
int operator[](_) => 0;
}
''');
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
import 'a.dart';
void f() {
@@ -152,7 +154,7 @@ extension E on int {
void operator[]=(_, __) {}
}
''');
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
import 'a.dart';
void f() {
@@ -167,7 +169,7 @@ extension E on int {
void operator[]=(_, __) {}
}
''');
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
import 'a.dart';
void f() {
@@ -182,7 +184,7 @@ extension E on String {
String empty() => '';
}
''');
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
import 'lib1.dart';
f() {
@@ -221,7 +223,7 @@ extension E on String {
String operator -(String s) => this;
}
''');
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
import 'lib1.dart';
f() {
@@ -236,7 +238,7 @@ extension E on String {
void operator -() {}
}
''');
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
import 'lib1.dart';
f() {
@@ -251,7 +253,7 @@ extension E on String {
void set foo(int i) {}
}
''');
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
import 'lib1.dart';
f() {
@@ -266,7 +268,7 @@ extension E on String {
String get empty => '';
}
''');
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
import 'lib1.dart';
f() {
@@ -281,7 +283,7 @@ extension E on String {
String empty() => '';
}
''');
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
import 'lib1.dart' as lib1;
f() {
@@ -296,12 +298,11 @@ extension E on String {
String empty() => '';
}
''');
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
import 'lib1.dart' as lib1;
''',
[error(diag.unusedImport, 7, 11)],
);
// ^^^^^^^^^^^
// [diag.unusedImport] Unused import: 'lib1.dart'.
''');
}
test_library_extension_static_field() async {
@@ -310,7 +311,7 @@ extension E on String {
static const String empty = '';
}
''');
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
import 'lib1.dart';
f() {
@@ -323,18 +324,17 @@ f() {
newFile('$testPackageLibPath/lib1.dart', r'''
class A {}
''');
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib1.dart';
import 'lib1.dart' hide A;
// ^^^^^^^^^^^
// [diag.unusedImport] Unused import: 'lib1.dart'.
A? a;
''',
[error(diag.unusedImport, 27, 11)],
);
''');
}
test_library_inComment_libraryDirective() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
/// Use [Future] class.
import 'dart:async';
''');
@@ -344,7 +344,7 @@ import 'dart:async';
newFile('$testPackageLibPath/lib1.dart', r'''
const x = 0;
''');
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
@A(x)
import 'lib1.dart';
class A {
@@ -365,23 +365,23 @@ extension E on String {
String b() => '';
}
''');
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
import 'lib1.dart';
// ^^^^^^^^^^^
// [diag.unusedImport] Unused import: 'lib1.dart'.
import 'lib2.dart';
f() {
''.b();
}
''',
[error(diag.unusedImport, 7, 11)],
);
''');
}
test_library_noPrefix_constructorName_name() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'dart:async';
// ^^^^^^^^^^^^
// [diag.unusedImport] Unused import: 'dart:async'.
class A {
A.foo();
@@ -390,40 +390,36 @@ class A {
void f() {
A.foo();
}
''',
[error(diag.unusedImport, 7, 12)],
);
''');
}
test_library_noPrefix_named_argument() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'dart:math';
// ^^^^^^^^^^^
// [diag.unusedImport] Unused import: 'dart:math'.
void f() {
Duration(seconds: 0);
}
''',
[error(diag.unusedImport, 7, 11)],
);
''');
}
test_library_prefixed() async {
newFile('$testPackageLibPath/lib1.dart', r'''
class A {}
''');
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib1.dart';
// ^^^^^^^^^^^
// [diag.unusedImport] Unused import: 'lib1.dart'.
import 'lib1.dart' as one;
one.A a = one.A();
''',
[error(diag.unusedImport, 7, 11)],
);
''');
}
test_library_prefixed_commentReference_prefix() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'dart:math' as math;
/// [math]
@@ -432,7 +428,7 @@ void f() {}
}
test_library_prefixed_commentReference_prefixClass() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'dart:math' as math;
/// [math.Random]
@@ -447,14 +443,13 @@ class A {}
newFile('$testPackageLibPath/lib2.dart', r'''
class B {}
''');
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib1.dart' as one;
import 'lib2.dart' as one;
// ^^^^^^^^^^^
// [diag.unusedImport] Unused import: 'lib2.dart'.
one.A a = one.A();
''',
[error(diag.unusedImport, 34, 11)],
);
''');
}
test_library_prefixed_samePrefix_referenced() async {
@@ -464,7 +459,7 @@ class A {}
newFile('$testPackageLibPath/lib2.dart', r'''
class B {}
''');
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib1.dart' as one;
import 'lib2.dart' as one;
one.A a = one.A();
@@ -482,7 +477,7 @@ class B {}
newFile('$testPackageLibPath/lib3.dart', r'''
export 'lib2.dart';
''');
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib1.dart' as one;
import 'lib3.dart' as one;
one.A a = one.A();
@@ -495,7 +490,7 @@ one.B b = one.B();
class A {}
class B {}
''');
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib1.dart' as one show A, B;
one.A a = one.A();
one.B b = one.B();
@@ -507,19 +502,18 @@ one.B b = one.B();
class One {}
topLevelFunction() {}
''');
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib1.dart' hide topLevelFunction;
import 'lib1.dart' as one show topLevelFunction;
class A {
static void x() {
One o;
// ^
// [diag.unusedLocalVariable] The value of the local variable 'o' isn't used.
one.topLevelFunction();
}
}
''',
[error(diag.unusedLocalVariable, 129, 1)],
);
''');
}
test_library_prefixed_showTopLevelFunction_multipleDirectives() async {
@@ -527,7 +521,7 @@ class A {
class One {}
topLevelFunction() {}
''');
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib1.dart' hide topLevelFunction;
import 'lib1.dart' as one show topLevelFunction;
import 'lib1.dart' as two show topLevelFunction;
@@ -544,14 +538,13 @@ class A {
newFile('$testPackageLibPath/a.dart', '''
class File {}
''');
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'dart:io' as prefix;
// ^^^^^^^^^
// [diag.unusedImport] Unused import: 'dart:io'.
import 'a.dart' as prefix;
prefix.File? f;
''',
[error(diag.unusedImport, 7, 9)],
);
''');
}
test_library_show() async {
@@ -559,38 +552,35 @@ prefix.File? f;
class A {}
class B {}
''');
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib1.dart' show A;
import 'lib1.dart' show B;
// ^^^^^^^^^^^
// [diag.unusedImport] Unused import: 'lib1.dart'.
A a = A();
''',
[error(diag.unusedImport, 34, 11)],
);
''');
}
test_library_systemLibrary() async {
newFile('$testPackageLibPath/lib1.dart', '''
class File {}
''');
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'dart:io';
// ^^^^^^^^^
// [diag.unusedImport] Unused import: 'dart:io'.
import 'lib1.dart';
File? f;
''',
[error(diag.unusedImport, 7, 9)],
);
''');
}
test_library_unusedImport() async {
newFile('$testPackageLibPath/lib1.dart', '');
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib1.dart';
''',
[error(diag.unusedImport, 7, 11)],
);
// ^^^^^^^^^^^
// [diag.unusedImport] Unused import: 'lib1.dart'.
''');
}
test_part_extension_usedLibraryImport() async {
@@ -1,16 +1,18 @@
// 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[diff_block_end]
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnusedLabelTest);
defineReflectiveTests(UnusedLabelTest_Language219);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@@ -24,36 +26,34 @@ class UnusedLabelTest_Language219 extends PubPackageResolutionTest
mixin UnusedLabelTestCases on PubPackageResolutionTest {
test_unused_inSwitch() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
f(x) {
switch (x) {
label: case 0:
// ^^^^^^
// [diag.unusedLabel] The label 'label' isn't used.
break;
default:
break;
}
}
''',
[error(diag.unusedLabel, 26, 6)],
);
''');
}
test_unused_onWhile() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
f(condition()) {
label: while (condition()) {
//^^^^^^
// [diag.unusedLabel] The label 'label' isn't used.
break;
}
}
''',
[error(diag.unusedLabel, 19, 6)],
);
''');
}
test_used_inSwitch() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
f(x) {
switch (x) {
label: case 0:
@@ -66,7 +66,7 @@ f(x) {
}
test_used_onWhile() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
f(condition()) {
label: while (condition()) {
break label;
@@ -2,32 +2,32 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnusedLocalVariableTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UnusedLocalVariableTest extends PubPackageResolutionTest {
test_forEachPartsWithPattern_notUsed() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(List<(int,)> x) {
for (var (a,) in x) {}
// ^
// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used.
}
''',
[error(diag.unusedLocalVariable, 37, 1)],
);
''');
}
test_forEachPartsWithPattern_used() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(List<(int,)> x) {
for (var (a,) in x) {
a;
@@ -37,7 +37,7 @@ void f(List<(int,)> x) {
}
test_forEachPartsWithPattern_wildcard() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(List<(int,)> x) {
for (var (_,) in x) {}
}
@@ -45,18 +45,17 @@ void f(List<(int,)> x) {
}
test_forPartsWithPattern_notUsed() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f() {
for (var (a,) = (0,);;) {}
// ^
// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used.
}
''',
[error(diag.unusedLocalVariable, 23, 1)],
);
''');
}
test_forPartsWithPattern_used() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f() {
for (var (a,) = (0,);;) {
a;
@@ -66,7 +65,7 @@ void f() {
}
test_forPartsWithPattern_wildcard() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f() {
for (var (_,) = (0,);;) {}
}
@@ -74,21 +73,19 @@ void f() {
}
test_ifStatement_caseClause_logicalOr_notUsed() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
if (x case int a || [int a]) {}
// ^
// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used.
// ^
// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used.
}
''',
[
error(diag.unusedLocalVariable, 37, 1),
error(diag.unusedLocalVariable, 47, 1),
],
);
''');
}
test_ifStatement_caseClause_logicalOr_used() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
if (x case int a || [int a]) {
a;
@@ -98,18 +95,17 @@ void f(Object? x) {
}
test_ifStatement_caseClause_single_notUsed() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
if (x case int a) {}
// ^
// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used.
}
''',
[error(diag.unusedLocalVariable, 37, 1)],
);
''');
}
test_ifStatement_caseClause_single_used() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
if (x case int a) {
a;
@@ -119,7 +115,7 @@ void f(Object? x) {
}
test_ifStatement_caseClause_whenClause() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
if (x case int a when a > 0) {}
}
@@ -127,7 +123,7 @@ void f(Object? x) {
}
test_ifStatement_caseClause_wildcard() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
if (x case int _) {}
}
@@ -135,22 +131,21 @@ void f(Object? x) {
}
test_inFor_underscores() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
f() {
for (var _ in [1,2,3]) {
for (var __ in [4,5,6]) {
// ^^
// [diag.unusedLocalVariable] The value of the local variable '__' isn't used.
// do something
}
}
}
''',
[error(diag.unusedLocalVariable, 46, 2)],
);
''');
}
test_inFor_underscores_preWildCards() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
// @dart = 3.4
// (pre wildcard-variables)
f() {
@@ -164,35 +159,32 @@ f() {
}
test_localVariable_forElement_underscores() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
f() {
[
for (var __ in [1, 2, 3]) 1
// ^^
// [diag.unusedLocalVariable] The value of the local variable '__' isn't used.
];
}
''',
[error(diag.unusedLocalVariable, 27, 2)],
);
''');
}
test_localVariable_underscores() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
f() {
var __ = 0;
// ^^
// [diag.unusedLocalVariable] The value of the local variable '__' isn't used.
var ___ = 0;
// ^^^
// [diag.unusedLocalVariable] The value of the local variable '___' isn't used.
}
''',
[
error(diag.unusedLocalVariable, 12, 2),
error(diag.unusedLocalVariable, 26, 3),
],
);
''');
}
test_localVariable_wildcard() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
f() {
var _ = 0;
}
@@ -200,18 +192,17 @@ f() {
}
test_localVariableListPattern_underscores() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
f() {
var [__] = [1];
// ^^
// [diag.unusedLocalVariable] The value of the local variable '__' isn't used.
}
''',
[error(diag.unusedLocalVariable, 13, 2)],
);
''');
}
test_localVariableListPattern_wildcard() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
f() {
var [_] = [1];
}
@@ -219,18 +210,17 @@ f() {
}
test_localVariablePattern_underscores() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
f() {
var (__) = (1);
// ^^
// [diag.unusedLocalVariable] The value of the local variable '__' isn't used.
}
''',
[error(diag.unusedLocalVariable, 13, 2)],
);
''');
}
test_localVariablePattern_wildcard() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
f() {
var (_) = (1);
}
@@ -238,20 +228,19 @@ f() {
}
test_localVariableSwitchListPattern_underscores() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object o) {
switch(o) {
case [var __] : {}
// ^^
// [diag.unusedLocalVariable] The value of the local variable '__' isn't used.
}
}
''',
[error(diag.unusedLocalVariable, 47, 2)],
);
''');
}
test_localVariableSwitchListPattern_wildcard() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object o) {
switch(o) {
case [var _] : {}
@@ -261,63 +250,56 @@ void f(Object o) {
}
test_patternVariableDeclarationStatement_noneUsed() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f() {
var (a, b) = (0, 1);
// ^
// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used.
// ^
// [diag.unusedLocalVariable] The value of the local variable 'b' isn't used.
}
''',
[
error(diag.unusedLocalVariable, 18, 1),
error(diag.unusedLocalVariable, 21, 1),
],
);
''');
}
test_patternVariableDeclarationStatement_noneUsed_nested() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f() {
var (a, [b, _]) = (0, []);
// ^
// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used.
// ^
// [diag.unusedLocalVariable] The value of the local variable 'b' isn't used.
}
''',
[
error(diag.unusedLocalVariable, 18, 1),
error(diag.unusedLocalVariable, 22, 1),
],
);
''');
}
test_patternVariableDeclarationStatement_noneUsed_withChildStatements() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f() {
var (a, b) = () {
// ^
// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used.
// ^
// [diag.unusedLocalVariable] The value of the local variable 'b' isn't used.
var (c, d) = (0, 1);
return (c, d);
}();
}
''',
[
error(diag.unusedLocalVariable, 18, 1),
error(diag.unusedLocalVariable, 21, 1),
],
);
''');
}
test_patternVariableDeclarationStatement_notUsed() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f() {
var (a,) = (0,);
// ^
// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used.
}
''',
[error(diag.unusedLocalVariable, 18, 1)],
);
''');
}
test_patternVariableDeclarationStatement_someUsed() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f() {
var (a, b) = (0, 1);
a;
@@ -326,7 +308,7 @@ void f() {
}
test_patternVariableDeclarationStatement_someUsed_nested() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f() {
var (a, [b, c]) = (0, []);
c;
@@ -335,7 +317,7 @@ void f() {
}
test_patternVariableDeclarationStatement_used() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f() {
var (a,) = (0,);
a;
@@ -344,7 +326,7 @@ void f() {
}
test_patternVariableDeclarationStatement_wildcard() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f() {
var (a, _) = (0, 1);
a;
@@ -353,21 +335,20 @@ void f() {
}
test_switchExpression_notUsed() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
Object? f(Object? x) {
return switch (x) {
(int a,) => 0,
// ^
// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used.
_ => 0,
};
}
''',
[error(diag.unusedLocalVariable, 54, 1)],
);
''');
}
test_switchExpression_used() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
Object? f(Object? x) {
return switch (x) {
(int a,) => a,
@@ -378,7 +359,7 @@ Object? f(Object? x) {
}
test_switchExpression_wildcard() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
Object? f(Object? x) {
return switch (x) {
(int _,) => 0,
@@ -389,21 +370,20 @@ Object? f(Object? x) {
}
test_switchStatement_patternCase_notUsed() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
switch (x) {
case (var a,):
// ^
// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used.
break;
};
}
''',
[error(diag.unusedLocalVariable, 49, 1)],
);
''');
}
test_switchStatement_patternCase_used() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
switch (x) {
case (var a,):
@@ -414,7 +394,7 @@ void f(Object? x) {
}
test_switchStatement_patternCase_wildcard() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
switch (x) {
case (int _,):
@@ -425,25 +405,23 @@ void f(Object? x) {
}
test_switchStatement_sharedScope_consistent_notUsed() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
switch (x) {
case (var a,):
// ^
// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used.
case [var a,]:
// ^
// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used.
break;
};
}
''',
[
error(diag.unusedLocalVariable, 49, 1),
error(diag.unusedLocalVariable, 68, 1),
],
);
''');
}
test_switchStatement_sharedScope_consistent_used() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
switch (x) {
case (var a,):
@@ -455,52 +433,49 @@ void f(Object? x) {
}
test_switchStatement_sharedScope_notConsistent_notUsed() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
switch (x) {
case 0:
case [var a]:
// ^
// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used.
break;
};
}
''',
[error(diag.unusedLocalVariable, 61, 1)],
);
''');
}
test_switchStatement_sharedScope_notConsistent_used() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
switch (x) {
case 0:
case [var a]:
a;
// ^
// [diag.patternVariableSharedCaseScopeNotAllCases] The variable 'a' is available in some, but not all cases that share this body.
};
}
''',
[error(diag.patternVariableSharedCaseScopeNotAllCases, 71, 1)],
);
''');
}
test_switchStatement_sharedScope_whenClause_notUsed_used() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
switch (x) {
case [int a,]:
// ^
// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used.
case (int a,) when a > 0:
break;
};
}
''',
[error(diag.unusedLocalVariable, 49, 1)],
);
''');
}
test_switchStatement_sharedScope_whenClause_used_notDeclared() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
switch (x) {
case (int a,) when a > 0:
@@ -512,22 +487,21 @@ void f(Object? x) {
}
test_switchStatement_sharedScope_whenClause_used_notUsed() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
switch (x) {
case (int a,) when a > 0:
case [int a,]:
// ^
// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used.
break;
};
}
''',
[error(diag.unusedLocalVariable, 79, 1)],
);
''');
}
test_switchStatement_sharedScope_whenClause_used_used() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
void f(Object? x) {
switch (x) {
case (int a,) when a > 0:
@@ -539,33 +513,31 @@ void f(Object? x) {
}
test_variableDeclarationStatement_inFunction() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
main() {
var v = 1;
// ^
// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used.
v = 2;
}
''',
[error(diag.unusedLocalVariable, 15, 1)],
);
''');
}
test_variableDeclarationStatement_inMethod() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
foo() {
var v = 1;
// ^
// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used.
v = 2;
}
}
''',
[error(diag.unusedLocalVariable, 28, 1)],
);
''');
}
test_variableDeclarationStatement_isInvoked() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
typedef Foo();
main() {
Foo foo = () {};
@@ -575,7 +547,7 @@ main() {
}
test_variableDeclarationStatement_isNullAssigned() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
typedef Foo();
main() {
var v;
@@ -586,43 +558,40 @@ doSomething() => 42;
}
test_variableDeclarationStatement_isRead_notUsed_compoundAssign() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
main() {
var v = 1;
// ^
// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used.
v += 2;
}
''',
[error(diag.unusedLocalVariable, 15, 1)],
);
''');
}
test_variableDeclarationStatement_isRead_notUsed_postfixExpr() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
main() {
var v = 1;
// ^
// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used.
v++;
}
''',
[error(diag.unusedLocalVariable, 15, 1)],
);
''');
}
test_variableDeclarationStatement_isRead_notUsed_prefixExpr() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
main() {
var v = 1;
// ^
// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used.
++v;
}
''',
[error(diag.unusedLocalVariable, 15, 1)],
);
''');
}
test_variableDeclarationStatement_isRead_usedArgument() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
main() {
var v = 1;
print(++v);
@@ -632,7 +601,7 @@ print(x) {}
}
test_variableDeclarationStatement_isRead_usedInvocationTarget() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
foo() {}
}
File diff suppressed because it is too large Load Diff
@@ -2,21 +2,22 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnusedShownNameTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UnusedShownNameTest extends PubPackageResolutionTest {
test_dartCore_unused() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'dart:core' as core show int;
''');
}
@@ -28,16 +29,15 @@ extension E on String {
}
String s = '';
''');
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib1.dart' show E, s;
// ^
// [diag.unusedShownName] The name E is shown, but isn't used.
f() {
s.length;
}
''',
[error(diag.unusedShownName, 24, 1)],
);
''');
}
test_extension_instance_method_used() async {
@@ -46,7 +46,7 @@ extension E on String {
String empty() => '';
}
''');
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
import 'lib1.dart' show E;
f() {
@@ -60,7 +60,7 @@ f() {
var a = 0;
''');
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'a.dart' as p show a;
void f() {
@@ -74,7 +74,7 @@ void f() {
var a = 0;
''');
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'a.dart' as p show a;
void f() {
@@ -88,7 +88,7 @@ void f() {
var a = 0;
''');
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'a.dart' as p show a;
void f() {
@@ -102,7 +102,7 @@ void f() {
var a = 0;
''');
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'a.dart' show a;
void f() {
@@ -116,7 +116,7 @@ void f() {
var a = 0;
''');
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'a.dart' show a;
void f() {
@@ -130,7 +130,7 @@ void f() {
var a = 0;
''');
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
import 'a.dart' show a;
void f() {
@@ -144,13 +144,12 @@ void f() {
class A {}
class B {}
''');
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib1.dart' show A, B;
// ^
// [diag.unusedShownName] The name B is shown, but isn't used.
A a = A();
''',
[error(diag.unusedShownName, 27, 1)],
);
''');
}
test_unreferenced_dotShorthand() async {
@@ -160,28 +159,26 @@ class A {}
void f(A a) {}
''');
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'a.dart' show A, f;
// ^
// [diag.unusedShownName] The name A is shown, but isn't used.
void g() {
f(.new());
}
''',
[error(diag.unusedShownName, 21, 1)],
);
''');
}
test_unresolved() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'dart:math' show max, FooBar;
// ^^^^^^
// [diag.undefinedShownName] The library 'dart:math' doesn't export a member with the shown name 'FooBar'.
main() {
print(max(1, 2));
}
''',
[error(diag.undefinedShownName, 29, 6)],
);
''');
}
test_unusedShownName_as() async {
@@ -189,13 +186,12 @@ main() {
class A {}
class B {}
''');
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib1.dart' as p show A, B;
// ^
// [diag.unusedShownName] The name B is shown, but isn't used.
p.A a = p.A();
''',
[error(diag.unusedShownName, 32, 1)],
);
''');
}
test_unusedShownName_duplicates() async {
@@ -205,15 +201,16 @@ class B {}
class C {}
class D {}
''');
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib1.dart' show A, B;
// ^
// [diag.unusedShownName] The name B is shown, but isn't used.
import 'lib1.dart' show C, D;
// ^
// [diag.unusedShownName] The name D is shown, but isn't used.
A a = A();
C c = C();
''',
[error(diag.unusedShownName, 27, 1), error(diag.unusedShownName, 57, 1)],
);
''');
}
test_unusedShownName_topLevelVariable() async {
@@ -223,15 +220,14 @@ const int var2 = 2;
const int var3 = 3;
const int var4 = 4;
''');
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'lib1.dart' show var1, var2;
import 'lib1.dart' show var3, var4;
// ^^^^
// [diag.unusedShownName] The name var4 is shown, but isn't used.
int a = var1;
int b = var2;
int c = var3;
''',
[error(diag.unusedShownName, 66, 4)],
);
''');
}
}
@@ -2,41 +2,40 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UriDoesNotExistInDocImportTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UriDoesNotExistInDocImportTest extends PubPackageResolutionTest {
test_libraryDocImport_cannotResolve_dart() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
/// @docImport 'dart:foo';
// ^^^^^^^^^^
// [diag.uriDoesNotExistInDocImport] Target of URI doesn't exist: 'dart:foo'.
library;
''',
[error(diag.uriDoesNotExistInDocImport, 15, 10)],
);
''');
}
test_libraryDocImport_cannotResolve_file() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
/// @docImport 'foo.dart';
// ^^^^^^^^^^
// [diag.uriDoesNotExistInDocImport] Target of URI doesn't exist: 'foo.dart'.
library;
''',
[error(diag.uriDoesNotExistInDocImport, 15, 10)],
);
''');
}
test_libraryDocImport_canResolve_dart() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
/// @docImport 'dart:math';
library;
''');
@@ -46,7 +45,7 @@ library;
newFile('$testPackageLibPath/foo.dart', r'''
class A {}
''');
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
/// @docImport 'foo.dart';
library;
''');
@@ -6,16 +6,18 @@ import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UriDoesNotExistTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UriDoesNotExistTest extends PubPackageResolutionTest {
@FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/51407')
@SkippedTest(issue: 'https://github.com/dart-lang/sdk/issues/51407')
test_doubleSlash() async {
newFolder('$testPackageLibPath/c');
newFile('$testPackageLibPath/c/d.dart', '''''
@@ -25,64 +27,56 @@ class D {}
import 'c/d.dart';
void g(D d) {}
''');
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'b.dart';
import 'c//d.dart';
void f() {
g(D());
}
''',
[error(diag.uriDoesNotExist, 24, 11)],
);
''');
}
test_libraryExport() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
export 'unknown.dart';
''',
[error(diag.uriDoesNotExist, 7, 14)],
);
// ^^^^^^^^^^^^^^
// [diag.uriDoesNotExist] Target of URI doesn't exist: 'unknown.dart'.
''');
}
test_libraryExport_cannotResolve() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
export 'dart:foo';
''',
[error(diag.uriDoesNotExist, 7, 10)],
);
// ^^^^^^^^^^
// [diag.uriDoesNotExist] Target of URI doesn't exist: 'dart:foo'.
''');
}
test_libraryExport_dart() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
export 'dart:math/bar.dart';
''',
[error(diag.uriDoesNotExist, 7, 20)],
);
// ^^^^^^^^^^^^^^^^^^^^
// [diag.uriDoesNotExist] Target of URI doesn't exist: 'dart:math/bar.dart'.
''');
}
test_libraryImport() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
import 'unknown.dart';
''',
[error(diag.uriDoesNotExist, 7, 14)],
);
// ^^^^^^^^^^^^^^
// [diag.uriDoesNotExist] Target of URI doesn't exist: 'unknown.dart'.
''');
}
test_libraryImport_appearsAfterDeletingTarget() async {
String filePath = newFile('$testPackageLibPath/target.dart', '').path;
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
import 'target.dart';
''',
[error(diag.unusedImport, 7, 13)],
);
// ^^^^^^^^^^^^^
// [diag.unusedImport] Unused import: 'target.dart'.
''');
// Remove the overlay in the same way as AnalysisServer.
deleteFile(filePath);
@@ -96,43 +90,39 @@ import 'target.dart';
}
test_libraryImport_cannotResolve() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'dart:foo';
''',
[error(diag.uriDoesNotExist, 7, 10)],
);
// ^^^^^^^^^^
// [diag.uriDoesNotExist] Target of URI doesn't exist: 'dart:foo'.
''');
}
test_libraryImport_dart() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
import 'dart:math/bar.dart';
''',
[error(diag.uriDoesNotExist, 7, 20)],
);
// ^^^^^^^^^^^^^^^^^^^^
// [diag.uriDoesNotExist] Target of URI doesn't exist: 'dart:math/bar.dart'.
''');
}
test_libraryImport_deferredWithInvalidUri() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import '[invalid uri]' deferred as p;
// ^^^^^^^^^^^^^^^
// [diag.uriDoesNotExist] Target of URI doesn't exist: '[invalid uri]'.
main() {
p.loadLibrary();
}
''',
[error(diag.uriDoesNotExist, 7, 15)],
);
''');
}
@failingTest
test_libraryImport_disappears_when_fixed() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
import 'target.dart';
''',
[error(diag.uriDoesNotExist, 7, 13)],
);
// ^^^^^^^^^^^^^
// [diag.uriDoesNotExist] Target of URI doesn't exist: 'target.dart'.
''');
newFile('$testPackageLibPath/target.dart', '');
@@ -144,21 +134,19 @@ import 'target.dart';
}
test_part() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
library lib;
part 'unknown.dart';
''',
[error(diag.uriDoesNotExist, 18, 14)],
);
// ^^^^^^^^^^^^^^
// [diag.uriDoesNotExist] Target of URI doesn't exist: 'package:test/unknown.dart'.
''');
}
test_part_cannotResolve() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
part 'dart:foo';
''',
[error(diag.uriDoesNotExist, 5, 10)],
);
// ^^^^^^^^^^
// [diag.uriDoesNotExist] Target of URI doesn't exist: 'dart:foo'.
''');
}
}
@@ -2,53 +2,50 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UriWithInterpolationTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UriWithInterpolationTest extends PubPackageResolutionTest {
test_library_docImport() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
/// @docImport '${'foo'}.dart';
// ^^^^^^^^^^^^^^^
// [diag.uriWithInterpolation] URIs can't use string interpolation.
library;
''',
[error(diag.uriWithInterpolation, 15, 15)],
);
''');
}
test_library_export() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
export '${'foo'}.dart';
''',
[error(diag.uriWithInterpolation, 7, 15)],
);
// ^^^^^^^^^^^^^^^
// [diag.uriWithInterpolation] URIs can't use string interpolation.
''');
}
test_library_import() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import '${'foo'}.dart';
''',
[error(diag.uriWithInterpolation, 7, 15)],
);
// ^^^^^^^^^^^^^^^
// [diag.uriWithInterpolation] URIs can't use string interpolation.
''');
}
test_part() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
part '${'foo'}.dart';
''',
[error(diag.uriWithInterpolation, 5, 15)],
);
// ^^^^^^^^^^^^^^^
// [diag.uriWithInterpolation] URIs can't use string interpolation.
''');
}
}
@@ -2,44 +2,42 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UseOfNativeExtensionTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UseOfNativeExtensionTest extends PubPackageResolutionTest {
test_docImport() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
/// @docImport 'dart-ext:x';
// ^^^^^^^^^^^^
// [diag.useOfNativeExtension] Dart native extensions are deprecated and aren't available in Dart 2.15.
library;
''',
[error(diag.useOfNativeExtension, 15, 12)],
);
''');
}
test_export() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
export 'dart-ext:x';
''',
[error(diag.useOfNativeExtension, 7, 12)],
);
// ^^^^^^^^^^^^
// [diag.useOfNativeExtension] Dart native extensions are deprecated and aren't available in Dart 2.15.
''');
}
test_import() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
import 'dart-ext:x';
''',
[error(diag.useOfNativeExtension, 7, 12)],
);
// ^^^^^^^^^^^^
// [diag.useOfNativeExtension] Dart native extensions are deprecated and aren't available in Dart 2.15.
''');
}
}
File diff suppressed because it is too large Load Diff
@@ -2,39 +2,33 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(UseOfPrivateParameterNameTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class UseOfPrivateParameterNameTest extends PubPackageResolutionTest {
test_andVoidLhsError() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
class C {
int? _x;
// ^^
// [diag.unusedField] The value of the field '_x' isn't used.
C({this._x});
}
void f() {
C(_x: 123);
// ^^
// [diag.useOfPrivateParameterName] The named parameter '_x' should use the corresponding public name 'x' at the callsite.
}
''',
[
error(diag.unusedField, 17, 2),
error(
diag.useOfPrivateParameterName,
54,
2,
messageContains: ["'_x'", "'x'"],
),
],
);
''');
}
}
File diff suppressed because it is too large Load Diff
@@ -2,129 +2,121 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(ValuesDeclarationInEnumTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class ValuesDeclarationInEnumTest extends PubPackageResolutionTest {
test_constant() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E {
values
//^^^^^^
// [diag.valuesDeclarationInEnum] A member named 'values' can't be declared in an enum.
}
''',
[error(diag.valuesDeclarationInEnum, 11, 6)],
);
''');
}
test_field() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E {
v;
final int values = 0;
// ^^^^^^
// [diag.valuesDeclarationInEnum] A member named 'values' can't be declared in an enum.
}
''',
[error(diag.valuesDeclarationInEnum, 26, 6)],
);
''');
}
test_field_static() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E {
v;
static int values = 0;
// ^^^^^^
// [diag.valuesDeclarationInEnum] A member named 'values' can't be declared in an enum.
}
''',
[error(diag.valuesDeclarationInEnum, 27, 6)],
);
''');
}
test_field_withConstructor() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E {
v;
final values = [];
// ^^^^^^
// [diag.valuesDeclarationInEnum] A member named 'values' can't be declared in an enum.
const E();
}
''',
[error(diag.valuesDeclarationInEnum, 22, 6)],
);
''');
}
test_getter() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E {
v;
int get values => 0;
// ^^^^^^
// [diag.valuesDeclarationInEnum] A member named 'values' can't be declared in an enum.
}
''',
[error(diag.valuesDeclarationInEnum, 24, 6)],
);
''');
}
test_getter_static() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E {
v;
static int get values => 0;
// ^^^^^^
// [diag.valuesDeclarationInEnum] A member named 'values' can't be declared in an enum.
}
''',
[error(diag.valuesDeclarationInEnum, 31, 6)],
);
''');
}
test_method() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E {
v;
void values() {}
// ^^^^^^
// [diag.valuesDeclarationInEnum] A member named 'values' can't be declared in an enum.
}
''',
[error(diag.valuesDeclarationInEnum, 21, 6)],
);
''');
}
test_method_static() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E {
v;
static void values() {}
// ^^^^^^
// [diag.valuesDeclarationInEnum] A member named 'values' can't be declared in an enum.
}
''',
[error(diag.valuesDeclarationInEnum, 28, 6)],
);
''');
}
test_setter() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E {
v;
set values(_) {}
// ^^^^^^
// [diag.valuesDeclarationInEnum] A member named 'values' can't be declared in an enum.
}
''',
[error(diag.valuesDeclarationInEnum, 20, 6)],
);
''');
}
test_setter_static() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
enum E {
v;
static set values(_) {}
File diff suppressed because it is too large Load Diff
@@ -2,92 +2,86 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(VariableTypeMismatchTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class VariableTypeMismatchTest extends PubPackageResolutionTest {
test_assignNullToInt() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
const int? x = null;
''');
}
test_assignNullToUndefined() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
const Unresolved x = null;
''',
[error(diag.undefinedClass, 6, 10)],
);
// ^^^^^^^^^^
// [diag.undefinedClass] Undefined class 'Unresolved'.
''');
}
test_assignUnrelatedTypes() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
const int x = 'foo';
''',
[error(diag.invalidAssignment, 14, 5)],
);
// ^^^^^
// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'.
''');
}
test_assignValueToUndefined() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
const Unresolved x = 'foo';
''',
[error(diag.undefinedClass, 6, 10)],
);
// ^^^^^^^^^^
// [diag.undefinedClass] Undefined class 'Unresolved'.
''');
}
test_int_to_double_variable_reference_is_not_promoted() async {
// Note: in the following code, the declaration of `y` should produce an
// error because we should only promote literal ints to doubles; we
// shouldn't promote the reference to the variable `x`.
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
const dynamic x = 0;
const double y = x;
''',
[error(diag.variableTypeMismatch, 38, 1)],
);
// ^
// [diag.variableTypeMismatch] A value of type 'int' can't be assigned to a const variable of type 'double'.
''');
}
test_listLiteral_inferredElementType() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
const dynamic x = [1];
const List<String> y = x;
''',
[error(diag.variableTypeMismatch, 46, 1)],
);
// ^
// [diag.variableTypeMismatch] A value of type 'List<int>' can't be assigned to a const variable of type 'List<String>'.
''');
}
test_mapLiteral_inferredKeyType() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
const dynamic x = {1: 1};
const Map<String, dynamic> y = x;
''',
[error(diag.variableTypeMismatch, 57, 1)],
);
// ^
// [diag.variableTypeMismatch] A value of type 'Map<int, int>' can't be assigned to a const variable of type 'Map<String, dynamic>'.
''');
}
test_mapLiteral_inferredValueType() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
const dynamic x = {1: 1};
const Map<dynamic, String> y = x;
''',
[error(diag.variableTypeMismatch, 57, 1)],
);
// ^
// [diag.variableTypeMismatch] A value of type 'Map<int, int>' can't be assigned to a const variable of type 'Map<dynamic, String>'.
''');
}
}
@@ -2,32 +2,32 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(VoidWithTypeArgumentsTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class VoidWithTypeArgumentsTest extends PubPackageResolutionTest {
test_noArguments() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
void f() {}
''');
}
test_withArguments() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
void<int> f() {}
''',
[error(diag.voidWithTypeArguments, 4, 1)],
);
// ^
// [diag.voidWithTypeArguments] Type 'void' can't have type arguments.
''');
var node = findNode.namedType('void<int>');
assertResolvedNodeText(node, r'''
@@ -6,10 +6,12 @@ import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(WrongNumberOfParametersForOperatorTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@@ -54,7 +56,7 @@ class WrongNumberOfParametersForOperatorTest extends PubPackageResolutionTest {
}
test_correct_number_of_parameters_index_assignment() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
operator []=(a, b) {}
}
@@ -67,14 +69,13 @@ class A {
}
test_unaryMinus() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A {
operator -(a, b) {}
// ^
// [diag.wrongNumberOfParametersForOperatorMinus] Operator '-' should declare 0 or 1 parameter, but 2 found.
}
''',
[error(diag.wrongNumberOfParametersForOperatorMinus, 21, 1)],
);
''');
}
test_unaryTilde() async {
@@ -2,38 +2,37 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(WrongNumberOfTypeArgumentsEnumTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class WrongNumberOfTypeArgumentsEnumTest extends PubPackageResolutionTest {
test_tooFew() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E<T, U> {
v<int>()
// ^^^^^
// [diag.wrongNumberOfTypeArgumentsEnum] The enum is declared with 2 type parameters, but 1 type arguments were given.
}
''',
[error(diag.wrongNumberOfTypeArgumentsEnum, 18, 5)],
);
''');
}
test_tooMany() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
enum E<T> {
v<int, int>()
// ^^^^^^^^^^
// [diag.wrongNumberOfTypeArgumentsEnum] The enum is declared with 1 type parameters, but 2 type arguments were given.
}
''',
[error(diag.wrongNumberOfTypeArgumentsEnum, 15, 10)],
);
''');
}
}
@@ -2,32 +2,32 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(WrongNumberOfTypeArgumentsExtensionTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class WrongNumberOfTypeArgumentsExtensionTest extends PubPackageResolutionTest {
test_notGeneric() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
extension E on int {
void foo() {}
}
void f() {
E<int>(0).foo();
// ^^^^^
// [diag.wrongNumberOfTypeArgumentsExtension] The extension 'E' is declared with 0 type parameters, but 1 type arguments were given.
}
''',
[error(diag.wrongNumberOfTypeArgumentsExtension, 54, 5)],
);
''');
var node = findNode.extensionOverride('E<int>');
assertResolvedNodeText(node, r'''
@@ -56,18 +56,17 @@ ExtensionOverride
}
test_tooFew() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
extension E<S, T> on int {
void foo() {}
}
void f() {
E<bool>(0).foo();
// ^^^^^^
// [diag.wrongNumberOfTypeArgumentsExtension] The extension 'E' is declared with 2 type parameters, but 1 type arguments were given.
}
''',
[error(diag.wrongNumberOfTypeArgumentsExtension, 60, 6)],
);
''');
var node = findNode.extensionOverride('E<bool>');
assertResolvedNodeText(node, r'''
@@ -99,18 +98,17 @@ ExtensionOverride
}
test_tooMany() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
extension E<T> on int {
void foo() {}
}
void f() {
E<bool, int>(0).foo();
// ^^^^^^^^^^^
// [diag.wrongNumberOfTypeArgumentsExtension] The extension 'E' is declared with 1 type parameters, but 2 type arguments were given.
}
''',
[error(diag.wrongNumberOfTypeArgumentsExtension, 57, 11)],
);
''');
var node = findNode.extensionOverride('E<bool, int>');
assertResolvedNodeText(node, r'''
File diff suppressed because it is too large Load Diff
@@ -2,14 +2,15 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(WrongTypeParameterVarianceInSuperinterfaceTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@@ -17,18 +18,17 @@ main() {
class WrongTypeParameterVarianceInSuperinterfaceTest
extends PubPackageResolutionTest {
test_class_extends_function_parameterType() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
typedef F<X> = void Function(X);
class A<X> {}
class B<X> extends A<F<X>> {}
''',
[error(diag.wrongTypeParameterVarianceInSuperinterface, 55, 1)],
);
// ^
// [diag.wrongTypeParameterVarianceInSuperinterface] 'X' can't be used contravariantly or invariantly in 'A<F<X>>'.
''');
}
test_class_extends_function_parameterType_parameterType() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
typedef F1<X> = void Function(X);
typedef F2<X> = void Function(F1<X>);
class A<X> {}
@@ -37,19 +37,18 @@ class B<X> extends A<F2<X>> {}
}
test_class_extends_function_parameterType_returnType() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
typedef F1<X> = X Function();
typedef F2<X> = void Function(F1<X>);
class A<X> {}
class B<X> extends A<F2<X>> {}
''',
[error(diag.wrongTypeParameterVarianceInSuperinterface, 90, 1)],
);
// ^
// [diag.wrongTypeParameterVarianceInSuperinterface] 'X' can't be used contravariantly or invariantly in 'A<F2<X>>'.
''');
}
test_class_extends_function_returnType() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
typedef F<X> = X Function();
class A<X> {}
class B<X> extends A<F<X>> {}
@@ -57,37 +56,35 @@ class B<X> extends A<F<X>> {}
}
test_class_extends_function_returnType_parameterType() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
typedef F1<X> = void Function(X);
typedef F2<X> = F1<X> Function();
class A<X> {}
class B<X> extends A<F2<X>> {}
''',
[error(diag.wrongTypeParameterVarianceInSuperinterface, 90, 1)],
);
// ^
// [diag.wrongTypeParameterVarianceInSuperinterface] 'X' can't be used contravariantly or invariantly in 'A<F2<X>>'.
''');
}
test_class_extends_withoutFunction() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A<X> {}
class B<X> extends A<X> {}
''');
}
test_class_implements_function_parameterType() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
typedef F<X> = void Function(X);
class A<X> {}
class B<X> implements A<F<X>> {}
''',
[error(diag.wrongTypeParameterVarianceInSuperinterface, 55, 1)],
);
// ^
// [diag.wrongTypeParameterVarianceInSuperinterface] 'X' can't be used contravariantly or invariantly in 'A<F<X>>'.
''');
}
test_class_implements_function_returnType() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
typedef F<X> = X Function();
class A<X> {}
class B<X> implements A<F<X>> {}
@@ -95,25 +92,24 @@ class B<X> implements A<F<X>> {}
}
test_class_implements_withoutFunction() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A<X> {}
class B<X> implements A<X> {}
''');
}
test_class_with_function_parameterType() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
typedef F<X> = void Function(X);
mixin A<X> {}
class B<X> extends Object with A<F<X>> {}
''',
[error(diag.wrongTypeParameterVarianceInSuperinterface, 55, 1)],
);
// ^
// [diag.wrongTypeParameterVarianceInSuperinterface] 'X' can't be used contravariantly or invariantly in 'A<F<X>>'.
''');
}
test_class_with_function_returnType() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
typedef F<X> = X Function();
mixin A<X> {}
class B<X> extends Object with A<F<X>> {}
@@ -121,38 +117,36 @@ class B<X> extends Object with A<F<X>> {}
}
test_class_with_withoutFunction() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
mixin A<X> {}
class B<X> extends Object with A<X> {}
''');
}
test_classTypeAlias_extends_function_invariant() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
typedef F<X> = X Function(X);
class A<X> {}
mixin M {}
class B<X> = A<F<X>> with M;
''',
[error(diag.wrongTypeParameterVarianceInSuperinterface, 63, 1)],
);
// ^
// [diag.wrongTypeParameterVarianceInSuperinterface] 'X' can't be used contravariantly or invariantly in 'A<F<X>>'.
''');
}
test_classTypeAlias_extends_function_parameterType() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
typedef F<X> = void Function(X);
class A<X> {}
mixin M {}
class B<X> = A<F<X>> with M;
''',
[error(diag.wrongTypeParameterVarianceInSuperinterface, 66, 1)],
);
// ^
// [diag.wrongTypeParameterVarianceInSuperinterface] 'X' can't be used contravariantly or invariantly in 'A<F<X>>'.
''');
}
test_classTypeAlias_extends_function_returnType() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
typedef F<X> = X Function();
class A<X> {}
mixin M {}
@@ -161,7 +155,7 @@ class B<X> = A<F<X>> with M;
}
test_classTypeAlias_extends_withoutFunction() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A<X> {}
mixin M {}
class B<X> = A<X> with M;
@@ -169,19 +163,18 @@ class B<X> = A<X> with M;
}
test_classTypeAlias_implements_function_parameterType() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
typedef F<X> = void Function(X);
class A<X> {}
mixin M {}
class B<X> = Object with M implements A<F<X>>;
''',
[error(diag.wrongTypeParameterVarianceInSuperinterface, 66, 1)],
);
// ^
// [diag.wrongTypeParameterVarianceInSuperinterface] 'X' can't be used contravariantly or invariantly in 'A<F<X>>'.
''');
}
test_classTypeAlias_implements_function_returnType() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
typedef F<X> = X Function();
class A<X> {}
mixin M {}
@@ -190,7 +183,7 @@ class B<X> = Object with M implements A<F<X>>;
}
test_classTypeAlias_implements_withoutFunction() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A<X> {}
mixin M {}
class B<X> = Object with M implements A<X>;
@@ -198,18 +191,17 @@ class B<X> = Object with M implements A<X>;
}
test_classTypeAlias_with_function_parameterType() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
typedef F<X> = void Function(X);
mixin M<X> {}
class B<X> = Object with M<F<X>>;
''',
[error(diag.wrongTypeParameterVarianceInSuperinterface, 55, 1)],
);
// ^
// [diag.wrongTypeParameterVarianceInSuperinterface] 'X' can't be used contravariantly or invariantly in 'M<F<X>>'.
''');
}
test_classTypeAlias_with_function_returnType() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
typedef F<X> = X Function();
mixin M<X> {}
class B<X> = Object with M<F<X>>;
@@ -217,27 +209,26 @@ class B<X> = Object with M<F<X>>;
}
test_classTypeAlias_with_withoutFunction() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
mixin M<X> {}
class B<X> = Object with M<X>;
''');
}
test_enum_implements_function_parameterType() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
typedef F<X> = void Function(X);
class A<X> {}
enum E<X> implements A<F<X>> {
// ^
// [diag.wrongTypeParameterVarianceInSuperinterface] 'X' can't be used contravariantly or invariantly in 'A<F<X>>'.
v
}
''',
[error(diag.wrongTypeParameterVarianceInSuperinterface, 54, 1)],
);
''');
}
test_enum_implements_function_returnType() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
typedef F<X> = X Function();
class A<X> {}
enum E<X> implements A<F<X>> {
@@ -247,7 +238,7 @@ enum E<X> implements A<F<X>> {
}
test_enum_implements_withoutFunction() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A<X> {}
enum E<X> implements A<X> {
v
@@ -256,20 +247,19 @@ enum E<X> implements A<X> {
}
test_enum_with_function_parameterType() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
typedef F<X> = void Function(X);
mixin A<X> {}
enum E<X> with A<F<X>> {
// ^
// [diag.wrongTypeParameterVarianceInSuperinterface] 'X' can't be used contravariantly or invariantly in 'A<F<X>>'.
v
}
''',
[error(diag.wrongTypeParameterVarianceInSuperinterface, 54, 1)],
);
''');
}
test_enum_with_function_returnType() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
typedef F<X> = X Function();
mixin A<X> {}
enum E<X> with A<F<X>> {
@@ -279,7 +269,7 @@ enum E<X> with A<F<X>> {
}
test_enum_with_withoutFunction() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
mixin A<X> {}
enum E<X> with A<X> {
v
@@ -288,18 +278,17 @@ enum E<X> with A<X> {
}
test_extensionType_contravariant() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A<T> {}
extension type B<T>(A<void Function(Object?)> it)
// ^
// [diag.wrongTypeParameterVarianceInSuperinterface] 'T' can't be used contravariantly or invariantly in 'A<void Function(T)>'.
implements A<void Function(T)> {}
''',
[error(diag.wrongTypeParameterVarianceInSuperinterface, 31, 1)],
);
''');
}
test_extensionType_covariant() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A<T> {}
extension type B<T>(A<Never Function()> it)
implements A<T Function()> {}
@@ -307,29 +296,27 @@ extension type B<T>(A<Never Function()> it)
}
test_extensionType_invariant() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A<T> {}
extension type B<T>(A<Never Function(Object?)> it)
// ^
// [diag.wrongTypeParameterVarianceInSuperinterface] 'T' can't be used contravariantly or invariantly in 'A<T Function(T)>'.
implements A<T Function(T)> {}
''',
[error(diag.wrongTypeParameterVarianceInSuperinterface, 31, 1)],
);
''');
}
test_mixin_implements_function_parameterType() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
typedef F<X> = void Function(X);
class A<X> {}
mixin B<X> implements A<F<X>> {}
''',
[error(diag.wrongTypeParameterVarianceInSuperinterface, 55, 1)],
);
// ^
// [diag.wrongTypeParameterVarianceInSuperinterface] 'X' can't be used contravariantly or invariantly in 'A<F<X>>'.
''');
}
test_mixin_implements_function_returnType() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
typedef F<X> = X Function();
class A<X> {}
mixin B<X> implements A<F<X>> {}
@@ -337,25 +324,24 @@ mixin B<X> implements A<F<X>> {}
}
test_mixin_implements_withoutFunction() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A<X> {}
mixin B<X> implements A<X> {}
''');
}
test_mixin_on_function_parameterType() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
typedef F<X> = void Function(X);
class A<X> {}
mixin B<X> on A<F<X>> {}
''',
[error(diag.wrongTypeParameterVarianceInSuperinterface, 55, 1)],
);
// ^
// [diag.wrongTypeParameterVarianceInSuperinterface] 'X' can't be used contravariantly or invariantly in 'A<F<X>>'.
''');
}
test_mixin_on_function_returnType() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
typedef F<X> = X Function();
class A<X> {}
mixin B<X> on A<F<X>> {}
@@ -363,19 +349,18 @@ mixin B<X> on A<F<X>> {}
}
test_mixin_on_withoutFunction() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
class A<X> {}
mixin B<X> on A<X> {}
''');
}
test_typeParameter_bound() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
class A<X> {}
class B<X> extends A<void Function<Y extends X>()> {}
''',
[error(diag.wrongTypeParameterVarianceInSuperinterface, 22, 1)],
);
// ^
// [diag.wrongTypeParameterVarianceInSuperinterface] 'X' can't be used contravariantly or invariantly in 'A<void Function<Y extends X>()>'.
''');
}
}
@@ -2,43 +2,42 @@
// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(YieldEachInNonGeneratorTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class YieldEachInNonGeneratorTest extends PubPackageResolutionTest {
test_async() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
f() async {
yield* 0;
//^^^^^^^^^
// [diag.yieldEachInNonGenerator] Yield-each statements must be in a generator function (one marked with either 'async*' or 'sync*').
}
''',
[error(diag.yieldEachInNonGenerator, 14, 9)],
);
''');
}
@FailingTest(
@SkippedTest(
reason:
'We are currently trying to parse the yield statement as a '
'binary expression.',
)
) // TODO(scheglov): review this
test_sync() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
f() {
yield* 0;
//^^^^^^^^^
// [diag.yieldEachInNonGenerator] Yield-each statements must be in a generator function (one marked with either 'async*' or 'sync*').
}
''',
[error(diag.yieldEachInNonGenerator, 0, 0)],
);
''');
}
}
@@ -6,28 +6,29 @@ import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(YieldInNonGeneratorTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class YieldInNonGeneratorTest extends PubPackageResolutionTest {
test_async() async {
await assertErrorsInCode(
r'''
await resolveTestCodeWithDiagnostics(r'''
f() async {
yield 0;
//^^^^^^^^
// [diag.yieldInNonGenerator] Yield statements must be in a generator function (one marked with either 'async*' or 'sync*').
}
''',
[error(diag.yieldInNonGenerator, 14, 8)],
);
''');
}
test_asyncStar() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
f() async* {
yield 0;
}
@@ -51,7 +52,7 @@ f() {
}
test_syncStar() async {
await assertNoErrorsInCode(r'''
await resolveTestCodeWithDiagnostics(r'''
f() sync* {
yield 0;
}
@@ -6,18 +6,20 @@ import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import '../dart/resolution/node_text_expectations.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(YieldOfInvalidTypeTest);
defineReflectiveTests(YieldOfInvalidTypeWithStrictCastsTest);
defineReflectiveTests(UpdateNodeTextExpectations);
});
}
@reflectiveTest
class YieldOfInvalidTypeTest extends PubPackageResolutionTest {
test_none_asyncStar_dynamic_to_streamInt() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
Stream<int> f(dynamic a) async* {
yield a;
}
@@ -25,18 +27,16 @@ Stream<int> f(dynamic a) async* {
}
test_none_asyncStar_int_to_basic() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
int f() async* {
// [diag.illegalAsyncGeneratorReturnType][column 1][length 3] Functions marked 'async*' must have a return type that is a supertype of 'Stream<T>' for some type 'T'.
yield 0;
}
''',
[error(diag.illegalAsyncGeneratorReturnType, 0, 3)],
);
''');
}
test_none_asyncStar_int_to_dynamic() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
dynamic f() async* {
yield 0;
}
@@ -44,18 +44,16 @@ dynamic f() async* {
}
test_none_asyncStar_int_to_iterableDynamic() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
Iterable<int> f() async* {
// [diag.illegalAsyncGeneratorReturnType][column 1][length 13] Functions marked 'async*' must have a return type that is a supertype of 'Stream<T>' for some type 'T'.
yield 0;
}
''',
[error(diag.illegalAsyncGeneratorReturnType, 0, 13)],
);
''');
}
test_none_asyncStar_int_to_streamDynamic() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
Stream f() async* {
yield 0;
}
@@ -63,7 +61,7 @@ Stream f() async* {
}
test_none_asyncStar_int_to_streamInt() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
Stream<int> f() async* {
yield 0;
}
@@ -71,32 +69,30 @@ Stream<int> f() async* {
}
test_none_asyncStar_int_to_streamString() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
Stream<String> f() async* {
yield 0;
// ^
// [diag.yieldOfInvalidType] A yielded value of type 'int' must be assignable to 'String'.
}
''',
[error(diag.yieldOfInvalidType, 36, 1)],
);
''');
}
test_none_asyncStar_int_to_streamString_functionExpression() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
void f() {
// ignore:unused_local_variable
Stream<String> Function() v = () async* {
yield 1;
// ^
// [diag.yieldOfInvalidType] A yielded value of type 'int' must be assignable to 'String'.
};
}
''',
[error(diag.yieldOfInvalidType, 99, 1)],
);
''');
}
test_none_asyncStar_int_to_untyped() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
f() async* {
yield 0;
}
@@ -104,37 +100,35 @@ f() async* {
}
test_none_asyncStar_null_to_streamInt() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
Stream<int> f() async* {
yield null;
// ^^^^
// [diag.yieldOfInvalidType] A yielded value of type 'Null' must be assignable to 'int'.
}
''',
[error(diag.yieldOfInvalidType, 33, 4)],
);
''');
}
test_none_asyncStar_to_futureOrNullableStream() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
import 'dart:async';
FutureOr<Stream<int>?> f() async* {
yield 3.14;
// ^^^^
// [diag.yieldOfInvalidType] A yielded value of type 'double' must be assignable to 'int'.
yield '2';
// ^^^
// [diag.yieldOfInvalidType] A yielded value of type 'String' must be assignable to 'int'.
yield Future<int>.value(0);
// ^^^^^^^^^^^^^^^^^^^^
// [diag.yieldOfInvalidType] A yielded value of type 'Future<int>' must be assignable to 'int'.
}
''',
[
error(diag.yieldOfInvalidType, 66, 4),
error(diag.yieldOfInvalidType, 80, 3),
error(diag.yieldOfInvalidType, 93, 20),
],
);
''');
}
test_none_syncStar_dynamic_to_iterableInt() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
Iterable<int> f(dynamic a) sync* {
yield a;
}
@@ -142,18 +136,16 @@ Iterable<int> f(dynamic a) sync* {
}
test_none_syncStar_int_to_basic() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
int f() sync* {
// [diag.illegalSyncGeneratorReturnType][column 1][length 3] Functions marked 'sync*' must have a return type that is a supertype of 'Iterable<T>' for some type 'T'.
yield 0;
}
''',
[error(diag.illegalSyncGeneratorReturnType, 0, 3)],
);
''');
}
test_none_syncStar_int_to_dynamic() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
dynamic f() sync* {
yield 0;
}
@@ -161,7 +153,7 @@ dynamic f() sync* {
}
test_none_syncStar_int_to_iterableDynamic() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
Iterable f() sync* {
yield 0;
}
@@ -169,7 +161,7 @@ Iterable f() sync* {
}
test_none_syncStar_int_to_iterableInt() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
Iterable<int> f() sync* {
yield 0;
}
@@ -177,43 +169,39 @@ Iterable<int> f() sync* {
}
test_none_syncStar_int_to_iterableString() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
Iterable<String> f() sync* {
yield 0;
// ^
// [diag.yieldOfInvalidType] A yielded value of type 'int' must be assignable to 'String'.
}
''',
[error(diag.yieldOfInvalidType, 37, 1)],
);
''');
}
test_none_syncStar_int_to_iterableString_functionExpression() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
void f() {
// ignore:unused_local_variable
Iterable<String> Function() v = () sync* {
yield 1;
// ^
// [diag.yieldOfInvalidType] A yielded value of type 'int' must be assignable to 'String'.
};
}
''',
[error(diag.yieldOfInvalidType, 100, 1)],
);
''');
}
test_none_syncStar_int_to_stream() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
Stream<int> f() sync* {
// [diag.illegalSyncGeneratorReturnType][column 1][length 11] Functions marked 'sync*' must have a return type that is a supertype of 'Iterable<T>' for some type 'T'.
yield 0;
}
''',
[error(diag.illegalSyncGeneratorReturnType, 0, 11)],
);
''');
}
test_none_syncStar_int_to_untyped() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
f() sync* {
yield 0;
}
@@ -221,26 +209,25 @@ f() sync* {
}
test_none_syncStar_to_futureOrNullableIterable() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
import 'dart:async';
FutureOr<Iterable<int>?> f() sync* {
yield 3.14;
// ^^^^
// [diag.yieldOfInvalidType] A yielded value of type 'double' must be assignable to 'int'.
yield '2';
// ^^^
// [diag.yieldOfInvalidType] A yielded value of type 'String' must be assignable to 'int'.
yield Future<int>.value(0);
// ^^^^^^^^^^^^^^^^^^^^
// [diag.yieldOfInvalidType] A yielded value of type 'Future<int>' must be assignable to 'int'.
}
''',
[
error(diag.yieldOfInvalidType, 67, 4),
error(diag.yieldOfInvalidType, 81, 3),
error(diag.yieldOfInvalidType, 94, 20),
],
);
''');
}
test_star_asyncStar_dynamic_to_dynamic() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
f() async* {
yield* g();
}
@@ -250,7 +237,7 @@ g() => throw 0;
}
test_star_asyncStar_dynamic_to_streamDynamic() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
Stream f() async* {
yield* g();
}
@@ -260,7 +247,7 @@ g() => throw 0;
}
test_star_asyncStar_dynamic_to_streamInt() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
Stream<int> f() async* {
yield* g();
}
@@ -270,54 +257,50 @@ g() => throw 0;
}
test_star_asyncStar_int_to_dynamic() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f() async* {
yield* 0;
// ^
// [diag.yieldEachOfInvalidType] The type 'int' implied by the 'yield*' expression must be assignable to 'Stream<dynamic>'.
}
''',
[error(diag.yieldEachOfInvalidType, 22, 1)],
);
''');
}
test_star_asyncStar_iterableInt_to_dynamic() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f() async* {
var a = <int>[];
yield* a;
// ^
// [diag.yieldEachOfInvalidType] The type 'List<int>' implied by the 'yield*' expression must be assignable to 'Stream<dynamic>'.
}
''',
[error(diag.yieldEachOfInvalidType, 41, 1)],
);
''');
}
test_star_asyncStar_iterableInt_to_streamInt() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
Stream<int> f() async* {
var a = <int>[];
yield* a;
// ^
// [diag.yieldEachOfInvalidType] The type 'List<int>' implied by the 'yield*' expression must be assignable to 'Stream<int>'.
}
''',
[error(diag.yieldEachOfInvalidType, 53, 1)],
);
''');
}
test_star_asyncStar_iterableString_to_streamInt() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
Stream<int> f() async* {
var a = <String>[];
yield* a;
// ^
// [diag.yieldEachOfInvalidType] The type 'List<String>' implied by the 'yield*' expression must be assignable to 'Stream<int>'.
}
''',
[error(diag.yieldEachOfInvalidType, 56, 1)],
);
''');
}
test_star_asyncStar_streamDynamic_to_dynamic() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
f() async* {
yield* g();
}
@@ -327,20 +310,19 @@ Stream g() => throw 0;
}
test_star_asyncStar_streamDynamic_to_streamInt() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
Stream<int> f() async* {
yield* g();
// ^^^
// [diag.yieldEachOfInvalidType] The type 'Stream<dynamic>' implied by the 'yield*' expression must be assignable to 'Stream<int>'.
}
Stream g() => throw 0;
''',
[error(diag.yieldEachOfInvalidType, 34, 3)],
);
''');
}
test_star_asyncStar_streamInt_to_dynamic() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
f() async* {
yield* g();
}
@@ -350,7 +332,7 @@ Stream<int> g() => throw 0;
}
test_star_asyncStar_streamInt_to_streamInt() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
Stream<int> f() async* {
yield* g();
}
@@ -360,20 +342,19 @@ Stream<int> g() => throw 0;
}
test_star_asyncStar_streamString_to_streamInt() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
Stream<int> f() async* {
yield* g();
// ^^^
// [diag.yieldEachOfInvalidType] The type 'Stream<String>' implied by the 'yield*' expression must be assignable to 'Stream<int>'.
}
Stream<String> g() => throw 0;
''',
[error(diag.yieldEachOfInvalidType, 34, 3)],
);
''');
}
test_star_syncStar_dynamic_to_dynamic() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
f() sync* {
yield* g();
}
@@ -383,7 +364,7 @@ g() => throw 0;
}
test_star_syncStar_dynamic_to_iterableDynamic() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
Iterable f() sync* {
yield* g();
}
@@ -393,7 +374,7 @@ g() => throw 0;
}
test_star_syncStar_dynamic_to_iterableInt() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
Iterable<int> f() sync* {
yield* g();
}
@@ -403,32 +384,30 @@ g() => throw 0;
}
test_star_syncStar_int() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
f() sync* {
yield* 0;
// ^
// [diag.yieldEachOfInvalidType] The type 'int' implied by the 'yield*' expression must be assignable to 'Iterable<dynamic>'.
}
''',
[error(diag.yieldEachOfInvalidType, 21, 1)],
);
''');
}
test_star_syncStar_int_closure() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
main() {
var f = () sync* {
yield* 0;
// ^
// [diag.yieldEachOfInvalidType] The type 'int' implied by the 'yield*' expression must be assignable to 'Iterable<dynamic>'.
};
f;
}
''',
[error(diag.yieldEachOfInvalidType, 41, 1)],
);
''');
}
test_star_syncStar_iterableDynamic_to_dynamic() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
f() sync* {
yield* g();
}
@@ -438,20 +417,19 @@ Iterable g() => throw 0;
}
test_star_syncStar_iterableDynamic_to_iterableInt() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
Iterable<int> f() sync* {
yield* g();
// ^^^
// [diag.yieldEachOfInvalidType] The type 'Iterable<dynamic>' implied by the 'yield*' expression must be assignable to 'Iterable<int>'.
}
Iterable g() => throw 0;
''',
[error(diag.yieldEachOfInvalidType, 35, 3)],
);
''');
}
test_star_syncStar_iterableInt_to_dynamic() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
f() sync* {
yield* g();
}
@@ -461,7 +439,7 @@ Iterable<int> g() => throw 0;
}
test_star_syncStar_iterableInt_to_iterableInt() async {
await assertNoErrorsInCode('''
await resolveTestCodeWithDiagnostics('''
Iterable<int> f() sync* {
yield* g();
}
@@ -471,16 +449,15 @@ Iterable<int> g() => throw 0;
}
test_star_syncStar_iterableString_to_iterableInt() async {
await assertErrorsInCode(
'''
await resolveTestCodeWithDiagnostics('''
Iterable<int> f() sync* {
yield* g();
// ^^^
// [diag.yieldEachOfInvalidType] The type 'Iterable<String>' implied by the 'yield*' expression must be assignable to 'Iterable<int>'.
}
Iterable<String> g() => throw 0;
''',
[error(diag.yieldEachOfInvalidType, 35, 3)],
);
''');
}
}