collection_methods_unrelated_type
Invocation of various collection methods with arguments of unrelated types.
This rule is available as of Dart 2.19.
Rule sets: core, recommended, flutter
Details
#DON'T invoke certain collection method with an argument with an unrelated type.
Doing this will invoke ==
on the collection's elements and most likely will
return false
.
An argument passed to a collection method should relate to the collection type as follows:
- an argument to
Iterable<E>.contains
should be related toE
- an argument to
List<E>.remove
should be related toE
- an argument to
Map<K, V>.containsKey
should be related toK
- an argument to
Map<K, V>.containsValue
should be related toV
- an argument to
Map<K, V>.remove
should be related toK
- an argument to
Map<K, V>.[]
should be related toK
- an argument to
Queue<E>.remove
should be related toE
- an argument to
Set<E>.lookup
should be related toE
- an argument to
Set<E>.remove
should be related toE
BAD:
dart
void someFunction() {
var list = <int>[];
if (list.contains('1')) print('someFunction'); // LINT
}
BAD:
dart
void someFunction() {
var set = <int>{};
set.remove('1'); // LINT
}
GOOD:
dart
void someFunction() {
var list = <int>[];
if (list.contains(1)) print('someFunction'); // OK
}
GOOD:
dart
void someFunction() {
var set = <int>{};
set.remove(1); // OK
}
Usage
#To enable the collection_methods_unrelated_type
rule,
add collection_methods_unrelated_type
under linter > rules in your
analysis_options.yaml
file:
analysis_options.yaml
yaml
linter:
rules:
- collection_methods_unrelated_type
除非另有说明,文档之所提及适用于 Dart 3.5.4 版本,本页面最后更新时间: 2024-08-02。 查看文档源码 或者 报告页面问题。