# unicorn/no-useless-spread

> Disallow spreads that only build an array or object to take it apart or copy it again

A spread that changes nothing costs an extra array or object and makes the line harder to read. The rule reports three kinds.

- A literal spread into a literal or a call: `[...[a, b], c]`, `{ ...{ a }, b }`, `save(...[a, b])`. Write the elements directly.
- An iterable turned into an array for something that takes any iterable: `for...of`, `yield*`, `new Set()`, `new Map()`, typed array constructors, `Array.from()`, `Object.fromEntries()`, `Promise.all()`, `Promise.allSettled()`, `Promise.any()`, `Promise.race()`.
- A copy of a value that's already new: `[...items.map(fn)]`, `[...text.split(",")]`, `[...Object.keys(user)]`, `{ ...Object.fromEntries(entries) }`.

`--fix` removes the spread in most cases. It doesn't touch `for...of` and `yield*`.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"unicorn/no-useless-spread" = "error" # or "warn"
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 3} */}
```js
const colors = [...["red", "green"], extra];
const config = { ...{ debug: false }, ...overrides };
log(...["saved", file]);
```

{/* bad {"count": 2} */}
```js
const unique = new Set([...names]);

for (const entry of [...map.entries()]) {
  visit(entry);
}
```

{/* bad {"count": 2} */}
```js
const upper = [...names.map(name => name.toUpperCase())];
const results = [...(await Promise.all(jobs))];
```

Not reported:

{/* good */}
```js
const colors = ["red", "green", extra];
const config = { debug: false, ...overrides };
log("saved", file);

const unique = new Set(names);

for (const entry of map.entries()) {
  visit(entry);
}

const upper = names.map(name => name.toUpperCase());
const results = await Promise.all(jobs);
```

Copying a variable is a real copy, and so is turning an iterable into an array to keep it:

{/* good */}
```js
const copy = [...names];
const clone = { ...user };
const keys = [...map.keys()];
```
