For AI agents: the complete documentation index is available at https://rslint.rs/llms.txt, the full documentation bundle is available at https://rslint.rs/llms-full.txt, and this page is available as Markdown at https://rslint.rs/rules/unicorn/prefer-while-loop-condition.md.
close

prefer-while-loop-condition

Added in v0.10.1

Configuration

rslint.config.ts
import { defineConfig, unicornPlugin } from '@rslint/core';

export default defineConfig([
  unicornPlugin.configs.recommended,
  {
    rules: {
      'unicorn/prefer-while-loop-condition': 'error',
    },
  },
]);

Rule Details

Prefer putting a loop's continuation condition in its while statement. This rule reports infinite loops whose first statement is an if without an else that only performs an unlabeled break.

Examples of incorrect code:

while (true) {
  if (!hasMore()) {
    break;
  }
  processNext();
}

for (;;) {
  if (done) break;
  processNext();
}

Examples of correct code:

while (hasMore()) {
  processNext();
}

while (!done) {
  processNext();
}

The rule checks while (true), for (;;), for (; true;), and do ... while (true). It ignores labeled loops, additional breaks targeting the same loop, and guards that reference lexical bindings declared inside the loop body. Breaks inside a nested loop, switch, or function do not prevent a report.

Automatic Fixes

The fix moves the negated guard condition into the loop header and removes the guard. Infinite for and do ... while loops become while loops.

Comments around the guard or loop syntax prevent automatic fixes. Comments inside the retained body are preserved and do not prevent fixing.

Conditions containing direct eval are reported without an automatic fix.

Options

This rule has no options.

Differences from upstream

Unlike eslint-plugin-unicorn v79.0.0, rslint withholds automatic fixes when the guard condition contains direct eval. For example, this code is reported but left unchanged by automatic fixing:

'use strict';

while (true) {
  if (eval('done()')) break;
  function done() {
    return true;
  }
}

Moving the eval call into the loop header would make done inaccessible and cause a ReferenceError.

Original Documentation