Aller au contenu

noParametersOnlyUsedInRecursion

Ce contenu n’est pas encore disponible dans votre langue.

biome.json
{
"linter": {
"rules": {
"nursery": {
"noParametersOnlyUsedInRecursion": "error"
}
}
}
}

Disallow function parameters that are only used in recursive calls.

A parameter that is only passed to recursive calls is effectively unused and can be removed or replaced with a constant, simplifying the function.

This rule is inspired by Rust Clippy’s only_used_in_recursion lint.

function factorial(n, acc) {
if (n === 0) return 1;
return factorial(n - 1, acc);
}
code-block.js:1:23 lint/nursery/noParametersOnlyUsedInRecursion  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━

This parameter is only used in recursive calls.

> 1 │ function factorial(n, acc) {
^^^
2 │ if (n === 0) return 1;
3 │ return factorial(n - 1, acc);

Parameters that are only used in recursive calls are effectively unused and can be removed.

If the parameter is needed for the recursion to work, consider if the function can be refactored to avoid it.

Unsafe fix: If this is intentional, prepend acc with an underscore.

1 - function·factorial(n,·acc)·{
1+ function·factorial(n,·_acc)·{
2 2 if (n === 0) return 1;
3 - ····return·factorial(n·-·1,·acc);
3+ ····return·factorial(n·-·1,·_acc);
4 4 }
5 5

function countdown(n, step) {
if (n === 0) return 0;
return countdown(n - step, step);
}
code-block.js:1:23 lint/nursery/noParametersOnlyUsedInRecursion  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━

This parameter is only used in recursive calls.

> 1 │ function countdown(n, step) {
^^^^
2 │ if (n === 0) return 0;
3 │ return countdown(n - step, step);

Parameters that are only used in recursive calls are effectively unused and can be removed.

If the parameter is needed for the recursion to work, consider if the function can be refactored to avoid it.

Unsafe fix: If this is intentional, prepend step with an underscore.

1 - function·countdown(n,·step)·{
1+ function·countdown(n,·_step)·{
2 2 if (n === 0) return 0;
3 - ····return·countdown(n·-·step,·step);
3+ ····return·countdown(n·-·_step,·_step);
4 4 }
5 5

class Counter {
count(n, acc) {
if (n === 0) return 0;
return this.count(n - 1, acc);
}
}
code-block.js:2:14 lint/nursery/noParametersOnlyUsedInRecursion  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━

This parameter is only used in recursive calls.

1 │ class Counter {
> 2 │ count(n, acc) {
^^^
3 │ if (n === 0) return 0;
4 │ return this.count(n - 1, acc);

Parameters that are only used in recursive calls are effectively unused and can be removed.

If the parameter is needed for the recursion to work, consider if the function can be refactored to avoid it.

Unsafe fix: If this is intentional, prepend acc with an underscore.

1 1 class Counter {
2 - ····count(n,·acc)·{
2+ ····count(n,·_acc)·{
3 3 if (n === 0) return 0;
4 - ········return·this.count(n·-·1,·acc);
4+ ········return·this.count(n·-·1,·_acc);
5 5 }
6 6 }

function fn(n, acc) {
if (n === 0) return 0;
return fn(n - 1, acc || 0);
}
code-block.js:1:16 lint/nursery/noParametersOnlyUsedInRecursion  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━

This parameter is only used in recursive calls.

> 1 │ function fn(n, acc) {
^^^
2 │ if (n === 0) return 0;
3 │ return fn(n - 1, acc || 0);

Parameters that are only used in recursive calls are effectively unused and can be removed.

If the parameter is needed for the recursion to work, consider if the function can be refactored to avoid it.

Unsafe fix: If this is intentional, prepend acc with an underscore.

1 - function·fn(n,·acc)·{
1+ function·fn(n,·_acc)·{
2 2 if (n === 0) return 0;
3 - ····return·fn(n·-·1,·acc·||·0);
3+ ····return·fn(n·-·1,·_acc·||·0);
4 4 }
5 5

class Counter {
count(n, acc) {
if (n === 0) return 0;
return this?.count(n - 1, acc);
}
}
code-block.js:2:14 lint/nursery/noParametersOnlyUsedInRecursion  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━

This parameter is only used in recursive calls.

1 │ class Counter {
> 2 │ count(n, acc) {
^^^
3 │ if (n === 0) return 0;
4 │ return this?.count(n - 1, acc);

Parameters that are only used in recursive calls are effectively unused and can be removed.

If the parameter is needed for the recursion to work, consider if the function can be refactored to avoid it.

Unsafe fix: If this is intentional, prepend acc with an underscore.

1 1 class Counter {
2 - ····count(n,·acc)·{
2+ ····count(n,·_acc)·{
3 3 if (n === 0) return 0;
4 - ········return·this?.count(n·-·1,·acc);
4+ ········return·this?.count(n·-·1,·_acc);
5 5 }
6 6 }

function factorial(n, acc) {
if (n === 0) return acc;
return factorial(n - 1, acc * n);
}
function countdown(n, step) {
console.log(step);
if (n === 0) return 0;
return countdown(n - step, step);
}
function fn(n, threshold) {
if (n > threshold) return n;
return fn(n + 1, threshold);
}