跳转到内容

noUselessSwitchCase

Diagnostic Category: lint/complexity/noUselessSwitchCase

Since: v1.0.0

Sources:

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.

switch (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:

switch (foo) {
case 0:
break;
default:
break;
}
switch (foo) {
case 0:
break;
}