noUnusedVariables
このコンテンツはまだ日本語訳がありません。
Summary
Section titled “Summary”- Rule available since:
v1.0.0
- Diagnostic Category:
lint/correctness/noUnusedVariables
- This rule is recommended, which means is enabled by default.
- This rule has an unsafe fix.
- The default severity of this rule is warning.
- Sources:
- Same as
no-unused-vars
- Same as
@typescript-eslint/no-unused-vars
- Same as
unused-imports/no-unused-vars
- Same as
Description
Section titled “Description”Disallow unused variables.
There is an exception to this rule: variables that start with underscore, e.g. let _something;
.
The pattern of having an underscore as a prefix of a variable is a very diffuse pattern among programmers, and Biome follows it.
This rule won’t report unused imports. If you want to report unused imports, enable noUnusedImports.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”let a = 4;a++;
code-block.js:1:5 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This variable a is unused.
> 1 │ let a = 4;
│ ^
2 │ a++;
3 │
ℹ Unused variables are often the result of an incomplete refactoring, typos, or other sources 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() {}
code-block.js:1:10 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This function foo is unused.
> 1 │ function foo() {}
│ ^^^
2 │
ℹ Unused variables are often the result of an incomplete refactoring, typos, or other sources of bugs.
ℹ Unsafe fix: If this is intentional, prepend foo with an underscore.
1 │ - function·foo()·{}
1 │ + function·_foo()·{}
2 2 │
function foo() { foo();}
code-block.js:1:10 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This function foo is unused.
> 1 │ function foo() {
│ ^^^
2 │ foo();
3 │ }
ℹ Unused variables are often the result of an incomplete refactoring, typos, or other sources of bugs.
ℹ Unsafe fix: If this is intentional, prepend foo with an underscore.
1 │ - function·foo()·{
2 │ - ····foo();
1 │ + function·_foo()·{
2 │ + ····_foo();
3 3 │ }
4 4 │
const foo = () => { foo();};
code-block.js:1:7 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This variable foo is unused.
> 1 │ const foo = () => {
│ ^^^
2 │ foo();
3 │ };
ℹ Unused variables are often the result of an incomplete refactoring, typos, or other sources 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>() {}
code-block.ts:1:19 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This type parameter T is unused.
> 1 │ export function f<T>() {}
│ ^
2 │
ℹ Unused variables are often the result of an incomplete refactoring, typos, or other sources 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 │
const { brand } = car;
code-block.js:1:9 lint/correctness/noUnusedVariables ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This variable brand is unused.
> 1 │ const { brand } = car;
│ ^^^^^
2 │
ℹ Unused variables are often the result of an incomplete refactoring, typos, or other sources of bugs.
function foo(b) { console.log(b)};foo();
export function foo(_unused) {}
function used_overloaded(): number;function used_overloaded(s: string): string;function used_overloaded(s?: string) { return s;}used_overloaded();
const car = { brand: "Tesla", year: 2019, countryCode: "US" };const { brand, ...rest } = car;console.log(brand, rest);
Options
Section titled “Options”The rule has the following options
ignoreRestSiblings
Section titled “ignoreRestSiblings”Whether to ignore unused variables from an object destructuring with a spread (i.e.: a
and b
in const { a, b, ...rest } = obj
should be ignored by this rule).
Defaults to true
.
{ "options": { "ignoreRestSiblings": false }}
const { brand, ...other } = car;console.log(brand);
code-block.js:1:19 lint/correctness/noUnusedVariables ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This variable other is unused.
> 1 │ const { brand, …other } = car;
│ ^^^^^
2 │ console.log(brand);
3 │
ℹ Unused variables are often the result of an incomplete refactoring, typos, or other sources of bugs.
How to configure
Section titled “How to configure”{ "linter": { "rules": { "correctness": { "noUnusedVariables": "error" } } }}