From 811fdc7e99d1ec2bf8749f5d85e54361f8f8dd03 Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Thu, 13 Jul 2023 08:51:28 -0700 Subject: [PATCH] Move unnecessary_getters_setters tests (dart-lang/linter#4549) --- lib/src/ast.dart | 10 +- test/rules/all.dart | 2 + .../unnecessary_getters_setters_test.dart | 126 ++++++++++++++++++ .../rules/unnecessary_getters_setters.dart | 91 ------------- 4 files changed, 134 insertions(+), 95 deletions(-) create mode 100644 test/rules/unnecessary_getters_setters_test.dart delete mode 100644 test_data/rules/unnecessary_getters_setters.dart diff --git a/lib/src/ast.dart b/lib/src/ast.dart index a1cc3ba958c4..dfd0ea307e3a 100644 --- a/lib/src/ast.dart +++ b/lib/src/ast.dart @@ -251,8 +251,8 @@ bool isSimpleGetter(MethodDeclaration declaration) { /// A simple setter takes this basic form: /// /// ```dart -/// var _x; -/// set(x) { +/// int _x; +/// set(int x) { /// _x = x; /// } /// ``` @@ -260,10 +260,12 @@ bool isSimpleGetter(MethodDeclaration declaration) { /// or: /// /// ```dart -/// set(x) => _x = x; +/// int _x; +/// set(int x) => _x = x; /// ``` /// -/// where the static type of the left and right hand sides must be the same. +/// where the static type of the left and right hand sides of the assignment +/// expression are the same. bool isSimpleSetter(MethodDeclaration setter) { var body = setter.body; if (body is ExpressionFunctionBody) { diff --git a/test/rules/all.dart b/test/rules/all.dart index a0c50b0aa236..e6f5b8380681 100644 --- a/test/rules/all.dart +++ b/test/rules/all.dart @@ -138,6 +138,7 @@ import 'unnecessary_breaks_test.dart' as unnecessary_breaks; import 'unnecessary_const_test.dart' as unnecessary_const; import 'unnecessary_constructor_name_test.dart' as unnecessary_constructor_name; import 'unnecessary_final_test.dart' as unnecessary_final; +import 'unnecessary_getters_setters_test.dart' as unnecessary_getters_setters; import 'unnecessary_lambdas_test.dart' as unnecessary_lambdas; import 'unnecessary_late_test.dart' as unnecessary_late; import 'unnecessary_library_directive_test.dart' @@ -283,6 +284,7 @@ void main() { unnecessary_const.main(); unnecessary_constructor_name.main(); unnecessary_final.main(); + unnecessary_getters_setters.main(); unnecessary_lambdas.main(); unnecessary_late.main(); unnecessary_library_directive.main(); diff --git a/test/rules/unnecessary_getters_setters_test.dart b/test/rules/unnecessary_getters_setters_test.dart new file mode 100644 index 000000000000..deaee5aaae6f --- /dev/null +++ b/test/rules/unnecessary_getters_setters_test.dart @@ -0,0 +1,126 @@ +// 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(UnnecessaryGettersSettersTest); + }); +} + +@reflectiveTest +class UnnecessaryGettersSettersTest extends LintRuleTest { + @override + String get lintRule => 'unnecessary_getters_setters'; + + test_necessary_differentType() async { + await assertNoDiagnostics(r''' +class C { + String? _x; + + dynamic get x { + return _x; + } + set x(dynamic value) { + _x = value; + } +} +'''); + } + + test_necessary_hasAnnotation() async { + await assertNoDiagnostics(r''' +class C { + String? _x; + + @Annotation() + String? get x => _x; + set x(String? value) { + _x = value; + } +} + +class Annotation { + const Annotation(); +} +'''); + } + + test_necessary_nonTrivialSetter() async { + await assertNoDiagnostics(r''' +class C { + String? _x; + + String? get x => _x; + set x(String? value) { + _x = value?.toLowerCase(); + } +} +'''); + } + + test_necessary_nullAwareAssignment() async { + await assertNoDiagnostics(r''' +class C { + String? _x; + + String? get x => _x; + set x(String? value) { + _x ??= value; + } +} +'''); + } + + test_unnecessary_getterAndSetterHaveBlockBody() async { + await assertDiagnostics(r''' +class C { + String? _x; + + String? get x { + return _x; + } + set x(String? value) { + _x = value; + } +} +''', [ + lint(39, 1), + ]); + } + + test_unnecessary_getterHasExpressionBody() async { + await assertDiagnostics(r''' +class C { + String? _x; + + String? get x => _x; + set x(String? value) + { + _x = value; + } +} +''', [ + lint(39, 1), + ]); + } + + test_unnecessary_setterHasExpressionBody() async { + await assertDiagnostics(r''' +class C { + String? _x; + + String? get x { + return _x; + } + set x(String? value) => _x = value; +} +''', [ + lint(39, 1), + ]); + } +} diff --git a/test_data/rules/unnecessary_getters_setters.dart b/test_data/rules/unnecessary_getters_setters.dart deleted file mode 100644 index 6484a7ec5c43..000000000000 --- a/test_data/rules/unnecessary_getters_setters.dart +++ /dev/null @@ -1,91 +0,0 @@ -// Copyright (c) 2015, 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 unnecessary_getters_setters` - -import 'package:meta/meta.dart'; - -class Box { - var _contents; - get contents => _contents; //LINT [7:8] - set contents(value) - { - _contents = value; - } -} - -class Box2 { - var _contents; - get contents //LINT - { - return _contents; - } - set contents(value) - { - _contents = value; - } -} - -class Box3 { - var _contents; - get contents //LINT - { - return _contents; - } - set contents(value) => _contents = value; -} - -class Box4 { - var _contents; - int get contents { - return _contents; - } - set contents(int value) // OK -- notice the type - { - _contents = value; - } -} - -class Box5 { - var _contents; - @protected - get contents => _contents; //OK (protected) - set contents(value) { - _contents = value; - } -} - -class Box6 { - var _contents; - @Deprecated('blah') - get contents => _contents; //OK (deprecated) - set contents(value) { - _contents = value; - } -} - -class Box7 { - var _contents; - @visibleForTesting - get contents => _contents; //OK (annotation) - set contents(value) { - _contents = value; - } -} - -class Box8 { - var _contents; - get contents => _contents; //OK (setter uses `??=`) - set contents(value) { - _contents ??= value; - } -} - -class LowerCase { - var _contents; - get contents => _contents; - set contents(value) { - _contents = value.toLowerCase(); - } -}