iterable_contains_unrelated_type
Invocation of Iterable
This rule is currently deprecated and available as of Dart 2.0.0.
Rule sets: core, recommended, flutter
Details
DON’T invoke contains
on Iterable
with an instance of different type
than the parameter type.
Doing this will invoke ==
on its elements and most likely will return false
.
BAD:
void someFunction() {
var list = <int>[];
if (list.contains('1')) print('someFunction'); // LINT
}
BAD:
void someFunction3() {
List<int> list = <int>[];
if (list.contains('1')) print('someFunction3'); // LINT
}
BAD:
void someFunction8() {
List<DerivedClass2> list = <DerivedClass2>[];
DerivedClass3 instance;
if (list.contains(instance)) print('someFunction8'); // LINT
}
BAD:
abstract class SomeIterable<E> implements Iterable<E> {}
abstract class MyClass implements SomeIterable<int> {
bool badMethod(String thing) => this.contains(thing); // LINT
}
GOOD:
void someFunction10() {
var list = [];
if (list.contains(1)) print('someFunction10'); // OK
}
GOOD:
void someFunction1() {
var list = <int>[];
if (list.contains(1)) print('someFunction1'); // OK
}
GOOD:
void someFunction4() {
List<int> list = <int>[];
if (list.contains(1)) print('someFunction4'); // OK
}
GOOD:
void someFunction5() {
List<ClassBase> list = <ClassBase>[];
DerivedClass1 instance;
if (list.contains(instance)) print('someFunction5'); // OK
}
abstract class ClassBase {}
class DerivedClass1 extends ClassBase {}
GOOD:
void someFunction6() {
List<Mixin> list = <Mixin>[];
DerivedClass2 instance;
if (list.contains(instance)) print('someFunction6'); // OK
}
abstract class ClassBase {}
abstract class Mixin {}
class DerivedClass2 extends ClassBase with Mixin {}
GOOD:
void someFunction7() {
List<Mixin> list = <Mixin>[];
DerivedClass3 instance;
if (list.contains(instance)) print('someFunction7'); // OK
}
abstract class ClassBase {}
abstract class Mixin {}
class DerivedClass3 extends ClassBase implements Mixin {}
DEPRECATED: This rule is deprecated in favor of
collection_methods_unrelated_type
.
The rule will be removed in a future Dart release.
Usage
To enable the iterable_contains_unrelated_type
rule,
add iterable_contains_unrelated_type
under linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- iterable_contains_unrelated_type