Skip to content
← Back to rules

eslint/no-unreachable-loop Nursery

功能 ​

禁止循环体只允许执行一次迭代的循环。

为什么这不好? ​

如果循环总是在第二次迭代之前退出,通常是意外情况, 可以使用更简单的控制流替代。

示例 ​

此规则的错误代码示例:

js
for (const item of items) {
  console.log(item);
  break;
}

此规则的正确代码示例:

js
for (const item of items) {
  console.log(item);
}

配置 ​

忽略 ​

类型:array

忽略[n] ​

类型:"WhileStatement" | "DoWhileStatement" | "ForStatement" | "ForInStatement" | "ForOfStatement"

使用方法 ​

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

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

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

版本 ​

此规则在 v1.79.0 中添加。

参考资料 ​