Skip to content

unicorn/no-useless-spread Correctness

This rule is turned on by default.
⚠️🛠️️ A dangerous auto-fix is available for this rule.

What it does

Disallows using spread syntax in following, unnecessary cases:

  • Spread an array literal as elements of an array literal
  • Spread an array literal as arguments of a call or a new call
  • Spread an object literal as properties of an object literal
  • Use spread syntax to clone an array created inline

Why is this bad?

The following builtins accept an iterable, so it's unnecessary to convert the iterable to an array:

  • Map constructor
  • WeakMap constructor
  • Set constructor
  • WeakSet constructor
  • TypedArray constructor
  • Array.from(…)
  • TypedArray.from(…)
  • Promise.{all,allSettled,any,race}(…)
  • Object.fromEntries(…)

The for…of loop can iterate over any iterable object not just array, so it's unnecessary to convert the iterable to an array.

The yield* can delegate to another iterable, so it's unnecessary to convert the iterable to an array.

Examples

Examples of incorrect code for this rule:

javascript
const array = [firstElement, ...[secondElement], thirdElement];

await Promise.all([...iterable]);

for (const foo of [...set]);

function* foo() {
  yield* [...anotherGenerator()];
}

function foo(bar) {
  return [...bar.map((x) => x * 2)];
}

Examples of correct code for this rule:

javascript
const array = [firstElement, secondElement, thirdElement];

await Promise.all(iterable);

for (const foo of set);

function* foo() {
  yield* anotherGenerator();
}

function foo(bar) {
  return bar.map((x) => x * 2);
}

How to use

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny unicorn/no-useless-spread
json
{
  "rules": {
    "unicorn/no-useless-spread": "error"
  }
}

References

Released under the MIT License.