{"record":{"id":"a3d595b6357d7aca","repo":"oxc-project/oxc","slug":"yield-can-delegate-to-an-iterable-toarray","errorCode":null,"errorMessage":"`yield*` can delegate to an iterable, `.toArray()` is unnecessary.","messagePattern":"`yield\\*` can delegate to an iterable, `\\.toArray\\(\\)` is unnecessary\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/no_useless_iterator_to_array.rs","lineNumber":33,"sourceCode":"    context::LintContext,\n    fixer::{RuleFix, RuleFixer},\n    rule::Rule,\n};\n\nfn iterable_accepting_diagnostic(span: Span, description: &str) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\n        \"`{description}` accepts an iterable, `.toArray()` is unnecessary.\"\n    ))\n    .with_label(span)\n}\n\nfn for_of_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"`for…of` can iterate over an iterable, `.toArray()` is unnecessary.\")\n        .with_label(span)\n}\n\nfn yield_star_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"`yield*` can delegate to an iterable, `.toArray()` is unnecessary.\")\n        .with_label(span)\n}\n\nfn spread_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Spread works on iterables, `.toArray()` is unnecessary.\").with_label(span)\n}\n\nfn iterator_method_diagnostic(span: Span, method: &str) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\n        \"`Iterator` has a `.{method}()` method, `.toArray()` is unnecessary.\"\n    ))\n    .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct NoUselessIteratorToArray;\n\ndeclare_oxc_lint!(","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/no_useless_iterator_to_array.rs#L15-L51","documentation":"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.","triggerScenarios":"`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.","commonSituations":"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.","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."],"exampleFix":"// before\nfunction* foo() {\n  yield* iterator.toArray();\n}\n\n// after\nfunction* foo() {\n  yield* iterator;\n}","handlingStrategy":"validation","validationCode":"# detect yield* x.toArray()\nrg -n --type js 'yield\\s*\\*\\s*.*\\.toArray\\(\\)' src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["lint","oxlint","unicorn","generator","yield","iterator-helpers","nursery"],"backgroundTag":"iterator-to-array","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}