← 返回首页
Self assignment — CodeQL query help documentation CodeQL docs
CodeQL documentation
CodeQL resources

Self assignment

ID: go/redundant-assignment Kind: problem Security severity: Severity: warning Precision: high Tags: - quality - reliability - correctness - external/cwe/cwe-480 - external/cwe/cwe-561 Query suites: - go-code-quality.qls - go-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 qualifier or a misspelled variable name.

Recommendation

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

Example

In the example below, the struct type Rect has two setter methods setWidth and setHeight that are meant to be used to update the width and height fields, respectively:

package main type Rect struct { x, y, width, height int } func (r *Rect) setWidth(width int) { r.width = width } func (r *Rect) setHeight(height int) { height = height }

Note, however, that in setHeight the programmer forgot to qualify the left-hand side of the assignment with the receiver variable r, so the method performs a useless assignment of the width parameter to itself and leaves the width field unchanged.

To fix this issue, insert a qualifier:

package main func (r *Rect) setHeightGood(height int) { r.height = height }

References