Lint Rules

noUnusedVariables (since v0.9.0)

Disallow unused variables.

There are two exceptions to this rule:

  1. variables that starts with underscore, ex: let _something;
  2. the React variable;

The pattern of having an underscore as prefix of a name of variable is a very diffuse pattern among programmers, and Rome 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

Invalid

const a = 4;
correctness/noUnusedVariables.js:1:7 lint/correctness/noUnusedVariables  FIXABLE  ━━━━━━━━━━━━━━━━━━

   This variable is unused.
  
  > 1 │ const a = 4;
         ^
    2 │ 
  
   Unused variables usually are result of incomplete refactoring, typos and other source of bugs.
  
   Suggested fix: If this is intentional, prepend a with an underscore.
  
    1  - const·a·=·4;
      1+ const·_a·=·4;
    2 2  
  
let a = 4;
correctness/noUnusedVariables.js:1:5 lint/correctness/noUnusedVariables  FIXABLE  ━━━━━━━━━━━━━━━━━━

   This variable is unused.
  
  > 1 │ let a = 4;
       ^
    2 │ 
  
   Unused variables usually are result of incomplete refactoring, typos and other source of bugs.
  
   Suggested fix: If this is intentional, prepend a with an underscore.
  
    1  - let·a·=·4;
      1+ let·_a·=·4;
    2 2  
  
function foo() {
};
correctness/noUnusedVariables.js:1:10 lint/correctness/noUnusedVariables  FIXABLE  ━━━━━━━━━━━━━━━━━

   This function is unused.
  
  > 1 │ function foo() {
            ^^^
    2 │ };
    3 │ 
  
   Unused variables usually are result of incomplete refactoring, typos and other source of bugs.
  
   Suggested fix: If this is intentional, prepend foo with an underscore.
  
    1  - function·foo()·{
      1+ function·_foo()·{
    2 2  };
    3 3  
  
function foo(myVar) {
    console.log('foo');
}
foo();
correctness/noUnusedVariables.js:1:14 lint/correctness/noUnusedVariables  FIXABLE  ━━━━━━━━━━━━━━━━━

   This parameter is unused.
  
  > 1 │ function foo(myVar) {
                ^^^^^
    2 │     console.log('foo');
    3 │ }
  
   Unused variables usually are result of incomplete refactoring, typos and other source of bugs.
  
   Suggested fix: If this is intentional, prepend myVar with an underscore.
  
    1  - function·foo(myVar)·{
      1+ function·foo(_myVar)·{
    2 2      console.log('foo');
    3 3  }
  
const foo = () => {
};
correctness/noUnusedVariables.js:1:7 lint/correctness/noUnusedVariables  FIXABLE  ━━━━━━━━━━━━━━━━━━

   This variable is unused.
  
  > 1 │ const foo = () => {
         ^^^
    2 │ };
    3 │ 
  
   Unused variables usually are result of incomplete refactoring, typos and other source of bugs.
  
   Suggested fix: If this is intentional, prepend foo with an underscore.
  
    1  - const·foo·=·()·=>·{
      1+ const·_foo·=·()·=>·{
    2 2  };
    3 3  
  
function foo() {
    foo();
}
correctness/noUnusedVariables.js:1:10 lint/correctness/noUnusedVariables  FIXABLE  ━━━━━━━━━━━━━━━━━

   This function is unused.
  
  > 1 │ function foo() {
            ^^^
    2 │     foo();
    3 │ }
  
   Unused variables usually are result of incomplete refactoring, typos and other source of bugs.
  
   Suggested fix: If this is intentional, prepend foo with an underscore.
  
    1  - function·foo()·{
      1+ function·_foo()·{
    2 2      foo();
    3 3  }
  
const foo = () => {
    foo();
    console.log(this);
};
correctness/noUnusedVariables.js:1:7 lint/correctness/noUnusedVariables  FIXABLE  ━━━━━━━━━━━━━━━━━━

   This variable is unused.
  
  > 1 │ const foo = () => {
         ^^^
    2 │     foo();
    3 │     console.log(this);
  
   Unused variables usually are result of incomplete refactoring, typos and other source of bugs.
  
   Suggested fix: If this is intentional, prepend foo with an underscore.
  
    1  - const·foo·=·()·=>·{
      1+ const·_foo·=·()·=>·{
    2 2      foo();
    3 3      console.log(this);
  

Valid

function foo(b) {
    console.log(b)
};
foo();
function foo(_unused) {
};
foo();
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();