diff --git a/test/rules/all.dart b/test/rules/all.dart index a0c50b0aa..513a06f72 100644 --- a/test/rules/all.dart +++ b/test/rules/all.dart @@ -114,6 +114,8 @@ import 'prefer_mixin_test.dart' as prefer_mixin; import 'prefer_relative_imports_test.dart' as prefer_relative_imports; import 'prefer_single_quotes_test.dart' as prefer_single_quotes; import 'prefer_spread_collections_test.dart' as prefer_spread_collections; +import 'prefer_typing_uninitialized_variables_test.dart' + as prefer_typing_uninitialized_variables; import 'prefer_void_to_null_test.dart' as prefer_void_to_null; import 'provide_deprecation_message_test.dart' as provide_deprecation_message; import 'public_member_api_docs_test.dart' as public_member_api_docs; @@ -263,6 +265,7 @@ void main() { prefer_relative_imports.main(); prefer_single_quotes.main(); prefer_spread_collections.main(); + prefer_typing_uninitialized_variables.main(); prefer_void_to_null.main(); provide_deprecation_message.main(); public_member_api_docs.main(); diff --git a/test/rules/prefer_typing_uninitialized_variables_test.dart b/test/rules/prefer_typing_uninitialized_variables_test.dart new file mode 100644 index 000000000..b8ac29a31 --- /dev/null +++ b/test/rules/prefer_typing_uninitialized_variables_test.dart @@ -0,0 +1,121 @@ +// Copyright (c) 2023, 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(PreferTypingUninitializedVariablesTest); + }); +} + +@reflectiveTest +class PreferTypingUninitializedVariablesTest extends LintRuleTest { + @override + String get lintRule => 'prefer_typing_uninitialized_variables'; + + test_field_final_noInitializer() async { + await assertDiagnostics(r''' +class C { + final x; + C(this.x); +} +''', [ + lint(18, 1), + ]); + } + + test_field_typed() async { + await assertNoDiagnostics(r''' +class C { + String? x; +} +'''); + } + + test_field_var_noInitializer() async { + await assertDiagnostics(r''' +class C { + var x; +} +''', [ + lint(16, 1), + ]); + } + + test_field_var_noInitializer_notFirst() async { + await assertDiagnostics(r''' +class C { + var a = 5, + b; +} +''', [ + lint(29, 1), + ]); + } + + test_field_var_noInitializer_static() async { + await assertDiagnostics(r''' +class C { + static var x; +} +''', [ + lint(23, 1), + ]); + } + + test_forEachLoopVariable_final() async { + await assertNoDiagnostics(r''' +void f() { + for (final e in []) {} +} +'''); + } + + test_forLoopVariable_var_noInitializer() async { + await assertDiagnostics(r''' +void f() { + for (var i, j = 0; j < 5; i = j, j++) {} +} +''', [ + lint(22, 1), + ]); + } + + test_localVariable_var_initializer() async { + await assertNoDiagnostics(r''' +void f() { + // ignore: unused_local_variable + var x = 1; +} +'''); + } + + test_localVariable_var_noInitializer() async { + await assertDiagnostics(r''' +void f() { + // ignore: unused_local_variable + var x; +} +''', [ + lint(52, 1), + ]); + } + + test_topLevelVariable_var_initializer() async { + await assertNoDiagnostics(r''' +var x = 4; +'''); + } + + test_topLevelVariable_var_noInitializer() async { + await assertDiagnostics(r''' +var x; +''', [ + lint(4, 1), + ]); + } +} diff --git a/test_data/rules/prefer_typing_uninitialized_variables.dart b/test_data/rules/prefer_typing_uninitialized_variables.dart deleted file mode 100644 index c86e26afa..000000000 --- a/test_data/rules/prefer_typing_uninitialized_variables.dart +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (c) 2017, 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. - -// test w/ `dart test -N prefer_typing_uninitialized_variables` - -class BadClass { - static var bar; // LINT - var foo; // LINT - final baz; // LINT - var a = 5, - b; // LINT - String? d; - - BadClass(this.baz); - - void method() { - var bar; // LINT - bar = 5; - print(bar); - } - - void someMethod() { - for (final v in []) { - var foo; // LINT - var bar = 8; - foo = v.length; - print('$foo$bar'); - } - - for (var i, // LINT - j = 0; j < 5; i = j, j++) { - print(i); - } - } -} - -void aFunction() { - var bar; // LINT - bar = 5; - print(bar); -} - -var topLevel; // LINT -var other = 4;