oxc-project/oxc · warning · OxcDiagnostic
`Iterator` has a `.{method}()` method, `.toArray()` is unnec
Error message
`Iterator` has a `.{method}()` method, `.toArray()` is unnecessary. What it means
Diagnostic from the oxlint rule `unicorn/no-useless-iterator-to-array` (nursery). This variant fires when `.every()`, `.find()`, `.forEach()`, `.some()`, or `.reduce()` is called on the result of `.toArray()`. The `Iterator` prototype provides these methods directly, so the array conversion is unnecessary. It is reported as a suggestion, not an autofix, because Array callbacks receive an extra `array` argument that Iterator callbacks do not; the rule skips the report entirely when the callback declares that parameter or when a `thisArg` is passed.
Source
Thrown at crates/oxc_linter/src/rules/unicorn/no_useless_iterator_to_array.rs:42
.with_label(span)
}
fn for_of_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("`for…of` can iterate over an iterable, `.toArray()` is unnecessary.")
.with_label(span)
}
fn yield_star_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("`yield*` can delegate to an iterable, `.toArray()` is unnecessary.")
.with_label(span)
}
fn spread_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Spread works on iterables, `.toArray()` is unnecessary.").with_label(span)
}
fn iterator_method_diagnostic(span: Span, method: &str) -> OxcDiagnostic {
OxcDiagnostic::warn(format!(
"`Iterator` has a `.{method}()` method, `.toArray()` is unnecessary."
))
.with_label(span)
}
#[derive(Debug, Default, Clone)]
pub struct NoUselessIteratorToArray;
declare_oxc_lint!(
/// ### What it does
///
/// Disallow unnecessary `.toArray()` on iterators.
///
/// ### Why is this bad?
///
/// [`Iterator.prototype.toArray()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator/toArray)
/// converts an iterator to an array. However, this conversion is unnecessary in many cases:
///View on GitHub (pinned to e1e7af627c)
Solutions
- Call the method on the iterator: `iterator.toArray().every(fn)` -> `iterator.every(fn)`.
- Before applying, verify the callback does not use the third (fourth for `reduce`) `array` argument.
- If the callback needs the array argument or a `thisArg`, keep `.toArray()`; the rule will then stop reporting.
- For `.filter()`/`.map()`/`.flatMap()` keep `.toArray()` if you need an array result.
Example fix
// before const ok = iterator.toArray().every(x => x > 0); // after const ok = iterator.every(x => x > 0);
Defensive patterns
Strategy: validation
Validate before calling
# detect toArray().every/find/forEach/some/reduce and callback arity
rg -n --type js 'toArray\(\)\.(?:every|find|forEach|some|reduce)\(' src/ Prevention
- Check callback arity before rewriting: callbacks using the `array` parameter (3rd arg, 4th for reduce) must keep `.toArray()`.
- Pass no `thisArg`; its presence keeps the array version required.
- Prefer calling every/find/forEach/some/reduce directly on the iterator when callbacks take (value[, index]).
When it happens
Trigger: `iterator.toArray().every(fn)`, `.find(fn)`, `.forEach(fn)`, `.some(fn)`, `.reduce(fn, init)` (exactly 2 arguments for reduce, at most 1 for the others). Not fired when the callback uses the array parameter (`(v, i, array) => ...`), when a `thisArg` is passed (`every(fn, thisArg)`), or for `.filter()`/`.map()`/`.flatMap()` whose Iterator versions return iterators instead of arrays.
Common situations: Pipeline code such as `values().take(10).toArray().every(x => x > 0)` written by developers used to Array methods. Appears with unicorn nursery rules enabled; behavior-sensitive callbacks mean the fix needs human review.
Related errors
- `{description}` accepts an iterable, `.toArray()` is unneces
- `for…of` can iterate over an iterable, `.toArray()` is unnec
- `yield*` can delegate to an iterable, `.toArray()` is unnece
- Spread works on iterables, `.toArray()` is unnecessary.
- The catch parameter {caught_ident:?} should be named {expect
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/3a5d3e8603f839c4.
Report an issue: GitHub.