collection_methods_unrelated_type

Group: errors

Maturity: stable

Dart SDK: >= 2.19.0 • (Linter v1.29.0)

Since info is static, may be stale

View all Lint Rules

Using the Linter

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:

BAD:

void someFunction() {
  var list = <int>[];
  if (list.contains('1')) print('someFunction'); // LINT
}

BAD:

void someFunction() {
  var set = <int>{};
  set.remove('1'); // LINT
}

GOOD:

void someFunction() {
  var list = <int>[];
  if (list.contains(1)) print('someFunction'); // OK
}

GOOD:

void someFunction() {
  var set = <int>{};
  set.remove(1); // OK
}

Usage

To enable the collection_methods_unrelated_type lint, add collection_methods_unrelated_type under linter > rules in your analysis_options.yaml file:

linter:
  rules:
    - collection_methods_unrelated_type