oxc-project/oxc · warning · OxcDiagnostic

Spread works on iterables, `.toArray()` is unnecessary.

Error message

Spread works on iterables, `.toArray()` is unnecessary.

What it means

Diagnostic from the oxlint rule `unicorn/no-useless-iterator-to-array` (nursery). This variant fires when a spread element whose argument is an `.toArray()` call appears inside an array literal, a call expression, or a `new` expression. Spread consumes any iterable, so materializing an array first is unnecessary. An autofix deletes the `.toArray()` call.

Source

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

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)
}

#[derive(Debug, Default, Clone)]
pub struct NoUselessIteratorToArray;

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Disallow unnecessary `.toArray()` on iterators.
    ///
    /// ### Why is this bad?

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Spread the iterator itself: `[...iterator.toArray()]` -> `[...iterator]`, `call(...iterator.toArray())` -> `call(...iterator)`.
  2. Apply the rule's autofix.
  3. Keep `.toArray()` when you need the array twice or mutate it afterwards; the rule only flags the single-use spread sites.
  4. Disable the rule inline for runtimes without iterator-helper support.

Example fix

// before
const items = [...iterator.toArray()];
run(...iterator.toArray());

// after
const items = [...iterator];
run(...iterator);
Defensive patterns

Strategy: validation

Validate before calling

# detect [...x.toArray()] and call(...x.toArray())
rg -n --type js -U '(?:\[|\()\s*\.\.\..*\.toArray\(\)' src/

Prevention

When it happens

Trigger: `[...iterator.toArray()]`, `[a, ...iterator.toArray()]`, `call(...iterator.toArray())`, `call(a, ...iterator.toArray())`, `new Foo(...iterator.toArray())`. The spread's parent must be an ArrayExpression, CallExpression, or NewExpression; object spread `{...iterator.toArray()}` is not flagged, nor are optional or computed `.toArray` access.

Common situations: Forwarding iterator output into variadic functions or literal construction, e.g. `log(...items.values().toArray())`, common after adopting iterator helpers. Hits projects with unicorn nursery rules enabled.

Related errors


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