{"record":{"id":"3a5d3e8603f839c4","repo":"oxc-project/oxc","slug":"iterator-has-a-method-method-toarray","errorCode":null,"errorMessage":"`Iterator` has a `.{method}()` method, `.toArray()` is unnecessary.","messagePattern":"`Iterator` has a `\\.(.+?)\\(\\)` method, `\\.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":42,"sourceCode":"    .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?\n    ///\n    /// [`Iterator.prototype.toArray()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator/toArray)\n    /// converts an iterator to an array. However, this conversion is unnecessary in many cases:\n    ///","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/no_useless_iterator_to_array.rs#L24-L60","documentation":"Diagnostic from the oxlint rule `unicorn/no-useless-iterator-to-array` (nursery). This variant fires when `.every()`, `.find()`, `.forEach()`, `.some()`, or `.reduce()` is called on the result of `.toArray()`. The `Iterator` prototype provides these methods directly, so the array conversion is unnecessary. It is reported as a suggestion, not an autofix, because Array callbacks receive an extra `array` argument that Iterator callbacks do not; the rule skips the report entirely when the callback declares that parameter or when a `thisArg` is passed.","triggerScenarios":"`iterator.toArray().every(fn)`, `.find(fn)`, `.forEach(fn)`, `.some(fn)`, `.reduce(fn, init)` (exactly 2 arguments for reduce, at most 1 for the others). Not fired when the callback uses the array parameter (`(v, i, array) => ...`), when a `thisArg` is passed (`every(fn, thisArg)`), or for `.filter()`/`.map()`/`.flatMap()` whose Iterator versions return iterators instead of arrays.","commonSituations":"Pipeline code such as `values().take(10).toArray().every(x => x > 0)` written by developers used to Array methods. Appears with unicorn nursery rules enabled; behavior-sensitive callbacks mean the fix needs human review.","solutions":["Call the method on the iterator: `iterator.toArray().every(fn)` -> `iterator.every(fn)`.","Before applying, verify the callback does not use the third (fourth for `reduce`) `array` argument.","If the callback needs the array argument or a `thisArg`, keep `.toArray()`; the rule will then stop reporting.","For `.filter()`/`.map()`/`.flatMap()` keep `.toArray()` if you need an array result."],"exampleFix":"// before\nconst ok = iterator.toArray().every(x => x > 0);\n\n// after\nconst ok = iterator.every(x => x > 0);","handlingStrategy":"validation","validationCode":"# detect toArray().every/find/forEach/some/reduce and callback arity\nrg -n --type js 'toArray\\(\\)\\.(?:every|find|forEach|some|reduce)\\(' src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check callback arity before rewriting: callbacks using the `array` parameter (3rd arg, 4th for reduce) must keep `.toArray()`.","Pass no `thisArg`; its presence keeps the array version required.","Prefer calling every/find/forEach/some/reduce directly on the iterator when callbacks take (value[, index])."],"tags":["lint","oxlint","unicorn","iterator-helpers","array-methods","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"}