Skip to content

noErasingOperation (JavaScript)

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

Disallow operations with zero that always result in zero.

Multiplying a number by 0, combining it with 0 using the bitwise AND operator (&), or dividing 0 by a number gives 0 no matter what the other number is. The other number has no effect on the result, so code like x * 0 is most likely a mistake, such as a leftover from debugging or a typo. If the result should be zero, write 0 directly.

A few special values produce a different result. For example, NaN * 0 and Infinity * 0 are both NaN, and -5 * 0 is -0.

The rule only reports a 0 that is written as a number, such as 0 or 0.0. It doesn’t report variables that contain zero, 0n (a BigInt zero), or 0 / 0, which results in NaN instead of zero.

const total = price * 0;
code-block.js:1:15 lint/nursery/noErasingOperation ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ This operation always results in zero.

> 1 │ const total = price * 0;
│ ^^^^^^^^^
2 │

ℹ Multiplying by zero gives zero, so the other value has no effect on the result.

ℹ This is likely a mistake. If the result should be zero, replace the operation with 0.

ℹ 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 flags = options & 0;
code-block.js:1:15 lint/nursery/noErasingOperation ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ This operation always results in zero.

> 1 │ const flags = options & 0;
│ ^^^^^^^^^^^
2 │

ℹ A bitwise AND with zero gives zero, so the other value has no effect on the result.

ℹ This is likely a mistake. If the result should be zero, replace the operation with 0.

ℹ 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 ratio = 0 / count;
code-block.js:1:15 lint/nursery/noErasingOperation ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ This operation always results in zero.

> 1 │ const ratio = 0 / count;
│ ^^^^^^^^^
2 │

ℹ Dividing zero by a number gives zero, so the other value has no effect on the result.

ℹ This is likely a mistake. If the result should be zero, replace the operation with 0.

ℹ 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 total = price * quantity;
const flags = options | 0;
const ratio = count / 0;
const zero = 0;