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.

作用

禁止在 switch 语句中出现无用的 default 分支。

为什么这很糟糕?

最后一个 default 分支前面的空分支是无用的,因为 无论如何,default 分支都会匹配到它。

示例

以下是此规则的错误代码示例:

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

以下是此规则的正确代码示例:

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

如何使用

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

版本

此规则是在 v0.0.18 中添加的。

参考资料