CodeQL documentation

Self assignment

ID: js/redundant-assignment
Kind: problem
Severity: warning
Precision: high
Tags:
   - reliability
   - correctness
   - external/cwe/cwe-480
   - external/cwe/cwe-561
Query suites:
   - javascript-security-and-quality.qls

Click to see the query in the CodeQL repository

Assigning a variable to itself typically indicates a mistake such as a missing this qualifier or a misspelled variable name.

Recommendation

Carefully inspect the assignment to check for misspellings or missing qualifiers.

If the self-assignment is intentional and is needed for documentation or optimization purposes, add a JSDoc comment with a @type tag. This will indicate the self-assignment is intentional.

Example

In the example below, the constructor function Rectangle is intended to initialize properties x, y, width, and height to the parameters of the same names.

function Rectangle(x, y, width, height) {
	this.x = x;
	this.y = y;
	width = width;
	this.height = height;
}

Note, however, that on line 4 the programmer forgot to qualify the left hand side of the assignment with this: the code now performs a useless assignment of the width parameter to itself and leaves the width property uninitialized.

To fix this issue, insert a this qualifier:

function Rectangle(x, y, width, height) {
	this.x = x;
	this.y = y;
	this.width = width;
	this.height = height;
}

References

  • Ecma International, ECMAScript Language Definition, 5.1 Edition, Section 11.13. ECMA, 2011.
  • Common Weakness Enumeration: CWE-480.
  • Common Weakness Enumeration: CWE-561.