oxc-project/oxc · warning · OxcDiagnostic

`{ctor_name}` accepts an iterable, so it's unnecessary to co

Error message

`{ctor_name}` accepts an iterable, so it's unnecessary to convert the iterable to an array.

What it means

Diagnostic from the oxlint rule `unicorn/no-useless-spread`. This variant (`iterable_to_array`) fires when a single spread of an iterable is wrapped in an array literal just to feed a consumer that accepts iterables natively: `new Set/Map/WeakSet/WeakMap([...])`, typed-array constructors, `Promise.all/allSettled/any/race([...])`, `Array.from([...])`, `TypedArray.from([...])`, and `Object.fromEntries([...])`. The `{ctor_name}` placeholder carries the consumer, e.g. `new Map(…)` or `Promise.all(…)`. An autofix removes the array wrapper and spread.

Source

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

    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)
}

fn iterable_to_array_in_for_of(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Using a spread operator here creates a new array unnecessarily.")
        .with_help("`for…of` can iterate over iterable, it's unnecessary to convert to an array.")
        .with_label(span)
}

fn iterable_to_array_in_yield_star(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Using a spread operator here creates a new array unnecessarily.")
        .with_help("`yield*` can delegate to another iterable, so it's unnecessary to convert the iterable to an array.")
        .with_label(span)
}

View on GitHub (pinned to a3d33dda7c)

Solutions

  1. Pass the iterable directly: `new Set([...iterable])` -> `new Set(iterable)`, `Promise.all([...items])` -> `Promise.all(items)`.
  2. Apply the rule's autofix.
  3. Keep the array wrapper only when you mix in extra elements (`[...iterable, extra]` is not flagged).
  4. For non-iterable sources (plain objects), keep the current code; the rule only skips what it can prove.

Example fix

// before
const set = new Set([...iterable]);
const results = await Promise.all([...tasks]);

// after
const set = new Set(iterable);
const results = await Promise.all(tasks);
Defensive patterns

Strategy: validation

Validate before calling

# detect iterable-accepting consumers receiving [...x]
rg -n --type js -U '(?:new (?:Weak)?(?:Set|Map)|(?:Int8|Uint8|Float64|BigUint64)Array|Promise\.(?:all|allSettled|any|race)|Array\.from|Object\.fromEntries)\(\[\.\.\.' src/

Prevention

When it happens

Trigger: `new Map([...iterable])`, `new Set([...iterable])`, `new WeakMap([...iterable])`, `new BigUint64Array([...iterable], byteOffset, length)`, `Promise.all([...iterable])`, `Promise.allSettled([...iterable])`, `Promise.any([...iterable])`, `Promise.race([...iterable])`, `Array.from([...iterable])`, `Uint8Array.from([...iterable])`, `Object.fromEntries([...iterable])`, including trailing commas. Not fired for namespaced callees (`new foo.Map(...)`), computed members (`Promise[all](...)`), or extra/mixed arguments like `new Map([...iterable], extraArgument)`.

Common situations: Wrapping Set/Map contents before construction out of array-era habit, and `Promise.all([...set])` patterns. Appears when the oxlint correctness category or unicorn plugin is enabled.

Related errors


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