Skip to content

noReturnArrayPush (JavaScript)

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

Disallow using the return value of Array.prototype.push() and Array.prototype.unshift().

push() and unshift() add items to an array and return the new length of the array. They don’t return the added item or the array itself. Returning or storing this length is almost always a mistake, because the code reads as if the result were meaningful when it says nothing about what was added.

To add an item and stop the function, call push() or unshift() before return. If you need the new length, read the length property of the array after adding the item.

The rule only reports calls with at least one argument. It ignores calls whose result is discarded with the void operator, such as void items.push(item), and calls whose result is accessed with . or [], such as router.push(to).catch(handleError), because this usually means push() is a custom method that returns something other than a length.

The rule also ignores push() calls on objects that are commonly not arrays: stream, router, this, this.stream, this.router, this.$router, process.stdin, process.stdout, and process.stderr. It ignores push() and unshift() calls on values that are written in a way that shows they aren’t arrays: objects ({}), functions, classes, template strings, and new expressions other than new Array().

function add(item) {
return items.push(item);
}
code-block.js:2:18 lint/nursery/noReturnArrayPush  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ The return value of push() is used here.

1 │ function add(item) {
> 2 │ return items.push(item);
│ ^^^^
3 │ }
4 │

ℹ push() returns the new length of the array, not the added item or the array. Using this length is usually a mistake.

ℹ Call push() as its own statement. If you need the new length, read the length property of the array afterward.

ℹ 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: Move the call before the return statement.

1 1 │ function add(item) {
2 │ - ····return·items.push(item);
2 │ + ····items.push(item);·return;
3 3 │ }
4 4 │

const add = (item) => items.push(item);
code-block.js:1:29 lint/nursery/noReturnArrayPush ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ The return value of push() is used here.

> 1 │ const add = (item) => items.push(item);
│ ^^^^
2 │

ℹ push() returns the new length of the array, not the added item or the array. Using this length is usually a mistake.

ℹ Call push() as its own statement. If you need the new length, read the length property of the array afterward.

ℹ 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.

const length = items.unshift(item);
code-block.js:1:22 lint/nursery/noReturnArrayPush ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ The return value of unshift() is used here.

> 1 │ const length = items.unshift(item);
│ ^^^^^^^
2 │

ℹ unshift() returns the new length of the array, not the added item or the array. Using this length is usually a mistake.

ℹ Call unshift() as its own statement. If you need the new length, read the length property of the array afterward.

ℹ 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.

An arrow function without braces returns its body, even when the caller ignores the result:

source.forEach((item) => target.push(item));
code-block.js:1:33 lint/nursery/noReturnArrayPush ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ The return value of push() is used here.

> 1 │ source.forEach((item) => target.push(item));
│ ^^^^
2 │

ℹ push() returns the new length of the array, not the added item or the array. Using this length is usually a mistake.

ℹ Call push() as its own statement. If you need the new length, read the length property of the array afterward.

ℹ 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.

function add(item) {
items.push(item);
return;
}
const addItem = (item) => {
items.push(item);
};
items.unshift(item);
const length = items.length;
function getNextLength(item) {
items.push(item);
return items.length;
}
target.push(...source);
void items.push(item);