collection-methods-unrelated-type
Same as collection_methods_unrelated_type in Dart lints
- Group
- Correctness
- Path
lint/correctness/collection-methods-unrelated-type- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 205 of 512
Reports collection lookups whose argument type is unrelated to the collection's type argument.
With
exactly one argument, Iterable<E>.contains, List<E>.remove,
Map<K, V>.containsKey/containsValue/remove, Queue<E>.remove,
Set<E>.lookup/remove, and Map<K, V>.[] are checked on a receiver
(explicit, or the implicit this of the enclosing type) that implements the
declaring collection, whatever the receiver overrides. The argument must not
be unrelated (TypeRelation::RuntimeUnrelatedTo) to the type argument.
Invalid
import 'dart:collection';
void bad(
Iterable<int> iterable,
List<String> list,
Set<int> set,
Queue<String> queue,
Map<String, int> map,
) {
iterable.contains('x');
list.remove(1);
set.lookup('x');
set.remove(false);
queue.remove(1);
map['key'];
map[1];
map.containsKey(1);
map.containsValue('value');
map.remove(false);
}
class IntValues {
final List<int> values;
IntValues(this.values) {
values.contains('x');
}
}
class BaseValues {
final List<int> values;
BaseValues(this.values);
}
class ForwardedValues extends BaseValues {
ForwardedValues(super.values) {
values.contains('x');
}
}
void scopedCollections(List<int> values) {
<String>['x'].forEach((String value) {
values.contains(value);
});
for (final value in <String>['x']) {
values.remove(value);
}
for (final [value] in <List<String>>[
['x'],
]) {
values.contains(value);
}
{
final value = 'x';
values.contains(value);
}
values.contains(1);
}
class Lookup implements Map<String, int> {
@override
bool containsKey(Object? key) => false;
@override
dynamic noSuchMethod(Invocation invocation) => null;
}
void overridden(Lookup lookup, List<int> values) {
lookup.containsKey(1);
values..contains('x');
}
extension IntListChecks on List<int> {
bool hasText(String text) => contains(text);
}) { iterable.contains('x'); iterable.contains('x'); list.remove(1); list.remove(1); set.lookup('x'); set.lookup('x'); set.remove(false); set.remove(false); queue.remove(1); map['key']; map[1]; map[1]; map.containsKey(1); map.containsKey(1); map.containsValue('value'); map.containsValue('value'); map.remove(false); IntValues(this.values) { values.contains('x'); ForwardedValues(super.values) { values.contains('x'); <String>['x'].forEach((String value) { values.contains(value); for (final value in <String>['x']) { values.remove(value); ]) { values.contains(value); final value = 'x'; values.contains(value);void overridden(Lookup lookup, List<int> values) { lookup.containsKey(1); lookup.containsKey(1); values..contains('x');extension IntListChecks on List<int> { bool hasText(String text) => contains(text);Valid
import 'dart:collection';
import 'dart:core' as core;
void good(
Iterable<num> iterable,
List<String> list,
Set<int> set,
Queue<String> queue,
core.Map<String, int> map,
dynamic unknown,
) {
iterable.contains(1);
list.remove('x');
set.lookup(1);
queue.remove('x');
map['key'];
map.containsKey('key');
map.containsValue(1);
map.remove('key');
map.containsKey(unknown);
}
class Map<K, V> {
bool containsKey(Object? value) => false;
}
void shadowed(Map<String, int> map) {
map.containsKey(1);
}
void functionKeys(core.Map<core.Function?, core.int> makers, core.int Function() maker) {
makers[maker];
}
// Named record fields match by name, not by source order.
void recordKeys() {
final byScope = <({core.String id, core.int count}), core.List<core.int>>{};
final key = (id: 'x', count: 1);
(byScope[key] ??= <core.int>[]).add(1);
byScope.containsKey((id: 'y', count: 2));
}Configure collection-methods-unrelated-type
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"correctness": {
"collection-methods-unrelated-type": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/collection-methods-unrelated-type: <reason>More correctness rules
- comment-references
comment_referencesrecommendedFlags doc-comment references
[name]that resolve to nothing in scope. - correct-order-for-super-dispose
correct_order_for_super_disposerecommendedflutterRequire
super.dispose()to be the last call indispose. - depend-on-referenced-packages
depend_on_referenced_packagesrecommendedcross-fileRequire imported packages to be declared in
pubspec.yaml. - deprecated-member-use-from-same-package
deprecated_member_use_from_same_packageReports uses of deprecated declarations from their own package.
- diagnostic-describe-all-properties
diagnostic_describe_all_propertiesflutterRequires every public property of a
Diagnosticableclass to be referenced in its debug methods. - discarded-futures
discarded_futuresDon't discard
Futures returned by calls in synchronous functions. - dispose-fieldsrecommendedflutter
Requires other owned disposable State fields to be released in
dispose(). - dispose-focus-node-and-timerrecommendedflutter
Requires owned
FocusNodeand Timer fields to be released inState.dispose().