avoid-collection-equality-checks

Path
lint/correctness/avoid-collection-equality-checks
Default
On (recommended)
Origin
Falcon
Rule
32 of 512
recommended

Flags equality comparisons between non-constant SDK collections.

List, Set, and Map equality compares identity rather than contents. Use a collection equality helper instead. Comparisons between two constants are exempt, as are unrelated types (covered by unrelated-type-equality-checks), null comparisons, and custom collection classes that may override equality.

Invalid

example.dartdart
void compare(List<int> a, List<int> b, Set<int> c, Set<int> d, Map<String, int> e, Map<String, int> f) {
  print(a == b);
  print(a != b);
  print(c == d);
  print(e != f);
  print(a == const [1]);
  print(<int>[1] == <int>[2]);
}
Collection equality compares identity — use a content equality helper instead.
1void compare(List<int> a, List<int> b, Set<int> c, Set<int> d, Map<String, int> e, Map<String, int> f) {
2 print(a == b);
Collection equality compares identity — use a content equality helper instead.
2 print(a == b);
3 print(a != b);
Collection equality compares identity — use a content equality helper instead.
3 print(a != b);
4 print(c == d);
Collection equality compares identity — use a content equality helper instead.
4 print(c == d);
5 print(e != f);
Collection equality compares identity — use a content equality helper instead.
5 print(e != f);
6 print(a == const [1]);
Collection equality compares identity — use a content equality helper instead.
6 print(a == const [1]);
7 print(<int>[1] == <int>[2]);

Valid

example.dartdart
void compare(List<int> values, dynamic unknown) {
  const a = [1];
  const b = [1];
  print(a == b);
  print(const <int>{1} == const <int>{1});
  print(const <String, int>{'x': 1} == const <String, int>{'x': 1});
  print(values == null);
  print(values == unknown);
  print(values == 1);
  print(1 == 2);
  print(identical(values, values));
}

class ValueList implements List<int> {
  @override
  bool operator ==(Object other) => other is ValueList;
  @override
  int get hashCode => 0;
  @override
  dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}
void custom(ValueList first, ValueList second) { print(first == second); }
void unrelated(List<int> integers, List<String> strings, Set<int> unique) {
  print(integers == strings);
  print(integers == unique);
}

Configure avoid-collection-equality-checks

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "avoid-collection-equality-checks": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/correctness/avoid-collection-equality-checks: <reason>