noUselessSwitchCase (since v1.0.0)
Diagnostic Category: lint/complexity/noUselessSwitchCase
Sources:
- Same as:
unicorn/no-useless-switch-case
Disallow useless case in switch statements.
A switch statement can optionally have a default clause.
The default clause will be still executed only if there is no match in the case clauses.
An empty case clause that precedes the default clause is thus useless.
Examples
Section titled ExamplesInvalid
Section titled Invalidswitch (foo) { case 0: default: break; case 1: break;}code-block.js:2:5 lint/complexity/noUselessSwitchCase FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Useless case clause.
1 │ switch (foo) {
> 2 │ case 0:
│ ^^^^^^^
3 │ default:
4 │ break;
ℹ because the default clause is present:
1 │ switch (foo) {
2 │ case 0:
> 3 │ default:
│ ^^^^^^^^
> 4 │ break;
│ ^^^^^^
5 │ case 1:
6 │ break;
ℹ Unsafe fix: Remove the useless case.
1 1 │ switch (foo) {
2 │ - ····case·0:
3 2 │ default:
4 3 │ break;
switch (foo) { default: case 0: break; case 1: break;}code-block.js:3:5 lint/complexity/noUselessSwitchCase FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Useless case clause.
1 │ switch (foo) {
2 │ default:
> 3 │ case 0:
│ ^^^^^^^
> 4 │ break;
│ ^^^^^^
5 │ case 1:
6 │ break;
ℹ because the default clause is present:
1 │ switch (foo) {
> 2 │ default:
│ ^^^^^^^^
3 │ case 0:
4 │ break;
ℹ Unsafe fix: Remove the useless case.
1 1 │ switch (foo) {
2 │ - ····default:
3 │ - ····case·0:
2 │ + ····default:
4 3 │ break;
5 4 │ case 1:
Valid
Section titled Validswitch (foo) { case 0: break; default: break;}switch (foo) { case 0: break;}