avoid-duplicate-collection-elements

Path
lint/correctness/avoid-duplicate-collection-elements
Default
Off, opt in
Origin
Falcon
Rule
47 of 512

Flags repeated scalar literals and stable local reads in list and set literals.

Duplicate entries often indicate a copy-and-paste mistake. Comparisons retain literal spelling. Calls, getters, spreads, and control-flow elements end the comparison region, since they can change values or contribute unknown items. Map keys are covered separately by avoid-duplicate-map-keys.

Invalid

example.dartdart
void f(int value, String text) {
  final a = [1, 1];
  final b = {'a', 'a'};
  final c = [true, true];
  final d = [null, null];
  final e = [value, value];
  final g = [text, text];
  final h = [1, 2, 1];
}
Remove or replace this duplicate collection element.
1void f(int value, String text) {
2 final a = [1, 1];
Remove or replace this duplicate collection element.
2 final a = [1, 1];
3 final b = {'a', 'a'};
Remove or replace this duplicate collection element.
3 final b = {'a', 'a'};
4 final c = [true, true];
Remove or replace this duplicate collection element.
4 final c = [true, true];
5 final d = [null, null];
Remove or replace this duplicate collection element.
5 final d = [null, null];
6 final e = [value, value];
Remove or replace this duplicate collection element.
6 final e = [value, value];
7 final g = [text, text];
Remove or replace this duplicate collection element.
7 final g = [text, text];
8 final h = [1, 2, 1];

Valid

example.dartdart
int next() => 1;
void f(int value, bool condition, List<int> other) {
  final a = [1, 2];
  final b = ['a b', 'ab'];
  final c = [next(), next()];
  final d = [value, value++];
  final e = [value, next(), value];
  final g = [1, ...other, 1];
  final h = [if (condition) 1 else 1];
  final i = [[1], [1]];
  final j = ['$value', '$value'];
}
class Base {
  int value = 0;
  List<int> values() => [value, value];
}
class Changing extends Base {
  int counter = 0;
  @override
  int get value => counter++;
  @override
  set value(int next) {}
}

Configure avoid-duplicate-collection-elements

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "avoid-duplicate-collection-elements": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/correctness/avoid-duplicate-collection-elements: <reason>