noUnusedVariables (since v1.0.0)
Diagnostic Category: lint/correctness/noUnusedVariables
Source: no-unused-vars
Disallow unused variables.
There are two exceptions to this rule:
- variables that starts with underscore, ex:
let _something;
- the
React
variable;
The pattern of having an underscore as prefix of a name of variable is a very diffuse pattern among programmers, and Biome decided to follow it.
Importing the React
variable was a mandatory pattern until some time ago:
For the time being this rule will ignore it, but this might change in the future releases.
Examples
Section titled ExamplesInvalid
Section titled Invalidlet a = 4;a++;
correctness/noUnusedVariables.js:1:5 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━
⚠ This variable is unused.
> 1 │ let a = 4;
│ ^
2 │ a++;
3 │
ℹ Unused variables usually are result of incomplete refactoring, typos and other source of bugs.
ℹ Unsafe fix: If this is intentional, prepend a with an underscore.
1 │ - let·a·=·4;
2 │ - a++;
1 │ + let·_a·=·4;
2 │ + _a++;
3 3 │
function foo() {}
correctness/noUnusedVariables.js:1:10 lint/correctness/noUnusedVariables ━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This function is unused.
> 1 │ function foo() {}
│ ^^^
2 │
ℹ Unused variables usually are result of incomplete refactoring, typos and other source of bugs.
export function foo(myVar) { console.log('foo');}
correctness/noUnusedVariables.js:1:21 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━
⚠ This parameter is unused.
> 1 │ export function foo(myVar) {
│ ^^^^^
2 │ console.log('foo');
3 │ }
ℹ Unused variables usually are result of incomplete refactoring, typos and other source of bugs.
ℹ Unsafe fix: If this is intentional, prepend myVar with an underscore.
1 │ - export·function·foo(myVar)·{
1 │ + export·function·foo(_myVar)·{
2 2 │ console.log('foo');
3 3 │ }
function foo() { foo();}
correctness/noUnusedVariables.js:1:10 lint/correctness/noUnusedVariables ━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This function is unused.
> 1 │ function foo() {
│ ^^^
2 │ foo();
3 │ }
ℹ Unused variables usually are result of incomplete refactoring, typos and other source of bugs.
const foo = () => { foo();};
correctness/noUnusedVariables.js:1:7 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━
⚠ This variable is unused.
> 1 │ const foo = () => {
│ ^^^
2 │ foo();
3 │ };
ℹ Unused variables usually are result of incomplete refactoring, typos and other source of bugs.
ℹ Unsafe fix: If this is intentional, prepend foo with an underscore.
1 │ - const·foo·=·()·=>·{
2 │ - ····foo();
1 │ + const·_foo·=·()·=>·{
2 │ + ····_foo();
3 3 │ };
4 4 │
export function f<T>() {}
correctness/noUnusedVariables.js:1:19 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━
⚠ This type parameter is unused.
> 1 │ export function f<T>() {}
│ ^
2 │
ℹ Unused variables usually are result of incomplete refactoring, typos and other source of bugs.
ℹ Unsafe fix: If this is intentional, prepend T with an underscore.
1 │ - export·function·f<T>()·{}
1 │ + export·function·f<_T>()·{}
2 2 │
Valid
Section titled Validfunction foo(b) { console.log(b)};foo();
export function foo(_unused) {}
import React from 'react';function foo() { return <div />;};foo();
function used_overloaded(): number;function used_overloaded(s: string): string;function used_overloaded(s?: string) { return s;}used_overloaded();