Skip to content

noUselessSwitchCase (since v1.0.0)

Diagnostic Category: lint/complexity/noUselessSwitchCase

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;
}
complexity/noUselessSwitchCase.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;
}
complexity/noUselessSwitchCase.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;
}