69 lines
1.7 KiB
Plaintext
69 lines
1.7 KiB
Plaintext
== Why is this an issue?
|
|
|
|
The Dart collections API has methods that allow developers to overcome type-safety restriction of the parameter, such as `Iterable.contains`.
|
|
|
|
When the actual type of the object provided to these methods is not consistent with the target collection's actual type, those methods will always return `false` or `null`. This is most likely unintended and hides a design problem.
|
|
|
|
This rule raises an issue when the type of the argument of the following APIs is unrelated to the type used for the collection declaration:
|
|
|
|
* `Iterable<E>.contains`
|
|
* `List<E>.remove`
|
|
* `Map<K, V>.containsKey`
|
|
* `Map<K, V>.containsValue`
|
|
* `Map<K, V>.remove`
|
|
* `Map<K, V>.[]`
|
|
* `Queue<E>.remove`
|
|
* `Set<E>.lookup`
|
|
* `Set<E>.remove`
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,dart]
|
|
----
|
|
void foo(List<String> list, Map<Int, String> map) {
|
|
|
|
list.contains(100); // Noncompliant, list contains only Strings
|
|
list.remove(3.14); // Noncompliant
|
|
|
|
map.containsKey["a"]; // Noncompliant
|
|
map["123"]; // Noncompliant
|
|
}
|
|
----
|
|
|
|
=== Compliant solution
|
|
|
|
[source,dart]
|
|
----
|
|
void foo(List<String> list, Map<Int, String> map) {
|
|
|
|
list.contains("100");
|
|
list.remove("3.14");
|
|
|
|
map.containsValue["a"];
|
|
map[123];
|
|
}
|
|
----
|
|
|
|
== Resources
|
|
|
|
* Dart Docs - https://dart.dev/tools/linter-rules/collection_methods_unrelated_type[Dart Linter rule - collection_methods_unrelated_type]
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
The argument type '<type of method argument>' isn't related to '<type of collection>'.
|
|
|
|
=== Highlighting
|
|
The argument of the call to the method of the collection.
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
endif::env-github,rspecator-view[]
|