Skip to content

eslint/no-useless-assignment false positive for conditional for-loop update expression #23789

Description

@graemefolk

What version of Oxlint are you using?

oxlint 1.71.0

What command did you run?

pnpm oxlint \
  --config scratch/oxlint-no-useless-assignment-conditional-update-repro.oxlintrc.json \
  scratch/oxlint-no-useless-assignment-conditional-update-repro.ts

What happened?

eslint/no-useless-assignment reports the conditional update expression in a for loop as unused:

function collectIds(ids: readonly string[], forward: boolean): string[] {
  const collected: string[] = [];
  const startIndex = forward ? 0 : ids.length - 1;

  for (
    let index = startIndex;
    forward ? index < ids.length : index >= 0;
    forward ? index++ : index--
  ) {
    collected.push(ids[index]!);
  }

  return collected;
}

Oxlint reports both index++ and index-- as useless assignments.

Those updates are used by the next loop condition check and by the next iteration's body. Without them, the loop behavior changes.

What did you expect to happen?

No diagnostic. The updated value is observably used on the next iteration.

Additional context

This came up while migrating a larger codebase to oxlint.

Metadata

Metadata

Assignees

Labels

Type

Fields

Priority

None yet

Effort

None yet

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions