Skip to content

fix(linter/unicorn): avoid prefer-array-find rest destructuring false positive#23654

Open
ColemanDunn wants to merge 1 commit into
oxc-project:mainfrom
ColemanDunn:codex/prefer-array-find-rest-destructure
Open

fix(linter/unicorn): avoid prefer-array-find rest destructuring false positive#23654
ColemanDunn wants to merge 1 commit into
oxc-project:mainfrom
ColemanDunn:codex/prefer-array-find-rest-destructure

Conversation

@ColemanDunn

@ColemanDunn ColemanDunn commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR fixes two issues in unicorn/prefer-array-find:

  • Oxlint reports a false positive for rest destructuring from filter(), where find() is not an equivalent replacement.
  • The rule docs are missing supported cases that the implementation already detects, including array destructuring and findLast()-style patterns. The upstream eslint-plugin-unicorn docs cover these cases, so this brings Oxlint's docs closer to the equivalent rule.

False positive

On this line, Oxlint currently reports a false positive diagnostic from unicorn/prefer-array-find:

const [element, ...restElements] = myArray.filter(isEven);

That diagnostic is incorrect because the code keeps both the first matching element and the remaining matching elements. Replacing it with find() would drop restElements, so it is not an equivalent transformation.

The fix is to only report array destructuring when there is no rest element. This still reports valid cases like:

const [element] = myArray.filter(isEven);

Docs mismatch

Oxlint already detects more than the docs describe. In addition to filter(...)[0] and .shift(), the implementation handles simple array destructuring and last-match cases using .at(-1) or .pop().

This PR updates the docs to include those supported patterns and their find() / findLast() replacements, matching the upstream eslint-plugin-unicorn docs more closely for the cases Oxlint currently implements.

Upstream parity mismatch still exists

eslint-plugin-unicorn also documents and detects .filter(fn).slice(-1)[0]-style last-element patterns. Oxlint does not currently detect that pattern, so this PR does not document it. That can be handled as a separate parity follow-up.

Tests

  • cargo fmt -p oxc_linter
  • cargo test -p oxc_linter prefer_array_find -- --nocapture
  • Manual local repro with oxlint -D unicorn/prefer-array-find

AI assistance

AI assistance was used to prepare this change. I have reviewed the code and remain responsible for the submission.

@ColemanDunn ColemanDunn requested a review from camc314 as a code owner June 19, 2026 20:23
@codspeed-hq

codspeed-hq Bot commented Jun 19, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 5 untouched benchmarks
⏩ 66 skipped benchmarks1


Comparing ColemanDunn:codex/prefer-array-find-rest-destructure (e9ad446) with main (b1948a1)

Open in CodSpeed

Footnotes

  1. 66 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@ColemanDunn ColemanDunn force-pushed the codex/prefer-array-find-rest-destructure branch from cbda5f5 to e9ad446 Compare June 19, 2026 20:54
@camc314 camc314 self-assigned this Jun 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants