From c5d2b1adbc5c71788f4ceb21310314e3ecf5c809 Mon Sep 17 00:00:00 2001 From: pq Date: Mon, 4 Oct 2021 11:28:21 -0700 Subject: [PATCH 1/2] test diagnostic cleanup --- test/rules/all.dart | 5 ++++ .../prefer_generic_function_type_aliases.dart | 30 +++++++++++++++++++ test/rules/unnecessary_null_checks.dart | 30 +++++++++++++++++++ ...uals_and_hash_code_on_mutable_classes.dart | 4 +-- .../avoid_redundant_argument_values.dart | 16 +++++----- .../rules/avoid_relative_lib_imports.dart | 2 +- .../nnbd/rules/overridden_fields.dart | 2 +- .../nnbd/rules/unnecessary_null_checks.dart | 4 --- test_data/rules/no_logic_in_create_state.dart | 4 +-- .../prefer_generic_function_type_aliases.dart | 4 --- 10 files changed, 79 insertions(+), 22 deletions(-) create mode 100644 test/rules/prefer_generic_function_type_aliases.dart create mode 100644 test/rules/unnecessary_null_checks.dart diff --git a/test/rules/all.dart b/test/rules/all.dart index 7914b6fba..c783d25c2 100644 --- a/test/rules/all.dart +++ b/test/rules/all.dart @@ -18,9 +18,12 @@ import 'prefer_const_constructors_in_immutables.dart' import 'prefer_const_literals_to_create_immutables.dart' as prefer_const_literals_to_create_immutables; import 'prefer_contains.dart' as prefer_contains; +import 'prefer_generic_function_type_aliases.dart' + as prefer_generic_function_type_aliases; import 'prefer_spread_collections.dart' as prefer_spread_collections; import 'type_init_formals.dart' as type_init_formals; import 'unawaited_futures.dart' as unawaited_futures; +import 'unnecessary_null_checks.dart' as unnecessary_null_checks; import 'void_checks.dart' as void_checks; void main() { @@ -35,8 +38,10 @@ void main() { prefer_const_constructors_in_immutables.main(); prefer_const_literals_to_create_immutables.main(); prefer_contains.main(); + prefer_generic_function_type_aliases.main(); prefer_spread_collections.main(); type_init_formals.main(); unawaited_futures.main(); + unnecessary_null_checks.main(); void_checks.main(); } diff --git a/test/rules/prefer_generic_function_type_aliases.dart b/test/rules/prefer_generic_function_type_aliases.dart new file mode 100644 index 000000000..faaffa6aa --- /dev/null +++ b/test/rules/prefer_generic_function_type_aliases.dart @@ -0,0 +1,30 @@ +// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:test_reflective_loader/test_reflective_loader.dart'; + +import '../rule_test_support.dart'; + +main() { + defineReflectiveSuite(() { + defineReflectiveTests(PreferGenericFunctionTypeAliasesTest); + }); +} + +@reflectiveTest +class PreferGenericFunctionTypeAliasesTest extends LintRuleTest { + @override + String get lintRule => 'prefer_generic_function_type_aliases'; + + /// https://github.com/dart-lang/linter/issues/2777 + test_undefinedFunction() async { + await assertDiagnostics(r''' +typedef Cb2 +''', [ + // No lint + error(ParserErrorCode.MISSING_TYPEDEF_PARAMETERS, 12, 0), + error(ParserErrorCode.EXPECTED_TOKEN, 12, 0), + ]); + } +} diff --git a/test/rules/unnecessary_null_checks.dart b/test/rules/unnecessary_null_checks.dart new file mode 100644 index 000000000..683b0fbbe --- /dev/null +++ b/test/rules/unnecessary_null_checks.dart @@ -0,0 +1,30 @@ +// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:test_reflective_loader/test_reflective_loader.dart'; + +import '../rule_test_support.dart'; + +main() { + defineReflectiveSuite(() { + defineReflectiveTests(UnnecessaryNullChecksTest); + }); +} + +@reflectiveTest +class UnnecessaryNullChecksTest extends LintRuleTest { + @override + String get lintRule => 'unnecessary_null_checks'; + + test_undefinedFunction() async { + await assertDiagnostics(r''' +f6(int? p) { + return B() + p!; // OK +} +''', [ + // No lint + error(CompileTimeErrorCode.UNDEFINED_FUNCTION, 22, 1), + ]); + } +} diff --git a/test_data/rules/avoid_equals_and_hash_code_on_mutable_classes.dart b/test_data/rules/avoid_equals_and_hash_code_on_mutable_classes.dart index c9dab2173..6d282ee7b 100644 --- a/test_data/rules/avoid_equals_and_hash_code_on_mutable_classes.dart +++ b/test_data/rules/avoid_equals_and_hash_code_on_mutable_classes.dart @@ -13,7 +13,7 @@ class A { @override operator ==(other) => other is A && other.key == key; // OK @override - int hashCode() => key.hashCode; // OK + int get hashCode => key.hashCode; // OK } class B { @@ -22,7 +22,7 @@ class B { @override operator ==(other) => other is B && other.key == key; // LINT @override - int hashCode() => key.hashCode; // LINT + int get hashCode => key.hashCode; // LINT } @immutable diff --git a/test_data/rules/avoid_redundant_argument_values.dart b/test_data/rules/avoid_redundant_argument_values.dart index e4555a8ce..39d41534f 100644 --- a/test_data/rules/avoid_redundant_argument_values.dart +++ b/test_data/rules/avoid_redundant_argument_values.dart @@ -7,10 +7,10 @@ import 'package:meta/meta.dart'; class A { - A({bool valWithDefault = true, bool val}); - void f({bool valWithDefault = true, bool val}) {} - void g({int valWithDefault = 1, bool val}) {} - void h({String valWithDefault = 'default', bool val}) {} + A({bool valWithDefault = true, bool? val}); + void f({bool valWithDefault = true, bool? val}) {} + void g({int valWithDefault = 1, bool? val}) {} + void h({String valWithDefault = 'default', bool? val}) {} } @@ -21,14 +21,14 @@ f(void g([bool b = false])) { bool q() => true; -void ff({bool valWithDefault = true, bool val}) {} -void g({@required bool valWithDefault = true, bool val}) {} +void ff({bool valWithDefault = true, bool? val}) {} +void g({@required bool valWithDefault = true, bool? val}) {} void gg(int x, [int y = 0]) {} void ggg([int a = 1, int b = 2]) {} -void gggg([int a = 0, int b]) {} +void gggg([int a = 0, int? b]) {} -void h([int a, int b = 1]) {} +void h([int? a, int? b = 1]) {} void main() { diff --git a/test_data/rules/avoid_relative_lib_imports.dart b/test_data/rules/avoid_relative_lib_imports.dart index dda3f4664..2665b972a 100644 --- a/test_data/rules/avoid_relative_lib_imports.dart +++ b/test_data/rules/avoid_relative_lib_imports.dart @@ -4,6 +4,6 @@ // test w/ `dart test -N avoid_relative_lib_imports` -import '../_data/avoid_relative_lib_imports/lib/dummy.dart'; //LINT +import '../integration/avoid_relative_lib_imports/lib/dummy.dart'; //LINT import 'avoid_catching_errors.dart'; //OK diff --git a/test_data/rules/experiments/nnbd/rules/overridden_fields.dart b/test_data/rules/experiments/nnbd/rules/overridden_fields.dart index 966c9ace7..a39d13dac 100644 --- a/test_data/rules/experiments/nnbd/rules/overridden_fields.dart +++ b/test_data/rules/experiments/nnbd/rules/overridden_fields.dart @@ -189,7 +189,7 @@ abstract class BBB { abstract final String s; } -class AAA extends BBB { +class AAAA extends BBB { @override String s = ''; // OK } diff --git a/test_data/rules/experiments/nnbd/rules/unnecessary_null_checks.dart b/test_data/rules/experiments/nnbd/rules/unnecessary_null_checks.dart index 44c1d40d0..c9d845d3c 100644 --- a/test_data/rules/experiments/nnbd/rules/unnecessary_null_checks.dart +++ b/test_data/rules/experiments/nnbd/rules/unnecessary_null_checks.dart @@ -60,7 +60,3 @@ f5(int? p) { v1 ??= p!; // OK } -f6(int? p) { - return B() + p!; // OK -} - diff --git a/test_data/rules/no_logic_in_create_state.dart b/test_data/rules/no_logic_in_create_state.dart index 23667988e..a0407338f 100644 --- a/test_data/rules/no_logic_in_create_state.dart +++ b/test_data/rules/no_logic_in_create_state.dart @@ -8,7 +8,7 @@ import 'package:flutter/widgets.dart'; class MyState extends State { - int field; + int field = 0; } class MyStatefulOK extends StatefulWidget { @@ -24,7 +24,7 @@ class MyStatefulOK2 extends StatefulWidget { } -MyState global; +MyState global = MyState(); class MyStatefulBad extends StatefulWidget { @override diff --git a/test_data/rules/prefer_generic_function_type_aliases.dart b/test_data/rules/prefer_generic_function_type_aliases.dart index a94946651..25536d897 100644 --- a/test_data/rules/prefer_generic_function_type_aliases.dart +++ b/test_data/rules/prefer_generic_function_type_aliases.dart @@ -4,9 +4,5 @@ // test w/ `dart test -N prefer_generic_function_type_aliases` - -//https://github.com/dart-lang/linter/issues/2777 -typedef Cb2 // OK - typedef void F1(); // LINT typedef F2 = void Function(); // OK From aaa60a7aab1b3455f8f74628e2e2b5f8cdc45340 Mon Sep 17 00:00:00 2001 From: pq Date: Mon, 4 Oct 2021 11:36:55 -0700 Subject: [PATCH 2/2] + super_goes_last --- test/rules/all.dart | 2 ++ test/rules/super_goes_last.dart | 39 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 test/rules/super_goes_last.dart diff --git a/test/rules/all.dart b/test/rules/all.dart index c783d25c2..03c006f1b 100644 --- a/test/rules/all.dart +++ b/test/rules/all.dart @@ -21,6 +21,7 @@ import 'prefer_contains.dart' as prefer_contains; import 'prefer_generic_function_type_aliases.dart' as prefer_generic_function_type_aliases; import 'prefer_spread_collections.dart' as prefer_spread_collections; +import 'super_goes_last.dart' as super_goes_last; import 'type_init_formals.dart' as type_init_formals; import 'unawaited_futures.dart' as unawaited_futures; import 'unnecessary_null_checks.dart' as unnecessary_null_checks; @@ -40,6 +41,7 @@ void main() { prefer_contains.main(); prefer_generic_function_type_aliases.main(); prefer_spread_collections.main(); + super_goes_last.main(); type_init_formals.main(); unawaited_futures.main(); unnecessary_null_checks.main(); diff --git a/test/rules/super_goes_last.dart b/test/rules/super_goes_last.dart new file mode 100644 index 000000000..b3650039f --- /dev/null +++ b/test/rules/super_goes_last.dart @@ -0,0 +1,39 @@ +// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:test_reflective_loader/test_reflective_loader.dart'; + +import '../rule_test_support.dart'; + +main() { + defineReflectiveSuite(() { + defineReflectiveTests(SuperGoesLastTest); + }); +} + +@reflectiveTest +class SuperGoesLastTest extends LintRuleTest { + @override + String get lintRule => 'super_goes_last'; + + test_invalidSuperInvocation() async { + await assertDiagnostics(r''' +class A { + int a; + A(this.a); +} + +class C extends A { + int _c; + C(int a) + : super(a), _c = a + 1; +} + +''', [ + error(HintCode.UNUSED_FIELD, 61, 2), + error(CompileTimeErrorCode.INVALID_SUPER_INVOCATION, 84, 5), + lint('super_goes_last', 84, 8), + ]); + } +}