Skip to content

noUselessSetConstruction (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noUselessSetConstruction": "error"
}
}
}
}

Disallow creating a Set that a Set method such as union() doesn’t need.

The Set methods union(), intersection(), difference(), symmetricDifference(), isSubsetOf(), isSupersetOf(), and isDisjointFrom() combine or compare two collections without changing either of them. Copying a value into a new Set right before or right after calling one of these methods does extra work, and creates a Set that is thrown away right after.

The rule reports three kinds of unnecessary copies:

  • A copy of the argument, such as a.union(new Set(b)). These methods read a Set directly, so a.union(b) gives the same result. They also read a Map as the collection of its keys, so a.union(new Set(map.keys())) can be written as a.union(map).
  • A copy of the Set that the method is called on, such as new Set(a).union(b). The method doesn’t change a, so a.union(b) gives the same result.
  • A copy of the result, such as new Set(a.union(b)). union(), intersection(), difference(), and symmetricDifference() already return a new Set.

The rule only reports code when it can tell that the values are a Set or a Map. This is the case when a value is:

  • created with new Set() or new Map();
  • a variable or a function parameter with the type Set, ReadonlySet, Map, or ReadonlyMap;
  • a const variable that is initialized with a Set or a Map, and that doesn’t have one of these types;
  • marked as one of these types with as or satisfies;
  • a conditional expression (condition ? a : b) whose two results are both a Set, or both a Map.

When a const variable, or a value marked with as or satisfies, has another type, such as Iterable<string>, the rule looks at the value itself. For example, const values: Iterable<string> = new Set() is a Set.

A copy of the Set that the method is called on is kept when the argument contains a function call, an assignment, or another operation that can change the original Set. For example, new Set(a).union(update(a)) reads the values that a had before update(a) ran. Reading a property and using a tagged template, such as tag`text` , don’t count as such operations, although both can run code.

The rule ignores a new Set() with explicit type arguments, such as new Set<string>(values), because removing it can change the types of the code. For the same reason, it ignores a copy of the argument, and a copy of the Set that the method is called on, when as or satisfies is applied to the new Set(), as in a.union(new Set(b) as Set<string>).

const selected = new Set([1, 2]);
const other = new Set([2, 3]);
selected.union(new Set(other));
code-block.js:3:16 lint/nursery/noUselessSetConstruction  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This Set copy isn't needed to use union().

1 │ const selected = new Set([1, 2]);
2 │ const other = new Set([2, 3]);
> 3 │ selected.union(new Set(other));
│ ^^^^^^^^^^^^^^
4 │

ℹ The method reads a Set, or the keys of a Map, directly and doesn't change them. Creating another Set only takes extra time and memory.

ℹ Pass the original Set or Map to the method instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Remove the unnecessary copy.

3 │ selected.union(new·Set(other));
│ -------- -
const selected = new Set([1, 2]);
const records = new Map([[2, "value"]]);
selected.intersection(new Set(records.keys()));
code-block.js:3:23 lint/nursery/noUselessSetConstruction  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This Set copy isn't needed to use intersection().

1 │ const selected = new Set([1, 2]);
2 │ const records = new Map([[2, "value"]]);
> 3 │ selected.intersection(new Set(records.keys()));
│ ^^^^^^^^^^^^^^^^^^^^^^^
4 │

ℹ The method reads a Set, or the keys of a Map, directly and doesn't change them. Creating another Set only takes extra time and memory.

ℹ Pass the original Set or Map to the method instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Remove the unnecessary copy.

1 1 │ const selected = new Set([1, 2]);
2 2 │ const records = new Map([[2, "value"]]);
3 │ - selected.intersection(new·Set(records.keys()));
3 │ + selected.intersection(records);
4 4 │

const selected = new Set([1, 2]);
const other = new Set([2, 3]);
new Set(selected).isSubsetOf(other);
code-block.js:3:1 lint/nursery/noUselessSetConstruction  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This Set copy isn't needed to use isSubsetOf().

1 │ const selected = new Set([1, 2]);
2 │ const other = new Set([2, 3]);
> 3 │ new Set(selected).isSubsetOf(other);
│ ^^^^^^^^^^^^^^^^^
4 │

ℹ The method doesn't change the Set it is called on. Copying the Set first only takes extra time and memory.

ℹ Call the method on the original Set instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Remove the unnecessary copy.

3 │ new·Set(selected).isSubsetOf(other);
│ -------- -
const selected = new Set([1, 2]);
const other = new Set([2, 3]);
const combined = new Set(selected.union(other));
code-block.js:3:18 lint/nursery/noUselessSetConstruction  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This Set copy isn't needed to use union().

1 │ const selected = new Set([1, 2]);
2 │ const other = new Set([2, 3]);
> 3 │ const combined = new Set(selected.union(other));
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4 │

ℹ The method already returns a new Set. Copying it only takes extra time and memory.

ℹ Use the Set that the method returns instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Remove the unnecessary copy.

3 │ const·combined·=·new·Set(selected.union(other));
│ -------- -
function overlap(selected: Set<number>, other: ReadonlySet<number>) {
return selected.intersection(new Set(other));
}
code-block.ts:2:34 lint/nursery/noUselessSetConstruction  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This Set copy isn't needed to use intersection().

1 │ function overlap(selected: Set<number>, other: ReadonlySet<number>) {
> 2 │ return selected.intersection(new Set(other));
│ ^^^^^^^^^^^^^^
3 │ }
4 │

ℹ The method reads a Set, or the keys of a Map, directly and doesn't change them. Creating another Set only takes extra time and memory.

ℹ Pass the original Set or Map to the method instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Remove the unnecessary copy.

2 │ ····return·selected.intersection(new·Set(other));
│ -------- -
const selected = new Set([1, 2]);
const other = new Set([2, 3]);
const records = new Map([[2, "value"]]);
selected.union(other);
selected.intersection(records);
selected.isSubsetOf(other);
const combined = selected.union(other);
// A copy that isn't used with one of these methods is allowed.
const copy = new Set(selected);
// The values of a `Map` are different from its keys.
selected.intersection(new Set(records.values()));
// The rule can't tell whether `items` is a `Set`.
selected.union(new Set(items));
// `isSubsetOf()` returns a boolean, not a `Set`.
const flags = new Set(selected.isSubsetOf(other));