Skip to content

noUselessLengthCheck (JavaScript)

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

Disallow checking the length of an array when the result doesn’t depend on it.

Some code checks whether an array is empty even though the code next to it already handles empty arrays:

  • some() returns false for an empty array, so array.length > 0 && array.some(callback) gives the same result as array.some(callback).
  • every() returns true for an empty array, so array.length === 0 || array.every(callback) gives the same result as array.every(callback).
  • A for...of loop doesn’t run its body for an empty array, so an if statement that only checks that the array isn’t empty before the loop is unnecessary.

Removing the check makes the code shorter and easier to read.

The rule only reports checks written as .length === 0, .length !== 0, or .length > 0. It reports a check next to some() or every() only when both are joined by the same && or || operator.

The rule assumes that the value is an array. The fix is unsafe because the value might be another kind of object with its own length, some(), or every(). It’s also unsafe because removing a check written before the call runs the call’s arguments even when the array is empty, and removing a check written after the call ignores a callback that changes the array.

if (array.length === 0 || array.every(Boolean));
code-block.js:1:5 lint/nursery/noUselessLengthCheck  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This check for an empty array is unnecessary.

> 1 │ if (array.length === 0 || array.every(Boolean));
│ ^^^^^^^^^^^^^^^^^^
2 │

ℹ every() returns true for an empty array, so the result is the same without the check.

ℹ Remove the length check.

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

1 │ if·(array.length·===·0·||·array.every(Boolean));
│ ----------------------
if (array.length !== 0 && array.some(Boolean));
code-block.js:1:5 lint/nursery/noUselessLengthCheck  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This check for a non-empty array is unnecessary.

> 1 │ if (array.length !== 0 && array.some(Boolean));
│ ^^^^^^^^^^^^^^^^^^
2 │

ℹ some() returns false for an empty array, so the result is the same without the check.

ℹ Remove the length check.

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

1 │ if·(array.length·!==·0·&&·array.some(Boolean));
│ ----------------------
if (array.length > 0 && array.some(Boolean));
code-block.js:1:5 lint/nursery/noUselessLengthCheck  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This check for a non-empty array is unnecessary.

> 1 │ if (array.length > 0 && array.some(Boolean));
│ ^^^^^^^^^^^^^^^^
2 │

ℹ some() returns false for an empty array, so the result is the same without the check.

ℹ Remove the length check.

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

1 │ if·(array.length·>·0·&&·array.some(Boolean));
│ --------------------
const isAllTruthyOrEmpty = array.length === 0 || array.every(Boolean);
code-block.js:1:28 lint/nursery/noUselessLengthCheck  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This check for an empty array is unnecessary.

> 1 │ const isAllTruthyOrEmpty = array.length === 0 || array.every(Boolean);
│ ^^^^^^^^^^^^^^^^^^
2 │

ℹ every() returns true for an empty array, so the result is the same without the check.

ℹ Remove the length check.

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

1 │ const·isAllTruthyOrEmpty·=·array.length·===·0·||·array.every(Boolean);
│ ----------------------
if (array.length > 0) {
for (const element of array) {
use(element);
}
}
code-block.js:1:5 lint/nursery/noUselessLengthCheck  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This check for a non-empty array is unnecessary.

> 1 │ if (array.length > 0) {
│ ^^^^^^^^^^^^^^^^
2 │ for (const element of array) {
3 │ use(element);

ℹ A for...of loop doesn't run its body for an empty array, so the code behaves the same without the check.

ℹ Remove the length check.

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

1 │ - if·(array.length·>·0)·{
2 │ - ····for·(const·element·of·array)·{
1 │ + for·(const·element·of·array)·{
3 2 │ use(element);
4 │ - ····}
5 │ - }
3 │ + ····}
6 4 │

if (array.every(Boolean));
if (array.some(Boolean));
for (const element of array) {
use(element);
}
if (array.length === 0 || anotherCheck() || array.every(Boolean));
const isNonEmptyAllTruthyArray = array.length > 0 && array.every(Boolean);
const isEmptyArrayOrAllTruthy = array.length === 0 || array.some(Boolean);