{"record":{"id":"a585fef3b39a33eb","repo":"oxc-project/oxc","slug":"do-not-spread-accumulators-in-array-prototype-redu","errorCode":null,"errorMessage":"Do not spread accumulators in Array.prototype.reduce()","messagePattern":"Do not spread accumulators in Array\\.prototype\\.reduce\\(\\)","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/oxc/no_accumulating_spread.rs","lineNumber":21,"sourceCode":"    ast::{\n        Argument, AssignmentExpression, AssignmentTarget, BindingPattern, CallExpression,\n        Expression, ForInStatement, ForOfStatement, ForStatement, VariableDeclarationKind,\n    },\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_semantic::{NodeId, SymbolId};\nuse oxc_span::{GetSpan, Span};\n\nuse crate::{\n    AstNode,\n    ast_util::{call_expr_method_callee_info, is_method_call},\n    context::LintContext,\n    rule::Rule,\n};\n\nfn reduce_likely_array_spread_diagnostic(spread_span: Span, reduce_span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Do not spread accumulators in Array.prototype.reduce()\")\n        .with_help(\"It looks like you're spreading an `Array`. Consider using the `Array.push` or `Array.concat` methods to mutate the accumulator instead.\")\n        .with_note(\"Using spreads within accumulators leads to `O(n^2)` time complexity.\")\n        .with_labels([\n            spread_span.label(\"From this spread\"),\n            reduce_span.label(\"For this reduce\")\n        ])\n}\n\nfn reduce_likely_object_spread_diagnostic(spread_span: Span, reduce_span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Do not spread accumulators in Array.prototype.reduce()\")\n        .with_help(\"It looks like you're spreading an `Object`. Consider using the `Object.assign` or assignment operators to mutate the accumulator instead.\")\n        .with_note(\"Using spreads within accumulators leads to `O(n^2)` time complexity.\")\n        .with_labels([\n            spread_span.label(\"From this spread\"),\n            reduce_span.label(\"For this reduce\")\n        ])\n}\n","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/oxc/no_accumulating_spread.rs#L3-L39","documentation":"The array variant of oxlint's `oxc/no-accumulating-spread` (perf category): a spread element copies the accumulator — the first parameter of a two-parameter callback — inside a `.reduce()`/`.reduceRight()` call (invoked with 1-2 arguments). Each iteration clones the whole array (`[...acc, x]`), degrading the reduce to O(n^2) time and memory; the help recommends `Array.push`/`Array.concat`, and the note states the quadratic complexity explicitly.","triggerScenarios":"`arr.reduce((acc, x) => [...acc, fn(x)], [])`; `items.reduceRight((acc, x) => [...acc, transform(x)], [])` — any array spread of the accumulator identifier inside a reduce callback.","commonSituations":"Functional-style accumulation written with spread because mutation feels wrong; React/Redux reducer idioms copied into hot loops; large datasets where the quadratic blowup causes timeouts or memory pressure.","solutions":["Mutate the accumulator and return it: `arr.reduce((acc, x) => { acc.push(fn(x)); return acc; }, [])`","Or replace the reduce with a plain `for...of` loop pushing into an array","Benchmark with realistic large inputs to confirm the fix"],"exampleFix":"// before\nconst out = arr.reduce((acc, x) => [...acc, fn(x)], []);\n\n// after\nconst out = arr.reduce((acc, x) => { acc.push(fn(x)); return acc; }, []);","handlingStrategy":"validation","validationCode":"// .oxlintrc.json — perf rule\n{\n  \"rules\": { \"oxc/no-accumulating-spread\": \"warn\" }\n}\n// CLI: npx oxlint src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never spread the reduce accumulator; mutate it and return it","Benchmark reduce pipelines with large arrays (100k+ items) before shipping","Use push/concat for array accumulation in hot paths"],"tags":["oxlint","oxc","performance","reduce","javascript","static-analysis"],"backgroundTag":"reduce-accumulator-spread","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"}