Skip to content
← Back to rules

unicorn/no-useless-switch-case Pedantic

🚧 An auto-fix is planned for this rule, but not implemented at this time.

What it does ​

Disallows useless default cases in switch statements.

Why is this bad? ​

An empty case before the last default case is useless, as the default case will catch it regardless.

Examples ​

Examples of incorrect code for this rule:

javascript
switch (foo) {
  case 1:
  default:
    handleDefaultCase();
    break;
}

Examples of correct code for this rule:

javascript
switch (foo) {
  case 1:
  case 2:
    handleCase1And2();
    break;
}

How to use ​

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "unicorn/no-useless-switch-case": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/no-useless-switch-case": "error",
  },
});
bash
oxlint --deny unicorn/no-useless-switch-case

Version ​

This rule was added in v0.0.18.

References ​