Skip to content

Commit

Permalink
Move unnecessary_getters_setters tests (dart-lang/linter#4549)
Browse files Browse the repository at this point in the history
  • Loading branch information
srawlins authored Jul 13, 2023
1 parent 246eabd commit 811fdc7
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 95 deletions.
10 changes: 6 additions & 4 deletions lib/src/ast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -251,19 +251,21 @@ bool isSimpleGetter(MethodDeclaration declaration) {
/// A simple setter takes this basic form:
///
/// ```dart
/// var _x;
/// set(x) {
/// int _x;
/// set(int x) {
/// _x = x;
/// }
/// ```
///
/// 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) {
Expand Down
2 changes: 2 additions & 0 deletions test/rules/all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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();
Expand Down
126 changes: 126 additions & 0 deletions test/rules/unnecessary_getters_setters_test.dart
Original file line number Diff line number Diff line change
@@ -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),
]);
}
}
91 changes: 0 additions & 91 deletions test_data/rules/unnecessary_getters_setters.dart

This file was deleted.

0 comments on commit 811fdc7

Please sign in to comment.