Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

Commit ecbe34b

Browse files
committed
Report unrelated argument types on Enums
1 parent 1a3af7b commit ecbe34b

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

lib/src/util/unrelated_types_visitor.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ abstract class UnrelatedTypesProcessors extends SimpleAstVisitor<void> {
103103
targetType = parent.declaredElement2?.thisType;
104104
} else if (parent is MixinDeclaration) {
105105
targetType = parent.declaredElement2?.thisType;
106+
} else if (parent is EnumDeclaration) {
107+
targetType = parent.declaredElement2?.thisType;
106108
}
107109
}
108110
}

test_data/rules/iterable_contains_unrelated_type.dart

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void someFunction6() {
4747
if (list.contains(instance)) print('someFunction6'); // OK
4848
}
4949

50-
class MixedIn with Mixin { }
50+
class MixedIn with Mixin {}
5151

5252
void someFunction6_1() {
5353
List<DerivedClass2> list = <DerivedClass2>[];
@@ -178,3 +178,17 @@ abstract class MyIterableMixedClass extends Object
178178
bool myConcreteBadMethod(String thing) => this.contains(thing); // LINT
179179
bool myConcreteBadMethod1(String thing) => contains(thing); // LINT
180180
}
181+
182+
enum E implements List<int> {
183+
one,
184+
two;
185+
186+
@override
187+
dynamic noSuchMethod(_) => throw UnsupportedError('');
188+
189+
void f() {
190+
contains('string'); // LINT
191+
this.contains('string'); // LINT
192+
contains(1); // OK
193+
}
194+
}

test_data/rules/list_remove_unrelated_type.dart

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void someFunction4() {
2626

2727
void someFunction4_1() {
2828
List list = [];
29-
if(list.remove(null)) print('someFunction4_1');
29+
if (list.remove(null)) print('someFunction4_1');
3030
}
3131

3232
void someFunction5_1() {
@@ -47,7 +47,7 @@ void someFunction6() {
4747
if (list.remove(instance)) print('someFunction6'); // OK
4848
}
4949

50-
class MixedIn with Mixin { }
50+
class MixedIn with Mixin {}
5151

5252
void someFunction6_1() {
5353
List<DerivedClass2> list = <DerivedClass2>[];
@@ -99,7 +99,8 @@ void someFunction12() {
9999
}
100100

101101
void bug_267(list) {
102-
if (list.remove('1')) print('someFunction'); // https://github.com/dart-lang/linter/issues/267
102+
if (list.remove('1'))
103+
print('someFunction'); // https://github.com/dart-lang/linter/issues/267
103104
}
104105

105106
abstract class ClassBase {}
@@ -135,19 +136,27 @@ bool takesList2(List<String> list) => list.remove('a'); // OK
135136
bool takesList3(List list) => list.remove('a'); // OK
136137

137138
abstract class A implements List<int> {}
139+
138140
abstract class B extends A {}
141+
139142
bool takesB(B b) => b.remove('a'); // LINT
140143

141144
abstract class A1 implements List<String> {}
145+
142146
abstract class B1 extends A1 {}
147+
143148
bool takesB1(B1 b) => b.remove('a'); // OK
144149

145150
abstract class A3 implements List {}
151+
146152
abstract class B3 extends A3 {}
153+
147154
bool takesB3(B3 b) => b.remove('a'); // OK
148155

149156
abstract class A2 implements List<String> {}
157+
150158
abstract class B2 extends A2 {}
159+
151160
bool takesB2(B2 b) => b.remove('a'); // OK
152161

153162
abstract class SomeList<E> implements List<E> {}
@@ -173,3 +182,17 @@ abstract class MyListMixedClass extends Object
173182
bool myConcreteBadMethod(String thing) => this.remove(thing); // LINT
174183
bool myConcreteBadMethod1(String thing) => remove(thing); // LINT
175184
}
185+
186+
enum E implements List<int> {
187+
one,
188+
two;
189+
190+
@override
191+
dynamic noSuchMethod(_) => throw UnsupportedError('');
192+
193+
void f() {
194+
remove('string'); // LINT
195+
this.remove('string'); // LINT
196+
remove(1); // OK
197+
}
198+
}

0 commit comments

Comments
 (0)