Skip to content

noDuplicateCase (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"suspicious": {
"noDuplicateCase": "error"
}
}
}
}

Disallow duplicate case labels.

If a switch statement has duplicate test expressions in case clauses, it is likely that a programmer copied a case clause but forgot to change the test expression.

switch (a) {
case 1:
break;
case 1:
break;
default:
break;
}
code-block.js:4:10 lint/suspicious/noDuplicateCase ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ Duplicate case label.

2 │ case 1:
3 │ break;
> 4 │ case 1:
│ ^
5 │ break;
6 │ default:

ℹ The first similar label is here:

1 │ switch (a) {
> 2 │ case 1:
│ ^
3 │ break;
4 │ case 1:

ℹ Only the first matching case is selected. If it does not break or return, execution can fall through to this duplicate's body. It is probably a copy-paste mistake.

ℹ Remove the duplicate case, or change its value to the one you meant.

switch (a) {
case one:
break;
case one:
break;
default:
break;
}
code-block.js:4:10 lint/suspicious/noDuplicateCase ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ Duplicate case label.

2 │ case one:
3 │ break;
> 4 │ case one:
│ ^^^
5 │ break;
6 │ default:

ℹ The first similar label is here:

1 │ switch (a) {
> 2 │ case one:
│ ^^^
3 │ break;
4 │ case one:

ℹ Only the first matching case is selected. If it does not break or return, execution can fall through to this duplicate's body. It is probably a copy-paste mistake.

ℹ Remove the duplicate case, or change its value to the one you meant.

switch (a) {
case "1":
break;
case "1":
break;
default:
break;
}
code-block.js:4:10 lint/suspicious/noDuplicateCase ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ Duplicate case label.

2 │ case "1":
3 │ break;
> 4 │ case "1":
│ ^^^
5 │ break;
6 │ default:

ℹ The first similar label is here:

1 │ switch (a) {
> 2 │ case "1":
│ ^^^
3 │ break;
4 │ case "1":

ℹ Only the first matching case is selected. If it does not break or return, execution can fall through to this duplicate's body. It is probably a copy-paste mistake.

ℹ Remove the duplicate case, or change its value to the one you meant.

switch (a) {
case 1:
break;
case 2:
break;
default:
break;
}
switch (a) {
case one:
break;
case two:
break;
default:
break;
}
switch (a) {
case "1":
break;
case "2":
break;
default:
break;
}