Skip to content
← Back to rules

eslint/no-unreachable Correctness

This rule is turned on by default.

它的作用

禁止在 returnthrowcontinuebreak 语句之后出现不可达代码。

如果在 tsconfig.json 中配置了 allowUnreachableCode: false,则可以为 TypeScript 代码禁用此规则, 因为 TypeScript 编译器会强制执行此检查。

为什么这不好?

returnthrowcontinuebreak 语句之后的不可达代码永远不会被执行。

示例

此规则的错误代码示例:

ts
function foo() {
  return 2;
  console.log("这段代码将永远不会被执行");
}

此规则的正确代码示例:

ts
function foo() {
  console.log("这段代码将会被执行");
  return 2;
}

如何使用

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

json
{
  "rules": {
    "no-unreachable": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "no-unreachable": "error",
  },
});
bash
oxlint --deny no-unreachable

版本

此规则于 v0.4.4 中添加。

参考资料