CodeQL documentation

Useless assignment to field

ID: go/useless-assignment-to-field
Kind: problem
Security severity: 
Severity: warning
Precision: very-high
Tags:
   - maintainability
   - external/cwe/cwe-563
Query suites:
   - go-security-and-quality.qls

Click to see the query in the CodeQL repository

A value is assigned to a field, but its value is never read. This means that the assignment has no effect, and could indicate a logic error or incomplete code.

Recommendation

Examine the assignment closely to determine whether it is redundant, or whether it is perhaps a symptom of another bug.

Example

The following example shows a simple struct type wrapping an integer counter with a method reset that sets the counter to zero.

package main

type counter struct {
	val int
}

func (c counter) reset() {
	c.val = 0
}

However, the receiver variable of reset is declared to be of type counter, not *counter, so the receiver value is passed into the method by value, not by reference. Consequently, the method does not actually mutate its receiver as intended.

To fix this, change the type of the receiver variable to *counter:

package main

func (c *counter) resetGood() {
	c.val = 0
}

References

  • © GitHub, Inc.
  • Terms
  • Privacy