avoid-duplicate-map-keys

Path
lint/correctness/avoid-duplicate-map-keys
Default
On (recommended)
Origin
Falcon
Rule
51 of 512
recommended

Flags repeated literal keys in map literals, including literal spreads and collection if entries.

A later key replaces the earlier value. Only scalar literal keys with equal token spelling are compared; arbitrary expressions and unknown spreads are left alone. Mutually exclusive if branches are checked independently.

Invalid

example.dartdart
void f(bool condition) {
  final first = {'a': 1, 'a': 2};
  final second = {1: 'a', 1: 'b'};
  final third = {true: 1, true: 2};
  final fourth = {null: 1, null: 2};
  final fifth = {'a': 1, ...{'a': 2}};
  final sixth = {'a': 1, if (condition) 'a': 2};
  final seventh = {if (condition) 'a': 1, 'a': 2};
}
Duplicate map key replaces an earlier entry. Remove or change the key.
1void f(bool condition) {
2 final first = {'a': 1, 'a': 2};
Duplicate map key replaces an earlier entry. Remove or change the key.
2 final first = {'a': 1, 'a': 2};
3 final second = {1: 'a', 1: 'b'};
Duplicate map key replaces an earlier entry. Remove or change the key.
3 final second = {1: 'a', 1: 'b'};
4 final third = {true: 1, true: 2};
Duplicate map key replaces an earlier entry. Remove or change the key.
4 final third = {true: 1, true: 2};
5 final fourth = {null: 1, null: 2};
Duplicate map key replaces an earlier entry. Remove or change the key.
5 final fourth = {null: 1, null: 2};
6 final fifth = {'a': 1, ...{'a': 2}};
Duplicate map key replaces an earlier entry. Remove or change the key.
6 final fifth = {'a': 1, ...{'a': 2}};
7 final sixth = {'a': 1, if (condition) 'a': 2};
Duplicate map key replaces an earlier entry. Remove or change the key.
7 final sixth = {'a': 1, if (condition) 'a': 2};
8 final seventh = {if (condition) 'a': 1, 'a': 2};

Valid

example.dartdart
int next() => 1;
void f(bool condition, Map<String, int> other) {
  final distinct = {'a': 1, 'b': 2};
  final types = <Object, int>{1: 1, '1': 2};
  final branches = {if (condition) 'a': 1 else 'a': 2};
  final unknown = {'a': 1, ...other};
  final effects = {next(): 1, next(): 2};
  final spaces = {'a b': 1, 'ab': 2};
  final scopes = {'a': {'a': 1}};
  final interpolated = {'$condition': 1, '$condition': 2};
}

Configure avoid-duplicate-map-keys

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "avoid-duplicate-map-keys": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/correctness/avoid-duplicate-map-keys: <reason>