noUselessLengthCheck (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.5 - Diagnostic Category:
lint/nursery/noUselessLengthCheck - This rule has an unsafe fix.
- The default severity of this rule is information.
- Sources:
- Same as
unicorn/no-useless-length-check
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noUselessLengthCheck": "error" } } }}Description
Section titled “Description”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()returnsfalsefor an empty array, soarray.length > 0 && array.some(callback)gives the same result asarray.some(callback).every()returnstruefor an empty array, soarray.length === 0 || array.every(callback)gives the same result asarray.every(callback).- A
for...ofloop doesn’t run its body for an empty array, so anifstatement 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.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”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);See Also
Section titled “See Also”- To choose how length checks are written, see
useExplicitLengthCheck.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.