Self assignment¶
ID: js/redundant-assignmentKind: problemSecurity severity: Severity: warningPrecision: highTags: - quality - reliability - correctness - external/cwe/cwe-480 - external/cwe/cwe-561Query 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 missingthis 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 functionRectangle is intended to initialize propertiesx,y,width, andheight to the parameters of the same names.
functionRectangle(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 withthis: the code now performs a useless assignment of thewidth parameter to itself and leaves thewidth property uninitialized.
To fix this issue, insert athis qualifier:
functionRectangle(x,y,width,height){this.x=x;this.y=y;this.width=width;this.height=height;}