Skip to content

noUselessSwitchCase

诊断类别:lint/complexity/noUselessSwitchCase

¥Diagnostic Category: lint/complexity/noUselessSwitchCase

自从:v1.0.0

¥Since: v1.0.0

来源:

¥Sources:

禁止在 switch 语句中使用无用的 case

¥Disallow useless case in switch statements.

switch 语句可以有可选的 default 子句。

¥A switch statement can optionally have a default clause.

仅当 case 子句中没有匹配项时,才会执行 default 子句。因此,位于 default 子句之前的空 case 子句是无用的。

¥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

¥Invalid

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:

¥Valid

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

¥Related links