phase-shift is a static analysis tool for Go.
The first analyzer checks functions annotated with //phase:nonmutating.
An annotated function must not mutate caller-visible state through parameters or method receivers.
Standard go install installation,
GOPRIVATE=github.com/phasemerge go install github.com/phasemerge/go-phase-shift/cmd/phase-shift@latestRun phase-shift on your Go packages,
phase-shift ./...This function is reported because it mutates through a pointer parameter,
//phase:nonmutating
func F(p *int) {
*p = 1
}This function is allowed because it only reads through the pointer parameter,
//phase:nonmutating
func F(p *int) int {
return *p
}This method is allowed because a scalar field assignment on a value receiver mutates only the receiver copy,
type Count struct{ n int }
//phase:nonmutating
func (c Count) Increment() Count {
c.n++
return c
}This method is reported because the slice field is mutated for the caller,
type Buffer struct{ data []byte }
//phase:nonmutating
func (b Buffer) ClearFirstByte() {
b.data[0] = 0
}Run all fixers,
mise run fixersRun all tests,
mise run testsRun all linters,
mise run linters