π Disallow useless case in switch statements.
πΌ This rule is enabled in the following configs: β
recommended, βοΈ unopinionated.
π‘ This rule is manually fixable by editor suggestions.
An empty case before the last default case is useless.
Removing it avoids suggesting that the case has distinct behavior when execution would immediately enter the default case.
In TypeScript files, empty case null:, case undefined:, and case void 0: before default: are allowed so type-aware exhaustiveness checks can require explicit nullish union members while default: still handles unexpected values.
// β
switch (foo) {
case 1:
default:
handleDefaultCase();
break;
}
// β
switch (foo) {
default:
handleDefaultCase();
break;
}// β
switch (foo) {
case 1:
case 2:
handleCase1And2();
break;
}// β
switch (foo) {
case 1:
handleCase1();
break;
default:
handleDefaultCase();
break;
}// β
switch (foo) {
case 1:
handleCase1();
// Fallthrough
default:
handleDefaultCase();
break;
}// β
switch (foo) {
// This is actually useless, but we only check cases where the last case is the `default` case
case 1:
default:
handleDefaultCase();
break;
case 2:
handleCase2();
break;
}