Skip to content

Commit 24eafc3

Browse files
authored
enhanced enum support for avoid_returning_this (dart-archive/linter#3189)
* enhanced enum support for `avoid_returning_this` * fmt
1 parent 6c8b8fc commit 24eafc3

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

lib/src/rules/avoid_returning_this.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,15 @@ class _Visitor extends SimpleAstVisitor<void> {
7777
if (node.isOperator) return;
7878

7979
var parent = node.parent;
80-
if (parent is ClassOrMixinDeclaration) {
80+
if (parent is ClassOrMixinDeclaration || parent is EnumDeclaration) {
8181
if (DartTypeUtilities.overridesMethod(node)) {
8282
return;
8383
}
8484

8585
var returnType = node.declaredElement?.returnType;
8686
if (returnType is InterfaceType &&
87-
returnType.element == parent.declaredElement) {
87+
// ignore: cast_nullable_to_non_nullable
88+
returnType.element == (parent as Declaration).declaredElement) {
8889
} else {
8990
return;
9091
}

test/rules/all.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'avoid_function_literals_in_foreach_calls.dart'
99
import 'avoid_init_to_null.dart' as avoid_init_to_null;
1010
import 'avoid_redundant_argument_values.dart'
1111
as avoid_redundant_argument_values;
12+
import 'avoid_returning_this.dart' as avoid_returning_this;
1213
import 'avoid_shadowing_type_parameters.dart'
1314
as avoid_shadowing_type_parameters;
1415
import 'avoid_types_as_parameter_names.dart' as avoid_types_as_parameter_names;
@@ -55,6 +56,7 @@ void main() {
5556
avoid_function_literals_in_foreach_calls.main();
5657
avoid_init_to_null.main();
5758
avoid_redundant_argument_values.main();
59+
avoid_returning_this.main();
5860
avoid_shadowing_type_parameters.main();
5961
avoid_types_as_parameter_names.main();
6062
avoid_unused_constructor_parameters.main();

test/rules/avoid_returning_this.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:test_reflective_loader/test_reflective_loader.dart';
6+
7+
import '../rule_test_support.dart';
8+
9+
main() {
10+
defineReflectiveSuite(() {
11+
defineReflectiveTests(AvoidReturningThisTest);
12+
});
13+
}
14+
15+
@reflectiveTest
16+
class AvoidReturningThisTest extends LintRuleTest {
17+
@override
18+
List<String> get experiments => [
19+
EnableString.enhanced_enums,
20+
];
21+
22+
@override
23+
String get lintRule => 'avoid_returning_this';
24+
25+
test_method() async {
26+
await assertDiagnostics(r'''
27+
enum A {
28+
a,b,c;
29+
A a() => this;
30+
}
31+
''', [
32+
lint('avoid_returning_this', 22, 1),
33+
]);
34+
}
35+
}

0 commit comments

Comments
 (0)