Skip to content

Latest commit

Β 

History

History
83 lines (69 loc) Β· 1.57 KB

no-useless-switch-case.md

File metadata and controls

83 lines (69 loc) Β· 1.57 KB

no-useless-switch-case

πŸ“ 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.

Examples

// ❌
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;
}