{"record":{"id":"d8385063835aef9b","repo":"oxc-project/oxc","slug":"spread-works-on-iterables-toarray-is-unneces","errorCode":null,"errorMessage":"Spread works on iterables, `.toArray()` is unnecessary.","messagePattern":"Spread works on iterables, `\\.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":38,"sourceCode":"fn 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!(\n    /// ### What it does\n    ///\n    /// Disallow unnecessary `.toArray()` on iterators.\n    ///\n    /// ### Why is this bad?","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/no_useless_iterator_to_array.rs#L20-L56","documentation":"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.","triggerScenarios":"`[...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.","commonSituations":"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.","solutions":["Spread the iterator itself: `[...iterator.toArray()]` -> `[...iterator]`, `call(...iterator.toArray())` -> `call(...iterator)`.","Apply the rule's autofix.","Keep `.toArray()` when you need the array twice or mutate it afterwards; the rule only flags the single-use spread sites.","Disable the rule inline for runtimes without iterator-helper support."],"exampleFix":"// before\nconst items = [...iterator.toArray()];\nrun(...iterator.toArray());\n\n// after\nconst items = [...iterator];\nrun(...iterator);","handlingStrategy":"validation","validationCode":"# detect [...x.toArray()] and call(...x.toArray())\nrg -n --type js -U '(?:\\[|\\()\\s*\\.\\.\\..*\\.toArray\\(\\)' src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Spread iterables directly (`[...iterator]`, `fn(...iterator)`).","Avoid double conversion when chaining iterator helpers; the spread is already the boundary.","Run oxlint with nursery unicorn rules in CI once your runtime baseline supports iterator helpers."],"tags":["lint","oxlint","unicorn","spread","iterator-helpers","performance","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-14T00:17:10.932Z"}