Dead variables should not be used

  • UNUSED_VAR_ASSIGN
  • Code Quality
  • Low
  • cwe

This rule applies when a variable is assigned a value that is not used by subsequent code or is overwritten by another assignment without use.

The value is dead so that it might imply a mistake or unnecessary code.

Note:

  1. Not applied for the assignment with initialization values like "", '', 0, true, !0, false, !1, null, undefined, void …, {}, [].
  2. Not applied for the assignment saving this context because it is often used as boilerplate (e.g. let self = this;).
  3. Not applied when the variable name consists of just _ or contains ignore or fake because it could be a programmer's intention that the assigned value is not to be used.
  4. Not applied at test case code because unused values 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(url) {
    var target = url + "user" + user; // UNUSED_VAR_ASSIGN alarm
    target = url.replace(/\.|\?|\&|\/|\=|\:|\-|\s/gi, ""); // UNUSED_VAR_ASSIGN alarm
}

Compliant Code Example

View with noncompliant examples side by side
function foo(url) {
    var target = url + "user" + user;
    target = target.replace(/\.|\?|\&|\/|\=|\:|\-|\s/gi, "");
    return target;
}

Version

This rule was introduced in DeepScan 1.0.0-alpha.

See

  • CWE-563

  • MISRA C:2012, Rule 2.2: There shall be no dead code

Was this documentation helpful?