Unused local variables and functions should be removed

  • UNUSED_DECL
  • Code Quality
  • Low
  • No tags

This rule applies when local variables and functions are declared but not used.

For maintainability, it is recommended to remove unused code. Also, it might be a mistake that a programmer forgets to use declared variables and functions.

Note:

  1. Not applied when the name of a const variable consists of just _ or contains ignore or fake because it could be a programmer's intention that the initialized value is not to be used
  2. Not applied when the value of a const variable holds this context because it is often used as boilerplate (e.g. const self = this;)
  3. Not applied at test case code because unused declarations are usually harmless at tests and tend to occur more. Currently, BDD, TDD and QUnit style test cases are recognized.

Noncompliant Code Example

View with compliant examples side by side
function foo() {
    function unusedFunction() { // UNUSED_DECL alarm because local variable 'unusedFunction' is not used.
    }
    var bar = 1;
    var unused_var; // UNUSED_DECL alarm because local variable 'unused_var' is not used.
    doSomething(bar);
}

function doSomethinng(bar) {
}

Compliant Code Example

View with noncompliant examples side by side
function foo() {
    var bar = 1;
    doSomething(bar);
}

function doSomethinng(bar) {
}

Version

This rule was introduced in DeepScan 1.0.0-alpha.