# for-direction

> Require the update clause of a `for` loop to move the counter toward the end of the loop

A loop that tests `i < 10` and then runs `i--` moves away from 10, so the test stays true and the loop never ends. It's almost always a typo, like `--` for `++` or `<` for `>`.

The rule looks at `++`, `--`, `+=` and `-=` on the counter. If it can't tell whether the step is positive or negative, it reports nothing.

This rule is on by default.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

On by default as an error. To turn it off:

```toml bunfig.toml
[lint.rules]
"for-direction" = "off" # or "warn"
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad */}
```js
for (let i = 0; i < items.length; i--) {
  render(items[i]);
}
```

{/* bad */}
```js
for (let i = items.length - 1; i >= 0; i += 1) {
  render(items[i]);
}
```

Not reported:

{/* good */}
```js
for (let i = 0; i < items.length; i++) {
  render(items[i]);
}

for (let i = items.length - 1; i >= 0; i -= 1) {
  render(items[i]);
}
```

The rule can't know the sign of `step` here, so it leaves the loop alone:

{/* good */}
```js
function walk(items, step) {
  for (let i = 0; i < items.length; i += step) {
    render(items[i]);
  }
}
```
