AST. Add tests for variable, type, and naming lint rules. These tests were written while migrating the lint rules to the V2 AST. They pass with the current V1 implementations, so they land first and pin the behavior that the migration has to keep. Change-Id: I36d9d531e20158bd75b46e21a80414de1cebf658 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/586761 Reviewed-by: Paul Berry <paulberry@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/linter/test/rules/always_specify_types_test.dart b/pkg/linter/test/rules/always_specify_types_test.dart index fe8d3c8..e7147de 100644 --- a/pkg/linter/test/rules/always_specify_types_test.dart +++ b/pkg/linter/test/rules/always_specify_types_test.dart
@@ -294,6 +294,22 @@ '''); } + test_constructorTearoff_implicitInstantiation() async { + // Known false negative: the type arguments of a constructor tear-off that + // are inferred from the context are not reported. + // See https://github.com/dart-lang/sdk/issues/65178. + await assertNoDiagnostics(r''' +class C<T> { + C(); + C.named(); +} +void f() { + C<int> Function() g = C.new; + C<int> Function() h = C.named; +} +'''); + } + test_constructorTearoff_keptGeneric() async { await assertNoDiagnostics(r''' void f() { @@ -1039,6 +1055,18 @@ '''); } + test_redirectingFactory_implicitTypeArgument() async { + await assertDiagnosticsFromMarkup(r''' +class A<T> { + A.a(); + factory A() = [!B!]; +} +class B<T> extends A<T> { + B() : super.a(); +} +'''); + } + test_setLiteral() async { await assertDiagnosticsFromMarkup(r''' f() {
diff --git a/pkg/linter/test/rules/avoid_shadowing_type_parameters_test.dart b/pkg/linter/test/rules/avoid_shadowing_type_parameters_test.dart index a7cad67..016dfb0 100644 --- a/pkg/linter/test/rules/avoid_shadowing_type_parameters_test.dart +++ b/pkg/linter/test/rules/avoid_shadowing_type_parameters_test.dart
@@ -18,6 +18,17 @@ @override String get lintRule => LintNames.avoid_shadowing_type_parameters; + test_closure_shadowingClass() async { + await assertDiagnosticsFromMarkup(r''' +class C<T> { + void f() { + var g = <[!T!]>() {}; + g<int>(); + } +} +'''); + } + test_enclosingElementsWithoutTypeParameters() async { // Make sure we don't hit any null pointers when none of a function or // method's ancestors have type parameters. @@ -84,6 +95,15 @@ '''); } + test_localFunction_shadowingClosure() async { + await assertDiagnosticsFromMarkup(r''' +var f = <T>() { + void g<[!T!]>() {} + g<int>(); +}; +'''); + } + test_localFunction_shadowingFunction() async { await assertDiagnosticsFromMarkup(r''' void f<T>() {
diff --git a/pkg/linter/test/rules/avoid_types_as_parameter_names_test.dart b/pkg/linter/test/rules/avoid_types_as_parameter_names_test.dart index f78ebf3..6601d62 100644 --- a/pkg/linter/test/rules/avoid_types_as_parameter_names_test.dart +++ b/pkg/linter/test/rules/avoid_types_as_parameter_names_test.dart
@@ -2,6 +2,7 @@ // 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 '../rule_test_support.dart'; @@ -35,6 +36,24 @@ '''); } + test_classTypeParameter_operatorFunctionTypedParameter() async { + await assertNoDiagnostics(r''' +// @dart = 3.14 +class A<X> { + int operator +(int g<X>()) => 0; +} +'''); + } + + test_classTypeParameter_setterFunctionTypedParameter() async { + await assertNoDiagnostics(r''' +// @dart = 3.14 +class A<X> { + set s(void g<X>()) {} +} +'''); + } + test_classTypeParameter_staticMethodTypeParameter() async { await assertNoDiagnostics(r''' class A<X> { @@ -231,12 +250,33 @@ '''); } + test_typeParameter_closure() async { + await assertDiagnosticsFromMarkup(r''' +var f = <[!int!]>() {}; +'''); + } + + test_typeParameter_extension() async { + // Known false negative: the scope walk does not stop at extensions, so + // their type parameters are never checked. + // See https://github.com/dart-lang/sdk/issues/65178. + await assertNoDiagnostics(r''' +extension E<int> on String {} +'''); + } + test_typeParameter_function() async { await assertDiagnosticsFromMarkup(r''' void f<[!int!]>() {} '''); } + test_typeParameter_genericFunctionType() async { + await assertDiagnosticsFromMarkup(r''' +void Function<[!int!]>()? f; +'''); + } + test_typeParameter_instanceMethod_class() async { await assertDiagnosticsFromMarkup(r''' class A<X> { @@ -247,6 +287,82 @@ '''); } + test_typeParameter_localFunction() async { + await assertDiagnosticsFromMarkup(r''' +void f() { + void g<[!int!]>() {} + g<String>(); +} +'''); + } + + test_typeParameter_localFunction_enclosingTypeParameter() async { + await assertDiagnosticsFromMarkup(r''' +void f<T>() { + void g<[!T!]>() {} + g<int>(); +} +'''); + } + + test_typeParameter_mixin() async { + // Known false negative: the scope walk does not stop at mixins, so their + // type parameters are never checked. + // See https://github.com/dart-lang/sdk/issues/65178. + await assertNoDiagnostics(r''' +mixin M<int> {} +'''); + } + + test_typeParameter_operator() async { + await assertDiagnostics( + r''' +class A { + int operator +<int>(other) => 0; +} +''', + [ + error(diag.typeParameterOnOperator, 26, 5), + lint(27, 3), + error(diag.returnOfInvalidTypeFromMethod, 42, 1), + ], + ); + } + + test_typeParameter_operatorFunctionTypedParameter_extension() async { + await assertDiagnosticsFromMarkup(r''' +// @dart = 3.14 +extension E on int { + int operator +(int g<[!int!]>()) => 0; +} +'''); + } + + test_typeParameter_setterFunctionTypedParameter() async { + await assertDiagnosticsFromMarkup(r''' +// @dart = 3.14 +class A { + set s(void g<[!int!]>()) {} +} +'''); + } + + test_typeParameter_setterFunctionTypedParameter_extension() async { + await assertDiagnosticsFromMarkup(r''' +// @dart = 3.14 +extension E on int { + set s(void g<[!int!]>()) {} +} +'''); + } + + test_typeParameter_topLevelSetterFunctionTypedParameter() async { + await assertDiagnosticsFromMarkup(r''' +// @dart = 3.14 +set s(void g<[!int!]>()) {} +'''); + } + test_typeParameter_wildcard() async { await assertNoDiagnostics(r''' class C<_> {
diff --git a/pkg/linter/test/rules/constant_identifier_names_test.dart b/pkg/linter/test/rules/constant_identifier_names_test.dart index 34b5c75..db6d45d 100644 --- a/pkg/linter/test/rules/constant_identifier_names_test.dart +++ b/pkg/linter/test/rules/constant_identifier_names_test.dart
@@ -156,6 +156,23 @@ '''); } + test_localConstant() async { + await assertDiagnosticsFromMarkup(r''' +void f() { + const [!DEBUG!] = 1; + DEBUG; +} +'''); + } + + test_localConstant_forParts() async { + await assertDiagnosticsFromMarkup(r''' +void f() { + for (const [!DEBUG!] = 1; DEBUG > 2;) {} +} +'''); + } + test_recordFieldDestructured() async { await assertDiagnosticsFromMarkup(r''' f(Object o) {
diff --git a/pkg/linter/test/rules/no_leading_underscores_for_library_prefixes_test.dart b/pkg/linter/test/rules/no_leading_underscores_for_library_prefixes_test.dart index 9efa0e8..6d1116b 100644 --- a/pkg/linter/test/rules/no_leading_underscores_for_library_prefixes_test.dart +++ b/pkg/linter/test/rules/no_leading_underscores_for_library_prefixes_test.dart
@@ -45,6 +45,17 @@ ''', code: diag.noLeadingUnderscoresForLibraryPrefixes); } + test_notShadow_field_namedType() async { + await assertDiagnosticsFromMarkup(r''' +import 'dart:math' as [!_foo!]; + +class C { + int? foo; + void f(_foo.Random r) {} +} +''', code: diag.noLeadingUnderscoresForLibraryPrefixes); + } + test_notShadow_noConflict() async { await assertDiagnosticsFromMarkup(''' import 'dart:math' as [!_foo!]; @@ -78,6 +89,68 @@ ''', code: diag.noLeadingUnderscoresForLibraryPrefixesShadowed); } + test_shadow_field_annotation() async { + newFile('$testPackageLibPath/a.dart', r''' +class A { + const A(); +} +'''); + await assertDiagnosticsFromMarkup(r''' +import 'a.dart' as [!_foo!]; + +class C { + int? foo; + + @_foo.A() + void f() {} +} +''', code: diag.noLeadingUnderscoresForLibraryPrefixesShadowed); + } + + test_shadow_field_assignment() async { + await assertDiagnosticsFromMarkup(r''' +import 'dart:io' as [!_foo!]; + +class C { + int? foo; + void f() { + _foo.exitCode = 0; + } +} +''', code: diag.noLeadingUnderscoresForLibraryPrefixesShadowed); + } + + test_shadow_field_invocation() async { + await assertDiagnosticsFromMarkup(r''' +import 'dart:math' as [!_foo!]; + +class C { + int? foo; + void f() { + _foo.sqrt(1); + } +} +''', code: diag.noLeadingUnderscoresForLibraryPrefixesShadowed); + } + + test_shadow_field_staticQualifier() async { + newFile('$testPackageLibPath/a.dart', r''' +class A { + static int x = 0; +} +'''); + await assertDiagnosticsFromMarkup(r''' +import 'a.dart' as [!_foo!]; + +class C { + int? foo; + void f() { + _foo.A.x; + } +} +''', code: diag.noLeadingUnderscoresForLibraryPrefixesShadowed); + } + test_shadow_parameter() async { await assertDiagnosticsFromMarkup(''' import 'dart:math' as [!_foo!];
diff --git a/pkg/linter/test/rules/no_leading_underscores_for_local_identifiers_test.dart b/pkg/linter/test/rules/no_leading_underscores_for_local_identifiers_test.dart index 053f186..e3a60e8 100644 --- a/pkg/linter/test/rules/no_leading_underscores_for_local_identifiers_test.dart +++ b/pkg/linter/test/rules/no_leading_underscores_for_local_identifiers_test.dart
@@ -95,6 +95,14 @@ '''); } + test_forElement_variable() async { + await assertDiagnosticsFromMarkup(r''' +void f() { + [for (var [!_i!] = 0; ;) 0]; +} +'''); + } + test_forLoop_firstVariable() async { await assertDiagnosticsFromMarkup(r''' void f() { @@ -418,6 +426,17 @@ ''', code: diag.noLeadingUnderscoresForLocalIdentifiersShadowed); } + test_shadow_localFunction() async { + await assertDiagnosticsFromMarkup(''' +void f() { + int foo = 1; + void [!_foo!]() {} + _foo(); + foo; +} +''', code: diag.noLeadingUnderscoresForLocalIdentifiersShadowed); + } + test_shadow_member() async { await assertDiagnosticsFromMarkup(''' class A {
diff --git a/pkg/linter/test/rules/no_raw_types_test.dart b/pkg/linter/test/rules/no_raw_types_test.dart index 4bb11aa..65cc523 100644 --- a/pkg/linter/test/rules/no_raw_types_test.dart +++ b/pkg/linter/test/rules/no_raw_types_test.dart
@@ -151,7 +151,7 @@ test_localVariable_extensionType_missingTypeArg() async { await assertDiagnosticsFromMarkup(r''' extension type E<T>(int i) {} - + void f() { [!E!] e = E(1); } @@ -161,7 +161,7 @@ test_localVariable_extensionType_withTypeArg() async { await assertNoDiagnostics(r''' extension type E<T>(int i) {} - + void f() { E<int> e = E<int>(1); } @@ -285,6 +285,48 @@ '''); } + test_redirectingFactoryConstructor_missingTypeArg() async { + await assertDiagnosticsFromMarkup(r''' +class A { + A(); + factory A.b() = [!B!]; +} +class B<T> implements A {} +'''); + } + + test_redirectingFactoryConstructor_typeAlias_fullyInstantiated() async { + await assertNoDiagnostics(r''' +class A { + A(); + factory A.b() = G; +} +class B<T> implements A {} +typedef G = B<dynamic>; +'''); + } + + test_redirectingFactoryConstructor_typeAlias_missingTypeArg() async { + await assertDiagnosticsFromMarkup(r''' +class A { + A(); + factory A.b() = [!E!]; +} +class B<T> implements A {} +typedef E<T> = B<T>; +'''); + } + + test_redirectingFactoryConstructor_withTypeArg() async { + await assertNoDiagnostics(r''' +class A { + A(); + factory A.b() = B<int>; +} +class B<T> implements A {} +'''); + } + test_returnType_missingTypeArg() async { await assertDiagnosticsFromMarkup(r''' [!List!] f(int a) => [1, 2, 3];
diff --git a/pkg/linter/test/rules/no_wildcard_variable_uses_test.dart b/pkg/linter/test/rules/no_wildcard_variable_uses_test.dart index 23fc994..840482f 100644 --- a/pkg/linter/test/rules/no_wildcard_variable_uses_test.dart +++ b/pkg/linter/test/rules/no_wildcard_variable_uses_test.dart
@@ -17,6 +17,16 @@ @override String get lintRule => LintNames.no_wildcard_variable_uses; + test_commentReference_param() async { + await assertDiagnosticsFromMarkup(r''' +// @dart = 3.4 +// (pre wildcard-variables) + +/// [[!_!]]. +void f(int _) {} +'''); + } + test_constructor() async { await assertNoDiagnostics(r''' // @dart = 3.4 @@ -117,6 +127,72 @@ '''); } + test_param_assignment() async { + await assertDiagnosticsFromMarkup(r''' +// @dart = 3.4 +// (pre wildcard-variables) + +void f(int _) { + [!_!] = 1; +} +'''); + } + + test_param_call() async { + await assertDiagnosticsFromMarkup(r''' +// @dart = 3.4 +// (pre wildcard-variables) + +void f(void Function() _) { + [!_!](); +} +'''); + } + + test_param_compoundAssignment() async { + await assertDiagnosticsFromMarkup(r''' +// @dart = 3.4 +// (pre wildcard-variables) + +void f(int _) { + [!_!] += 1; +} +'''); + } + + test_param_forIn() async { + await assertDiagnosticsFromMarkup(r''' +// @dart = 3.4 +// (pre wildcard-variables) + +void f(int _) { + for ([!_!] in [1]) {} +} +'''); + } + + test_param_increment() async { + await assertDiagnosticsFromMarkup(r''' +// @dart = 3.4 +// (pre wildcard-variables) + +void f(int _) { + [!_!]++; +} +'''); + } + + test_param_receiver() async { + await assertDiagnosticsFromMarkup(r''' +// @dart = 3.4 +// (pre wildcard-variables) + +void f(int _) { + [!_!].isEven; +} +'''); + } + test_topLevelFunction() async { await assertNoDiagnostics(r''' // @dart = 3.4
diff --git a/pkg/linter/test/rules/non_constant_identifier_names_test.dart b/pkg/linter/test/rules/non_constant_identifier_names_test.dart index b270efa..84f54c5 100644 --- a/pkg/linter/test/rules/non_constant_identifier_names_test.dart +++ b/pkg/linter/test/rules/non_constant_identifier_names_test.dart
@@ -646,6 +646,31 @@ ); } + test_instanceGetter_upper() async { + await assertDiagnosticsFromMarkup(r''' +class A { + int get [!Foo!] => 0; +} +'''); + } + + test_instanceSetter_upper() async { + await assertDiagnosticsFromMarkup(r''' +class A { + set [!Foo!](int _) {} +} +'''); + } + + test_localFunction_upper() async { + await assertDiagnosticsFromMarkup(r''' +void f() { + void [!Foo!]() {} + Foo(); +} +'''); + } + test_localVariable_const_allCaps() async { await assertNoDiagnostics(r''' void f() { @@ -770,6 +795,18 @@ '''); } + test_topLevelGetter_upper() async { + await assertDiagnosticsFromMarkup(r''' +int get [!Foo!] => 0; +'''); + } + + test_topLevelSetter_upper() async { + await assertDiagnosticsFromMarkup(r''' +set [!Foo!](int _) {} +'''); + } + test_topLevelVariable_constant_upper() async { await assertNoDiagnostics(r''' const ZZZ = 4;
diff --git a/pkg/linter/test/rules/omit_local_variable_types_test.dart b/pkg/linter/test/rules/omit_local_variable_types_test.dart index ea4562a..efc836e 100644 --- a/pkg/linter/test/rules/omit_local_variable_types_test.dart +++ b/pkg/linter/test/rules/omit_local_variable_types_test.dart
@@ -225,6 +225,20 @@ } /// https://github.com/dart-lang/linter/issues/3016 + test_typeNeededForInference_importPrefixed() async { + newFile('$testPackageLibPath/a.dart', r''' +T f<T>() => throw 0; +'''); + await assertNoDiagnostics(r''' +import 'a.dart' as a; +void g() { + int x = a.f(); + x; +} +'''); + } + + /// https://github.com/dart-lang/linter/issues/3016 test_typeParamProvided() async { await assertDiagnosticsFromMarkup(r''' T bar<T>(dynamic d) => d;
diff --git a/pkg/linter/test/rules/omit_obvious_local_variable_types_test.dart b/pkg/linter/test/rules/omit_obvious_local_variable_types_test.dart index 1f483d1..dd8584a 100644 --- a/pkg/linter/test/rules/omit_obvious_local_variable_types_test.dart +++ b/pkg/linter/test/rules/omit_obvious_local_variable_types_test.dart
@@ -2,6 +2,7 @@ // 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 '../rule_test_support.dart'; @@ -69,6 +70,60 @@ '''); } + test_dotShorthand_hashCode() async { + await assertDiagnostics( + r''' +class A { + static A get hashCode => A(); +} +void f() { + A x = .hashCode; + x; +} +''', + [error(diag.conflictingStaticAndInstance, 25, 8)], + ); + } + + test_dotShorthand_hashCode_unresolved() async { + await assertDiagnostics( + r''' +void f() { + int x = .hashCode; + x; +} +''', + [error(diag.undefinedStaticMemberReadNotFoundDotShorthand, 22, 8)], + ); + } + + test_dotShorthand_toString() async { + await assertDiagnostics( + r''' +class A { + static A toString() => A(); +} +void f() { + A x = .toString(); + x; +} +''', + [error(diag.conflictingStaticAndInstance, 21, 8)], + ); + } + + test_dotShorthand_toString_unresolved() async { + await assertDiagnostics( + r''' +void f() { + String x = .toString(); + x; +} +''', + [error(diag.undefinedStaticMemberReadNotFoundDotShorthand, 25, 8)], + ); + } + test_forEach_inferredList() async { await assertDiagnosticsFromMarkup(r''' f() {
diff --git a/pkg/linter/test/rules/parameter_assignments_test.dart b/pkg/linter/test/rules/parameter_assignments_test.dart index 79f3f8b..9df0eba 100644 --- a/pkg/linter/test/rules/parameter_assignments_test.dart +++ b/pkg/linter/test/rules/parameter_assignments_test.dart
@@ -382,6 +382,17 @@ '''); } + test_operator_assignment() async { + await assertDiagnosticsFromMarkup(r''' +class A { + int operator +(int other) { + [!other = 5!]; + return other; + } +} +'''); + } + test_postfixOperation() async { await assertDiagnosticsFromMarkup(r''' void f(int p) { @@ -433,4 +444,12 @@ } '''); } + + test_topLevelSetter_assignment() async { + await assertDiagnosticsFromMarkup(r''' +set x(int value) { + [!value = 5!]; +} +'''); + } }
diff --git a/pkg/linter/test/rules/prefer_const_declarations_test.dart b/pkg/linter/test/rules/prefer_const_declarations_test.dart index 98cde68..b73d624 100644 --- a/pkg/linter/test/rules/prefer_const_declarations_test.dart +++ b/pkg/linter/test/rules/prefer_const_declarations_test.dart
@@ -54,6 +54,16 @@ '''); } + test_forLoopVariable_final_intLiteral() async { + // Known false negative: `for` loop variable declarations are not visited. + // See https://github.com/dart-lang/sdk/issues/65178. + await assertNoDiagnostics(r''' +void f() { + for (final a = 1; a < 2;) {} +} +'''); + } + test_instanceField_final_listLiteral_const() async { await assertNoDiagnostics(r''' class C { @@ -70,6 +80,15 @@ '''); } + test_localVariable_final_annotated() async { + await assertDiagnosticsFromMarkup(r''' +const a = 0; +void f() { + [!@a final b = 1!]; +} +'''); + } + test_localVariable_final_constructorInvocation_const() async { await assertDiagnosticsFromMarkup(r''' void f() { @@ -185,6 +204,14 @@ '''); } + test_localVariable_late_final() async { + await assertDiagnosticsFromMarkup(r''' +void f() { + [!late final a = 1!]; +} +'''); + } + test_localVariable_listLiteral_final_typed() async { await assertNoDiagnostics(r''' void f() { @@ -271,6 +298,16 @@ '''); } + test_staticField_final_annotated_multiple() async { + await assertDiagnosticsFromMarkup(r''' +const a = 0; +class C { + @a + static [!final b = 1, c = 2!]; +} +'''); + } + test_staticField_final_listLiteral() async { await assertDiagnosticsFromMarkup(r''' class C { @@ -295,6 +332,14 @@ '''); } + test_staticField_late_final() async { + await assertDiagnosticsFromMarkup(r''' +class C { + static [!late final a = 1!]; +} +'''); + } + test_staticFunctionTearoff_inference() async { await assertDiagnosticsFromMarkup(r''' void f() { @@ -351,6 +396,14 @@ '''); } + test_topLevelVariable_final_annotated_multiple() async { + await assertDiagnosticsFromMarkup(r''' +const a = 0; +@a +[!final b = 1, c = 2!]; +'''); + } + test_topLevelVariable_final_doubleLiteral() async { await assertDiagnosticsFromMarkup(r''' [!final x = 1.3!]; @@ -394,6 +447,12 @@ '''); } + test_topLevelVariable_late_final() async { + await assertDiagnosticsFromMarkup(r''' +[!late final a = 1!]; +'''); + } + test_topLevelVariable_listLiteral_const() async { await assertNoDiagnostics(r''' const x = const [];
diff --git a/pkg/linter/test/rules/prefer_final_locals_test.dart b/pkg/linter/test/rules/prefer_final_locals_test.dart index cf75df7..016c884 100644 --- a/pkg/linter/test/rules/prefer_final_locals_test.dart +++ b/pkg/linter/test/rules/prefer_final_locals_test.dart
@@ -329,6 +329,43 @@ '''); } + test_forElement_notInFunction() async { + // Known false negative: variables outside of a function body are skipped, + // even though `i` is never reassigned. + // See https://github.com/dart-lang/sdk/issues/65178. + await assertNoDiagnostics(r''' +var a = [for (var i = 0; ;) i]; +'''); + } + + test_forElement_notReassigned() async { + await assertDiagnosticsFromMarkup(r''' +void f() { + [for ([!var!] i = 0; ;) i]; +} +'''); + } + + test_forLoop_notReassigned() async { + await assertDiagnosticsFromMarkup(r''' +void f() { + for ([!var!] i = 0; ;) { + print(i); + } +} +'''); + } + + test_forLoop_reassigned() async { + await assertNoDiagnostics(r''' +void f() { + for (var i = 0; ; i++) { + print(i); + } +} +'''); + } + test_ifPatternList() async { await assertDiagnosticsFromMarkup(r''' f(Object o) {
diff --git a/pkg/linter/test/rules/prefer_final_parameters_test.dart b/pkg/linter/test/rules/prefer_final_parameters_test.dart index 1ab77ab..6bde45a 100644 --- a/pkg/linter/test/rules/prefer_final_parameters_test.dart +++ b/pkg/linter/test/rules/prefer_final_parameters_test.dart
@@ -159,6 +159,29 @@ '''); } + test_localFunction() async { + await assertDiagnosticsFromMarkup(r''' +// @dart=3.12 +void f() { + void g([!int a!]) {} + g(1); +} +'''); + } + + test_localFunction_reassigned() async { + await assertNoDiagnostics(r''' +// @dart=3.12 +void f() { + void g(int a) { + a++; + } + + g(1); +} +'''); + } + test_method() async { await assertDiagnosticsFromMarkup(r''' // @dart=3.12 @@ -415,6 +438,13 @@ void f(int _){ } '''); } + + test_topLevelSetter() async { + await assertDiagnosticsFromMarkup(r''' +// @dart=3.12 +set s([!int v!]) {} +'''); + } } @reflectiveTest
diff --git a/pkg/linter/test/rules/prefer_void_to_null_test.dart b/pkg/linter/test/rules/prefer_void_to_null_test.dart index 9e0dc0a..e074ee0 100644 --- a/pkg/linter/test/rules/prefer_void_to_null_test.dart +++ b/pkg/linter/test/rules/prefer_void_to_null_test.dart
@@ -151,6 +151,18 @@ '''); } + test_functionTypeParameter_null_named() async { + await assertNoDiagnostics(r''' +void f(void Function({Null x}) g) {} +'''); + } + + test_functionTypeParameter_null_optionalPositional() async { + await assertNoDiagnostics(r''' +void f(void Function([Null]) g) {} +'''); + } + test_instanceField_futureOfNull() async { await assertDiagnosticsFromMarkup(r''' class C { @@ -335,6 +347,18 @@ '''); } + test_operatorReturnType_overrideChangingType() async { + await assertNoDiagnostics(r''' +class A { + Object? operator +(int other) => null; +} +class B extends A { + @override + Null operator +(int other) => null; +} +'''); + } + test_topLevelFunction_parameterType_null() async { await assertDiagnosticsFromMarkup(r''' void f([!Null!] x) {}
diff --git a/pkg/linter/test/rules/specify_nonobvious_local_variable_types_test.dart b/pkg/linter/test/rules/specify_nonobvious_local_variable_types_test.dart index e765cf4..821af3c 100644 --- a/pkg/linter/test/rules/specify_nonobvious_local_variable_types_test.dart +++ b/pkg/linter/test/rules/specify_nonobvious_local_variable_types_test.dart
@@ -308,6 +308,228 @@ '''); } + test_nonObviousType_constructor_rawGeneric() async { + await assertDiagnosticsFromMarkup(r''' +class A<T> {} +void f() { + [!var x = A()!]; + x; +} +'''); + } + + test_nonObviousType_hashCode_importPrefix() async { + // Known false negative: any `x.hashCode` is treated as obvious by name, + // so a top-level getter `hashCode` read through an import prefix is not + // reported. + // See https://github.com/dart-lang/sdk/issues/65178. + newFile('$testPackageLibPath/a.dart', r''' +int get hashCode => 1; +'''); + await assertNoDiagnostics(r''' +import 'a.dart' as a; + +void f() { + var x = a.hashCode; + x; +} +'''); + } + + test_nonObviousType_negativeVariable() async { + await assertDiagnosticsFromMarkup(r''' +void f(int a) { + [!var x = -a!]; + x; +} +'''); + } + + test_nonObviousType_recordNamedField() async { + await assertDiagnosticsFromMarkup(r''' +void f() { + [!var x = (a: 1)!]; + x; +} +'''); + } + + test_nonObviousType_toString_importPrefix() async { + // Known false negative: any `x.toString()` is treated as obvious by name, + // so a top-level function `toString` invoked through an import prefix is + // not reported. + // See https://github.com/dart-lang/sdk/issues/65178. + newFile('$testPackageLibPath/a.dart', r''' +String toString() => ''; +'''); + await assertNoDiagnostics(r''' +import 'a.dart' as a; + +void f() { + var x = a.toString(); + x; +} +'''); + } + + test_obviousType_conditional() async { + await assertNoDiagnostics(r''' +void f(bool b) { + var x = b ? 1 : 2; + x; +} +'''); + } + + test_obviousType_constructor_nonGeneric() async { + await assertNoDiagnostics(r''' +class A {} +void f() { + var x = A(); + x; +} +'''); + } + + test_obviousType_constructor_typeArguments() async { + await assertNoDiagnostics(r''' +class A<T> {} +void f() { + var x = A<int>(); + x; +} +'''); + } + + test_obviousType_equality() async { + await assertNoDiagnostics(r''' +void f(int a) { + var x = a == 1; + x; +} +'''); + } + + test_obviousType_hashCode_receiver() async { + await assertNoDiagnostics(r''' +void f(int a) { + var x = a.hashCode; + x; +} +'''); + } + + test_obviousType_inequality() async { + await assertNoDiagnostics(r''' +void f(int a) { + var x = a != 1; + x; +} +'''); + } + + test_obviousType_list_ifElement() async { + await assertNoDiagnostics(r''' +void f(bool b) { + var x = [if (b) 1]; + x; +} +'''); + } + + test_obviousType_list_spread() async { + await assertNoDiagnostics(r''' +void f() { + var x = [...[1]]; + x; +} +'''); + } + + test_obviousType_map() async { + await assertNoDiagnostics(r''' +void f() { + var x = {1: 2}; + x; +} +'''); + } + + test_obviousType_negativeDouble() async { + await assertNoDiagnostics(r''' +void f() { + var x = -1.0; + x; +} +'''); + } + + test_obviousType_negativeInt() async { + await assertNoDiagnostics(r''' +void f() { + var x = -1; + x; +} +'''); + } + + test_obviousType_parameter() async { + await assertNoDiagnostics(r''' +void f(int a) { + var x = a; + x; +} +'''); + } + + test_obviousType_toString_receiver() async { + await assertNoDiagnostics(r''' +void f(int a) { + var x = a.toString(); + x; +} +'''); + } + + test_obviousType_toString_unqualified() async { + await assertNoDiagnostics(r''' +class A { + void m() { + var x = toString(); + x; + } +} +'''); + } + + test_pattern_caseInIfStatementInSwitchBody() async { + // Reported only because the switch statement case visitor descends into + // the case body; see `test_pattern_ifCase` for the same `if-case` outside + // of a switch. + await assertDiagnosticsFromMarkup(r''' +void f(Object o) { + switch (o) { + case int(): + if (o case [!var a!]) { + a; + } + } +} +'''); + } + + test_pattern_ifCase() async { + // Known false negative: `if-case` statements are not visited. + // See https://github.com/dart-lang/sdk/issues/65178. + await assertNoDiagnostics(r''' +void f(Object o) { + if (o case var a) { + a; + } +} +'''); + } + test_pattern_list_destructured() async { await assertNoDiagnostics(r''' f() { @@ -346,6 +568,34 @@ '''); } + test_pattern_recordDeclaration() async { + await assertDiagnosticsFromMarkup(r''' +void f((int, int) r) { + var ([!a!], int b) = r; + a + b; +} +'''); + } + + test_pattern_switchExpression() async { + await assertDiagnosticsFromMarkup(r''' +int f(Object o) => switch (o) { + [!var a!] => a.hashCode, +}; +'''); + } + + test_pattern_switchStatement() async { + await assertDiagnosticsFromMarkup(r''' +void f(Object o) { + switch (o) { + case [!var a!]: + a; + } +} +'''); + } + test_switch_pattern_object() async { await assertNoDiagnostics(r''' class A {
diff --git a/pkg/linter/test/rules/specify_nonobvious_property_types_test.dart b/pkg/linter/test/rules/specify_nonobvious_property_types_test.dart index b1a5ae0..ac9dd11 100644 --- a/pkg/linter/test/rules/specify_nonobvious_property_types_test.dart +++ b/pkg/linter/test/rules/specify_nonobvious_property_types_test.dart
@@ -376,6 +376,31 @@ '''); } + test_late_instance_range() async { + await assertDiagnosticsFromMarkup(r''' +class A { + [!late var a = f()!]; +} +int f() => 1; +'''); + } + + test_late_static_range() async { + await assertDiagnosticsFromMarkup(r''' +class A { + static [!late final a = f()!]; +} +int f() => 1; +'''); + } + + test_late_topLevel_range() async { + await assertDiagnosticsFromMarkup(r''' +[!late var a = f()!]; +int f() => 1; +'''); + } + test_literal_bool_instance() async { await assertNoDiagnostics(r''' class A {
diff --git a/pkg/linter/test/rules/unnecessary_nullable_for_final_variable_declarations_test.dart b/pkg/linter/test/rules/unnecessary_nullable_for_final_variable_declarations_test.dart index a37964b..d6397b9 100644 --- a/pkg/linter/test/rules/unnecessary_nullable_for_final_variable_declarations_test.dart +++ b/pkg/linter/test/rules/unnecessary_nullable_for_final_variable_declarations_test.dart
@@ -18,6 +18,16 @@ String get lintRule => LintNames.unnecessary_nullable_for_final_variable_declarations; + test_forLoopVariable_nullableType() async { + // Known false negative: `for` loop variable declarations are not visited. + // See https://github.com/dart-lang/sdk/issues/65178. + await assertNoDiagnostics(r''' +void f() { + for (final int? a = 1; a != 2;) {} +} +'''); + } + test_list() async { await assertDiagnosticsFromMarkup(r''' f() {
diff --git a/pkg/linter/test/rules/unnecessary_underscores_test.dart b/pkg/linter/test/rules/unnecessary_underscores_test.dart index 3e660a7..1577c04 100644 --- a/pkg/linter/test/rules/unnecessary_underscores_test.dart +++ b/pkg/linter/test/rules/unnecessary_underscores_test.dart
@@ -17,6 +17,22 @@ @override String get lintRule => 'unnecessary_underscores'; + test_closure_parameter_unused() async { + await assertDiagnosticsFromMarkup(r''' +var f = (int [!__!]) {}; +'''); + } + + test_closure_parameter_used() async { + // Known false positive: closure bodies are not searched for references. + // See https://github.com/dart-lang/sdk/issues/65178. + await assertDiagnosticsFromMarkup(r''' +var f = (int [!__!]) { + print(__); +}; +'''); + } + test_commentReferenceFunction() async { await assertNoDiagnostics(''' /// This is a reference to [__]. @@ -42,6 +58,24 @@ '''); } + test_constructor_parameter_unused() async { + await assertDiagnosticsFromMarkup(r''' +class C { + C(int [!__!]); +} +'''); + } + + test_constructor_parameter_used() async { + await assertNoDiagnostics(r''' +class C { + C(int __) { + print(__); + } +} +'''); + } + test_enum_field_unused() async { await assertNoDiagnostics(r''' enum E { @@ -65,6 +99,15 @@ '''); } + test_forIn_target_used() async { + await assertNoDiagnostics(r''' +void f() { + int __ = 0; + for (__ in [1]) {} +} +'''); + } + test_forPart_unused() async { await assertDiagnosticsFromMarkup(r''' void f() { @@ -103,6 +146,21 @@ '''); } + test_functionType_parameter() async { + await assertDiagnosticsFromMarkup(r''' +typedef F = void Function(int [!__!]); +'''); + } + + test_local_assigned() async { + await assertNoDiagnostics(r''' +void f() { + int __ = 0; + __ = 1; +} +'''); + } + test_local_unused() async { await assertDiagnosticsFromMarkup(r''' void f() { @@ -146,6 +204,16 @@ '''); } + test_method_parameter_used() async { + await assertNoDiagnostics(r''' +class C { + void m(int __) { + print(__); + } +} +'''); + } + test_method_unused() async { await assertNoDiagnostics(r''' class A { @@ -154,6 +222,22 @@ '''); } + test_operator_parameter_unused() async { + await assertDiagnosticsFromMarkup(r''' +class C { + int operator +(int [!__!]) => 0; +} +'''); + } + + test_operator_parameter_used() async { + await assertNoDiagnostics(r''' +class C { + int operator +(int __) => __; +} +'''); + } + test_parameter_declaring_unused() async { await assertNoDiagnostics(r''' // ignore: unused_field_from_primary_constructor @@ -161,12 +245,54 @@ '''); } + test_primaryConstructor_parameter_used() async { + await assertNoDiagnostics(r''' +class C(int __) { + this { + print(__); + } +} +'''); + } + + test_setter_parameter_unused() async { + await assertDiagnosticsFromMarkup(r''' +class C { + set s(int [!__!]) {} +} +'''); + } + + test_setter_parameter_used() async { + await assertNoDiagnostics(r''' +class C { + set s(int __) { + print(__); + } +} +'''); + } + test_topLevelFunction_unused() async { await assertNoDiagnostics(r''' __() {} '''); } + test_topLevelSetter_parameter_unused() async { + await assertDiagnosticsFromMarkup(r''' +set s(int [!__!]) {} +'''); + } + + test_topLevelSetter_parameter_used() async { + await assertNoDiagnostics(r''' +set s(int __) { + print(__); +} +'''); + } + test_topLevelVariable_unused() async { await assertNoDiagnostics(r''' int __ = 0;
diff --git a/pkg/linter/test/rules/unsafe_variance_test.dart b/pkg/linter/test/rules/unsafe_variance_test.dart index 8cadbf0..f9ff9c1 100644 --- a/pkg/linter/test/rules/unsafe_variance_test.dart +++ b/pkg/linter/test/rules/unsafe_variance_test.dart
@@ -25,6 +25,20 @@ '''); } + test_class_localVariable() async { + // Known false positive: a local variable is not an instance member, but + // every variable declaration list is checked. + // See https://github.com/dart-lang/sdk/issues/65178. + await assertDiagnosticsFromMarkup(r''' +class A<T> { + void m(void Function(T) p) { + void Function([!T!]) v = p; + v; + } +} +'''); + } + test_class_method_bound() async { await assertDiagnosticsFromMarkup(r''' class A<X> { @@ -59,6 +73,14 @@ '''); } + test_class_operator_return() async { + await assertDiagnosticsFromMarkup(r''' +class A<T> { + void Function([!T!]) operator +(int other) => throw 0; +} +'''); + } + test_class_variable() async { await assertDiagnosticsFromMarkup(r''' class A<X> {
diff --git a/pkg/linter/test/rules/use_late_for_private_fields_and_variables_test.dart b/pkg/linter/test/rules/use_late_for_private_fields_and_variables_test.dart index 951f116..31d35eb 100644 --- a/pkg/linter/test/rules/use_late_for_private_fields_and_variables_test.dart +++ b/pkg/linter/test/rules/use_late_for_private_fields_and_variables_test.dart
@@ -222,6 +222,15 @@ '''); } + test_staticField_private_onClass_readByDotShorthand() async { + await assertNoDiagnostics(r''' +class C { + static C? _c; +} +C? f() => ._c; +'''); + } + test_staticField_private_onExtension() async { await assertDiagnosticsFromMarkup(''' extension E on int { @@ -263,6 +272,15 @@ '''); } + test_topLevel_assignedInForEach() async { + await assertNoDiagnostics(''' +int? _i; +void f(List<int?> l) { + for (_i in l) {} +} +'''); + } + test_topLevel_declaredInPart() async { newFile('$testPackageRootPath/lib/lib.dart', ''' part 'test.dart'; @@ -347,6 +365,40 @@ '''); } + test_topLevel_postfixIncrement_nullableOperator() async { + // Known false positive: the variable is written by the user-defined + // operator, which can return `null`, but this write is not counted as + // nullable. + // See https://github.com/dart-lang/sdk/issues/65178. + await assertDiagnosticsFromMarkup(''' +extension on int? { + int? operator +(int other) => null; + int? operator -(int other) => null; +} +int? [!_i!]; +void f() { + _i++; +} +'''); + } + + test_topLevel_prefixDecrement_nullableOperator() async { + // Known false positive: the variable is written by the user-defined + // operator, which can return `null`, but this write is not counted as + // nullable. + // See https://github.com/dart-lang/sdk/issues/65178. + await assertDiagnosticsFromMarkup(''' +extension on int? { + int? operator +(int other) => null; + int? operator -(int other) => null; +} +int? [!_i!]; +void f() { + --_i; +} +'''); + } + test_topLevel_public() async { await assertNoDiagnostics(''' int? i;