Skip to content

nnutter/go-phase-shift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

phase-shift

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.

Installation

Standard go install installation,

GOPRIVATE=github.com/phasemerge go install github.com/phasemerge/go-phase-shift/cmd/phase-shift@latest

Usage

Run phase-shift on your Go packages,

phase-shift ./...

Example

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
}

Development

Run all fixers,

mise run fixers

Run all tests,

mise run tests

Run all linters,

mise run linters

About

phase-shift is a static analysis tool for Go

Resources

Stars

Watchers

Forks

Releases

Contributors

Languages