Assignment To Receiver

ID

go.assignment_to_receiver

Severity

low

Remediation Complexity

simple

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Go

Tags

reliability

Description

Reports an assignment whose left-hand side is a method’s value receiver, for example func (r T) m() { r = X }.

Rationale

A value receiver is a copy, so reassigning it changes nothing the caller can observe — the statement is effectively dead. It usually signals a misplaced mutation that was meant to update a field (r.field = X) or a method that should have declared a pointer receiver. Writing through a pointer receiver (p.field = X) is a real, caller-visible mutation and is not flagged.

// Bad
func (r T) reset()  { r = T{} } // FLAW

// Good
func (p *T) reset() { p.x = 0 } // OK — visible to the caller

Remediation

Use a pointer receiver (func (r *T) m()) when the method must mutate the value, or assign the intended field through the receiver instead of replacing the receiver itself.