diff --git a/test/rules/all.dart b/test/rules/all.dart index 939563cfa..b4e93437c 100644 --- a/test/rules/all.dart +++ b/test/rules/all.dart @@ -115,6 +115,8 @@ import 'prefer_inlined_adds_test.dart' as prefer_inlined_adds; import 'prefer_interpolation_to_compose_strings_test.dart' as prefer_interpolation_to_compose_strings; import 'prefer_mixin_test.dart' as prefer_mixin; +import 'prefer_null_aware_method_calls_test.dart' + as prefer_null_aware_method_calls; 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; @@ -279,6 +281,7 @@ void main() { prefer_inlined_adds.main(); prefer_interpolation_to_compose_strings.main(); prefer_mixin.main(); + prefer_null_aware_method_calls.main(); prefer_relative_imports.main(); prefer_single_quotes.main(); prefer_spread_collections.main(); diff --git a/test/rules/prefer_null_aware_method_calls_test.dart b/test/rules/prefer_null_aware_method_calls_test.dart new file mode 100644 index 000000000..439001d42 --- /dev/null +++ b/test/rules/prefer_null_aware_method_calls_test.dart @@ -0,0 +1,103 @@ +// 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(PreferNullAwareMethodCallsTest); + }); +} + +@reflectiveTest +class PreferNullAwareMethodCallsTest extends LintRuleTest { + @override + String get lintRule => 'prefer_null_aware_method_calls'; + + test_conditional_differentTargets() async { + await assertNoDiagnostics(r''' +Function()? f1, f2; +void f() { + f1 != null ? f2!() : null; +} +'''); + } + + test_conditional_propertyAccess_differentProperties() async { + await assertNoDiagnostics(r''' +void f(dynamic p) { + p.a != null ? p.m!() : null; +} +'''); + } + + test_conditional_propertyAccess_sameProperty() async { + await assertDiagnostics(r''' +void f(dynamic p) { + p.m != null ? p.m!() : null; +} +''', [ + lint(36, 6), + ]); + } + + test_conditional_sameTarget() async { + await assertDiagnostics(r''' +Function()? func; +void f() { + func != null ? func!() : null; +} +''', [ + lint(46, 7), + ]); + } + + test_ifNotNull_propertyAccess_differentProperties() async { + await assertNoDiagnostics(r''' +void f(dynamic p) { + if (p.a != null) { + p.m!(); + } +} +'''); + } + + test_ifNotNull_propertyAccess_sameProperty() async { + await assertDiagnostics(r''' +void f(dynamic p) { + if (p.m != null) { + p.m!(); + } +} +''', [ + lint(45, 6), + ]); + } + + test_ifNotNull_sameTarget_blockStatement() async { + await assertDiagnostics(r''' +Function()? func; +void f() { + if (func != null) { + func!(); + } +} +''', [ + lint(55, 7), + ]); + } + + test_ifNotNull_sameTarget_expressionStatement() async { + await assertDiagnostics(r''' +Function()? func; +void f() { + if (func != null) func!(); +} +''', [ + lint(49, 7), + ]); + } +} diff --git a/test_data/rules/prefer_null_aware_method_calls.dart b/test_data/rules/prefer_null_aware_method_calls.dart deleted file mode 100644 index 203794a04..000000000 --- a/test_data/rules/prefer_null_aware_method_calls.dart +++ /dev/null @@ -1,31 +0,0 @@ -// 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. - -// test w/ `dart test -N prefer_null_aware_method_calls` - -var o; -var f, g; - -m() { - if (o != null) o!(); // LINT - - if (o != null) { - o!(); // LINT - } - - if (o.m != null) { - o.m!(); // LINT - } - - if (o.a != null) { - o.m!(); // OK - } - - o != null ? o!() : null; // LINT - o.m != null ? o.m!() : null; // LINT - o.a != null ? o.m!() : null; // OK - - if (f != null) g!(); // OK - f != null ? g!() : null; // OK -}