{"record":{"id":"278ab3e57e6bdf02","repo":"oxc-project/oxc","slug":"using-a-spread-operator-here-creates-a-new-arr-or","errorCode":null,"errorMessage":"Using a spread operator here creates a new {arr_or_obj} unnecessarily.","messagePattern":"Using a spread operator here creates a new (.+?) unnecessarily\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/no_useless_spread/mod.rs","lineNumber":29,"sourceCode":"use oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, SPAN, Span};\n\nmod const_eval;\n\nuse const_eval::{ConstEval, is_array_from, is_new_typed_array};\n\nuse crate::{\n    AstNode,\n    ast_util::{\n        get_new_expr_ident_name, is_method_call, is_new_expression, outermost_paren_parent,\n    },\n    context::LintContext,\n    fixer::{RuleFix, RuleFixer},\n    rule::Rule,\n};\n\nfn spread_in_list(span: Span, arr_or_obj: &str) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\n        \"Using a spread operator here creates a new {arr_or_obj} unnecessarily.\"\n    ))\n    .with_help(\"Consider removing the spread operator.\")\n    .with_label(span)\n}\n\nfn spread_in_arguments(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Using a spread operator here creates a new array unnecessarily.\")\n        .with_help(\"Pass arguments directly instead of spreading an array.\")\n        .with_label(span)\n}\n\nfn iterable_to_array(span: Span, ctor_name: &str) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\n        \"`{ctor_name}` accepts an iterable, so it's unnecessary to convert the iterable to an array.\"\n    ))\n    .with_help(\"Consider removing the spread operator.\")\n    .with_label(span)","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/oxc-project/oxc/blob/a3d33dda7cb69da23db4fcaa2c0c05de61e760a1/crates/oxc_linter/src/rules/unicorn/no_useless_spread/mod.rs#L11-L47","documentation":"Diagnostic from the oxlint rule `unicorn/no-useless-spread`. This variant (`spread_in_list`) fires when an array literal is spread directly into another array literal (`[...[a, b]]`) or an object literal into another object literal (`{...{a}}`). The inner literal is already the exact value being built, so the spread creates a needless copy. The message fills `{arr_or_obj}` with 'array' or 'object'; an autofix inlines the inner elements.","triggerScenarios":"`const array = [...[a]]`, `[a, ...[a, b], b]`, `[...[...[1,2,3]]]`, `const object = {...{a}}`, `{a, ...{a, b}, b}`, `({...{a:1}, ...{a: 2}})`, including trailing commas and parenthesized inner literals. Not fired for spreading identifiers (`[...a]`) or rest elements in destructuring.","commonSituations":"Copy-paste composition, refactoring leftovers where a literal replaced a variable, and conditional spreads that later became unconditional (`[...(cond ? [1] : [2])]` is flagged as a clone). Appears with the oxlint correctness category or unicorn plugin.","solutions":["Inline the inner literal's elements: `[first, ...[second], third]` -> `[first, second, third]`; `{...{a, b}}` -> `{a, b}`.","Apply the rule's autofix, which merges literals (including multiple all-spread arrays into one array).","If the inner literal comes from an unresolved expression, the rule reports without a fix; restructure manually.","Disable inline for intentional double-copy corner cases."],"exampleFix":"// before\nconst array = [first, ...[second], third];\nconst object = { a, ...{ b, c } };\n\n// after\nconst array = [first, second, third];\nconst object = { a, b, c };","handlingStrategy":"validation","validationCode":"# detect literal spread into same-shape literal\nrg -n --type js '(?:\\[\\.\\.\\.\\[|\\{\\.\\.\\.\\{)' src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Compose literals by writing elements inline, not by spreading literals.","When a spread source becomes constant, collapse it manually in the same commit.","Run `oxlint --fix` regularly; the merge fixer is safe for this variant."],"tags":["lint","oxlint","unicorn","spread","array-literal","object-literal","correctness"],"backgroundTag":"useless-spread","analyzedSha":"a3d33dda7cb69da23db4fcaa2c0c05de61e760a1","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}