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
- Remove `.toArray()`: `yield* iterator.toArray();` -> `yield* iterator;`.
- Apply the rule's autofix.
- Keep `.toArray()` if downstream consumers of the yielded value need an actual array (for example when they call `.sort()` on it).
- 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
- Delegate with `yield* iterable` directly in generators.
- Materialize arrays only when consumers need array semantics on the yielded value.
- Review generator code after switching producers from arrays to iterators.
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
- `{description}` accepts an iterable, `.toArray()` is unneces
- `for…of` can iterate over an iterable, `.toArray()` is unnec
- Spread works on iterables, `.toArray()` is unnecessary.
- `Iterator` has a `.{method}()` method, `.toArray()` is unnec
- Missing JSDoc `@yields` declaration for generator function.
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/a3d595b6357d7aca.
Report an issue: GitHub.