oxc-project/oxc · warning · OxcDiagnostic

`for…of` can iterate over an iterable, `.toArray()` is unnec

Error message

`for…of` can iterate over an iterable, `.toArray()` is unnecessary.

What it means

Diagnostic from the oxlint rule `unicorn/no-useless-iterator-to-array` (nursery). This variant fires when the right side of a `for…of` (or `for await…of`) loop is an `.toArray()` call. `for…of` iterates over any iterable directly, so materializing an array first is wasted work and memory. An autofix deletes the `.toArray()` member call.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/no_useless_iterator_to_array.rs:28

use oxc_span::{GetSpan, Span};

use crate::{
    AstNode,
    ast_util::{is_method_call, is_new_expression},
    context::LintContext,
    fixer::{RuleFix, RuleFixer},
    rule::Rule,
};

fn iterable_accepting_diagnostic(span: Span, description: &str) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!(
        "`{description}` accepts an iterable, `.toArray()` is unnecessary."
    ))
    .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)
}

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Delete `.toArray()` from the loop head: `for (const x of iterator.toArray())` -> `for (const x of iterator)`.
  2. Apply the rule's autofix (`oxlint --fix` or editor quick fix).
  3. Keep `.toArray()` only if the loop body mutates the collection while iterating or needs array APIs; otherwise remove it.
  4. Disable the rule inline if your target runtime lacks iterator helpers.

Example fix

// before
for (const item of iterator.toArray()) {
  handle(item);
}

// after
for (const item of iterator) {
  handle(item);
}
Defensive patterns

Strategy: validation

Validate before calling

# detect for/for-await ... of <expr>.toArray()
rg -n --type js -U 'for\s*(?:await\s*)?\(\s*(?:const|let|var)\s+.*\s+of\s+.*\.toArray\(\)' src/

Prevention

When it happens

Trigger: `for (const x of iterator.toArray());`, `for (const x of foo.bar().toArray());`, `for await (const x of iterator.toArray())`, including parenthesized forms `for (const x of (iterator.toArray()));`. Not fired for optional `iterator?.toArray()` or `iterator.toArray?.()`.

Common situations: Loops written before an API switched from returning arrays to returning iterators (e.g. `values().take(10)`), where the old `.toArray()` was left in place. Shows up when enabling unicorn nursery rules in oxlint.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/f5b3aaa3701d1bed. Report an issue: GitHub.