{"record":{"id":"4167692b1a684a69","repo":"oxc-project/oxc","slug":"description-accepts-an-iterable-toarray","errorCode":null,"errorMessage":"`{description}` accepts an iterable, `.toArray()` is unnecessary.","messagePattern":"`(.+?)` accepts 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":21,"sourceCode":"    ast::{\n        CallExpression, Expression, ForOfStatement, FormalParameters, NewExpression, SpreadElement,\n        YieldExpression,\n    },\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse 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}","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/no_useless_iterator_to_array.rs#L3-L39","documentation":"Diagnostic from the oxlint rule `unicorn/no-useless-iterator-to-array` (nursery). This variant fires when `Iterator.prototype.toArray()` (ES2025 iterator helpers) feeds a consumer that already accepts any iterable: the `Set`/`Map`/`WeakSet`/`WeakMap` constructors, typed-array constructors, `Array.from`, `TypedArray.from`, `Object.fromEntries`, and `Promise.all`/`allSettled`/`any`/`race`. The `{description}` placeholder is filled with the consumer, e.g. `new Set(…)` or `Array.from(…)`. Constructor and `.from` cases get an autofix; the Promise cases are reported as suggestions because removing `.toArray()` can turn a synchronous throw into an async rejection.","triggerScenarios":"`new Set(iterator.toArray())`, `new Map(iterator.toArray())`, `new Int8Array(iterator.toArray())`, `Array.from(iterator.toArray())`, `Uint8Array.from(iterator.toArray())`, `Object.fromEntries(iterator.toArray())`, `Promise.all(iterator.toArray())`. Skipped for optional forms (`iterator?.toArray()`, `.toArray?.()`), computed access `iterator['toArray']()`, calls with arguments `iterator.toArray(true)`, and namespaced callees like `new foo.Set(...)`.","commonSituations":"Codebases on Node 22+ or TS 5.6+ that adopted iterator helpers (`.take()`, `.map()`, `.drop()`) and convert to arrays out of habit before handing off to builtins. Appears when enabling unicorn nursery rules or after upgrading oxlint to a version that ships this rule.","solutions":["Remove the `.toArray()` call and pass the iterator straight through: `new Set(iterator.toArray())` -> `new Set(iterator)`.","For Promise combinators, apply the suggestion and be aware iteration errors now reject the promise instead of throwing synchronously.","Keep `.toArray()` when you afterwards need array-only APIs (`.sort()`, `.at()`, indexing) - the rule deliberately does not flag those.","On runtimes without iterator-helper support, keep the conversion and disable the rule."],"exampleFix":"// before\nconst set = new Set(iterator.toArray());\n\n// after\nconst set = new Set(iterator);","handlingStrategy":"validation","validationCode":"# detect toArray() feeding iterable-accepting consumers\nrg -n --type js '(?:new (?:Weak)?(?:Set|Map)|(?:Int8|Uint8|Uint8Clamped|Int16|Uint16|Int32|Uint32|Float16|Float32|Float64|BigInt64|BigUint64)Array|Array\\.from|Object\\.fromEntries|Promise\\.(?:all|allSettled|any|race))\\([^)]*toArray\\(\\)' src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Internalize that Set/Map constructors, Array.from, fromEntries, and Promise combinators take iterables.","End iterator chains at the consumer, not at toArray().","Confirm iterator-helper support (Node 22+, TS 5.6+) before enabling the nursery rule fleet-wide."],"tags":["lint","oxlint","unicorn","iterator-helpers","iterable","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-14T05:17:10.506Z"}