Skip to content
NewBun 1.4.5 is out. Release notes
AphrodyBun

Linter · Rules

no-unmodified-loop-condition

Disallow loop conditions that test variables the loop never changes

If nothing in a while, do...while, or for loop changes the variables in its condition, the loop either never runs or never stops. Usually someone forgot the update, or it changes the wrong variable.

Configure

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"no-unmodified-loop-condition" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"no-unmodified-loop-condition" = ["error", {
  checkConditionalExpressions = false,
}]

Examples

Reported:

let node = list.head;

while (node) {
  print(node.value);
  list.next();
}

Not reported:

let node = list.head;

while (node) {
  print(node.value);
  node = node.next;
}

In a comparison, the loop only needs to change one side:

const limit = 3;
let attempts = 0;

while (attempts < limit) {
  attempts++;
}

A property or a call's result can change without an assignment, so they aren't checked. Neither is a comparison that contains one:

let max = 10;

while (queue.length > max) {
  handle(queue);
}

A variable also counts as changed if the loop uses a function declaration that assigns to it:

let done = false;

function finish() {
  done = true;
}

while (!done) {
  finish();
}

Options

OptionDefaultWhat it does
checkConditionalExpressionsfalseCheck each variable of a ? : in the condition separately.

By default a ? : is treated like a comparison, so one changed variable is enough.

let urgent = true;
let high = 3;
let low = 5;

while (urgent ? high : low) {
  urgent = isUrgent();
}

With the option, high and low are reported:

let urgent = true;
let high = 3;
let low = 5;

while (urgent ? high : low) {
  urgent = isUrgent();
}