{"record":{"id":"f5b3aaa3701d1bed","repo":"oxc-project/oxc","slug":"for-of-can-iterate-over-an-iterable-toarray","errorCode":null,"errorMessage":"`for…of` can iterate over an iterable, `.toArray()` is unnecessary.","messagePattern":"`for…of` can iterate over 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":28,"sourceCode":"use oxc_span::{GetSpan, Span};\n\nuse crate::{\n    AstNode,\n    ast_util::{is_method_call, is_new_expression},\n    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}","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/no_useless_iterator_to_array.rs#L10-L46","documentation":"Diagnostic from the oxlint rule `unicorn/no-useless-iterator-to-array` (nursery). This variant fires when the right side of a `for…of` (or `for await…of`) loop is an `.toArray()` call. `for…of` iterates over any iterable directly, so materializing an array first is wasted work and memory. An autofix deletes the `.toArray()` member call.","triggerScenarios":"`for (const x of iterator.toArray());`, `for (const x of foo.bar().toArray());`, `for await (const x of iterator.toArray())`, including parenthesized forms `for (const x of (iterator.toArray()));`. Not fired for optional `iterator?.toArray()` or `iterator.toArray?.()`.","commonSituations":"Loops written before an API switched from returning arrays to returning iterators (e.g. `values().take(10)`), where the old `.toArray()` was left in place. Shows up when enabling unicorn nursery rules in oxlint.","solutions":["Delete `.toArray()` from the loop head: `for (const x of iterator.toArray())` -> `for (const x of iterator)`.","Apply the rule's autofix (`oxlint --fix` or editor quick fix).","Keep `.toArray()` only if the loop body mutates the collection while iterating or needs array APIs; otherwise remove it.","Disable the rule inline if your target runtime lacks iterator helpers."],"exampleFix":"// before\nfor (const item of iterator.toArray()) {\n  handle(item);\n}\n\n// after\nfor (const item of iterator) {\n  handle(item);\n}","handlingStrategy":"validation","validationCode":"# detect for/for-await ... of <expr>.toArray()\nrg -n --type js -U 'for\\s*(?:await\\s*)?\\(\\s*(?:const|let|var)\\s+.*\\s+of\\s+.*\\.toArray\\(\\)' src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat for…of as iterable-native; never pre-convert iterators to arrays for loops.","Keep `.toArray()` only when the loop body needs array mutation APIs.","Enable the rule in CI so leftover conversions surface immediately after refactors."],"tags":["lint","oxlint","unicorn","for-of","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"}