noUnusedVariables
诊断类别:lint/correctness/noUnusedVariables
¥Diagnostic Category: lint/correctness/noUnusedVariables
自从:v1.0.0
¥Since: v1.0.0
来源:
¥Sources:
-
与以下相同:
no-unused-vars
¥Same as:
no-unused-vars
-
与以下相同:
@typescript-eslint/no-unused-vars
¥Same as:
@typescript-eslint/no-unused-vars
-
与以下相同:
unused-imports/no-unused-vars
¥Same as:
unused-imports/no-unused-vars
禁止未使用的变量。
¥Disallow unused variables.
此规则有一个例外:以下划线开头的变量,例如 let _something;
。
¥There is an exception to this rule:
variables that starts with underscore, e.g. let _something;
.
在变量名称的前缀中使用下划线的模式在程序员中非常普遍,Biome 决定遵循它。
¥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.
此规则不会报告未使用的导入。如果你想报告未使用的导入,请启用 noUnusedImports。
¥This rule won’t report unused imports. If you want to report unused imports, enable noUnusedImports.
从 v1.9.0
开始,规则将不再检查未使用的函数参数。如果你想报告未使用的函数参数,请启用 noUnusedFunctionParameters。
¥From v1.9.0
, the rule won’t check unused function parameters any more.
If you want to report unused function parameters,
enable noUnusedFunctionParameters.
¥Examples
¥Invalid
code-block.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 │
code-block.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.
code-block.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 │ }
code-block.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.
code-block.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 │
code-block.ts: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
¥Related links