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

Report unrelated argument types on Enums #3695

Merged
merged 1 commit into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/src/util/unrelated_types_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ abstract class UnrelatedTypesProcessors extends SimpleAstVisitor<void> {
targetType = parent.declaredElement2?.thisType;
} else if (parent is MixinDeclaration) {
targetType = parent.declaredElement2?.thisType;
} else if (parent is EnumDeclaration) {
targetType = parent.declaredElement2?.thisType;
}
}
}
Expand Down
16 changes: 15 additions & 1 deletion test_data/rules/iterable_contains_unrelated_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void someFunction6() {
if (list.contains(instance)) print('someFunction6'); // OK
}

class MixedIn with Mixin { }
class MixedIn with Mixin {}

void someFunction6_1() {
List<DerivedClass2> list = <DerivedClass2>[];
Expand Down Expand Up @@ -178,3 +178,17 @@ abstract class MyIterableMixedClass extends Object
bool myConcreteBadMethod(String thing) => this.contains(thing); // LINT
bool myConcreteBadMethod1(String thing) => contains(thing); // LINT
}

enum E implements List<int> {
one,
two;

@override
dynamic noSuchMethod(_) => throw UnsupportedError('');

void f() {
contains('string'); // LINT
this.contains('string'); // LINT
contains(1); // OK
}
}
29 changes: 26 additions & 3 deletions test_data/rules/list_remove_unrelated_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void someFunction4() {

void someFunction4_1() {
List list = [];
if(list.remove(null)) print('someFunction4_1');
if (list.remove(null)) print('someFunction4_1');
}

void someFunction5_1() {
Expand All @@ -47,7 +47,7 @@ void someFunction6() {
if (list.remove(instance)) print('someFunction6'); // OK
}

class MixedIn with Mixin { }
class MixedIn with Mixin {}

void someFunction6_1() {
List<DerivedClass2> list = <DerivedClass2>[];
Expand Down Expand Up @@ -99,7 +99,8 @@ void someFunction12() {
}

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

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

abstract class A implements List<int> {}

abstract class B extends A {}

bool takesB(B b) => b.remove('a'); // LINT

abstract class A1 implements List<String> {}

abstract class B1 extends A1 {}

bool takesB1(B1 b) => b.remove('a'); // OK

abstract class A3 implements List {}

abstract class B3 extends A3 {}

bool takesB3(B3 b) => b.remove('a'); // OK

abstract class A2 implements List<String> {}

abstract class B2 extends A2 {}

bool takesB2(B2 b) => b.remove('a'); // OK

abstract class SomeList<E> implements List<E> {}
Expand All @@ -173,3 +182,17 @@ abstract class MyListMixedClass extends Object
bool myConcreteBadMethod(String thing) => this.remove(thing); // LINT
bool myConcreteBadMethod1(String thing) => remove(thing); // LINT
}

enum E implements List<int> {
one,
two;

@override
dynamic noSuchMethod(_) => throw UnsupportedError('');

void f() {
remove('string'); // LINT
this.remove('string'); // LINT
remove(1); // OK
}
}