oxc-project/oxc · warning · OxcDiagnostic

`yield*` can delegate to an iterable, `.toArray()` is unnece

Error message

`yield*` can delegate to an iterable, `.toArray()` is unnecessary.

What it means

Diagnostic from the oxlint rule `unicorn/no-useless-iterator-to-array` (nursery). This variant fires when a delegating `yield*` expression's argument is an `.toArray()` call. `yield*` delegates to any iterable, so converting the iterator to an array first is unnecessary. An autofix removes the `.toArray()` call.

Source

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

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

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

declare_oxc_lint!(

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Remove `.toArray()`: `yield* iterator.toArray();` -> `yield* iterator;`.
  2. Apply the rule's autofix.
  3. Keep `.toArray()` if downstream consumers of the yielded value need an actual array (for example when they call `.sort()` on it).
  4. Disable the rule for the line when the conversion is required by your runtime support matrix.

Example fix

// before
function* foo() {
  yield* iterator.toArray();
}

// after
function* foo() {
  yield* iterator;
}
Defensive patterns

Strategy: validation

Validate before calling

# detect yield* x.toArray()
rg -n --type js 'yield\s*\*\s*.*\.toArray\(\)' src/

Prevention

When it happens

Trigger: `function* foo() { yield* iterator.toArray(); }`, including chained producers like `yield* gen().take(5).toArray()`. Requires a delegating `yield*`; plain `yield iterator.toArray();` is not flagged, and optional forms (`?.toArray()`) are skipped.

Common situations: Generator pipelines (e.g. async task queues or streaming parsers) built on iterator helpers where an old array conversion survived a refactor. Appears when unicorn nursery rules are enabled in oxlint.

Related errors


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