Mixed Receivers
ID |
go.mixed_receivers |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
medium |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
Go |
Tags |
code-smell, suspicious |
Description
Reports a type whose methods use both value receivers (func (s S)) and pointer receivers
(func (s *S)).
Rationale
The Go guideline is to pick one receiver kind for a given type and use it for all of that type’s methods. Mixing the two is confusing and has real consequences: only certain method sets satisfy an interface, and copying a value of a type that has pointer-receiver methods silently drops their effect — a frequent source of subtle bugs.
// Bad
func (s S) Name() string {}
func (s *S) Start() {}
// Good
func (s *S) Name() string {}
func (s *S) Start() {}
Remediation
Choose a single receiver kind for the type (usually pointer when any method mutates the receiver or the type is large) and apply it to every method of the type.