oxc-project/oxc · warning · OxcDiagnostic

Using a spread operator here creates a new {arr_or_obj} unne

Error message

Using a spread operator here creates a new {arr_or_obj} unnecessarily.

What it means

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.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/no_useless_spread/mod.rs:29

use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, SPAN, Span};

mod const_eval;

use const_eval::{ConstEval, is_array_from, is_new_typed_array};

use crate::{
    AstNode,
    ast_util::{
        get_new_expr_ident_name, is_method_call, is_new_expression, outermost_paren_parent,
    },
    context::LintContext,
    fixer::{RuleFix, RuleFixer},
    rule::Rule,
};

fn spread_in_list(span: Span, arr_or_obj: &str) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!(
        "Using a spread operator here creates a new {arr_or_obj} unnecessarily."
    ))
    .with_help("Consider removing the spread operator.")
    .with_label(span)
}

fn spread_in_arguments(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Using a spread operator here creates a new array unnecessarily.")
        .with_help("Pass arguments directly instead of spreading an array.")
        .with_label(span)
}

fn iterable_to_array(span: Span, ctor_name: &str) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!(
        "`{ctor_name}` accepts an iterable, so it's unnecessary to convert the iterable to an array."
    ))
    .with_help("Consider removing the spread operator.")
    .with_label(span)

View on GitHub (pinned to a3d33dda7c)

Solutions

  1. Inline the inner literal's elements: `[first, ...[second], third]` -> `[first, second, third]`; `{...{a, b}}` -> `{a, b}`.
  2. Apply the rule's autofix, which merges literals (including multiple all-spread arrays into one array).
  3. If the inner literal comes from an unresolved expression, the rule reports without a fix; restructure manually.
  4. Disable inline for intentional double-copy corner cases.

Example fix

// before
const array = [first, ...[second], third];
const object = { a, ...{ b, c } };

// after
const array = [first, second, third];
const object = { a, b, c };
Defensive patterns

Strategy: validation

Validate before calling

# detect literal spread into same-shape literal
rg -n --type js '(?:\[\.\.\.\[|\{\.\.\.\{)' src/

Prevention

When it happens

Trigger: `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.

Common situations: 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.

Related errors


AI-assisted analysis of oxc-project/oxc@a3d33dda7c (2026-08-20). Data as JSON: /api/errors/278ab3e57e6bdf02. Report an issue: GitHub.