Skip to content
← Back to rules

eslint/no-unreachable-loop Nursery

What it does

Disallow loops whose body allows only one iteration.

Why is this bad?

A loop that always exits before a second iteration is usually accidental and can be replaced with simpler control flow.

Examples

Examples of incorrect code for this rule:

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

Examples of correct code for this rule:

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

Configuration

ignore

type: array

ignore[n]

type: "WhileStatement" | "DoWhileStatement" | "ForStatement" | "ForInStatement" | "ForOfStatement"

How to use

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

Version

This rule was added in vnext.

References