nilassign
nilassign finds that assigning to invalid memory address or nil pointer dereference.
Instruction
go install github.com/sivchari/nilassign/cmd/nilassign
Usage
package main
func main() {
{
var i *int
*i = 2 // ng
}
{
n := &Node{}
n.Val = 1 // ok
*n.Pval = 1 // ng
n.Node.Val = 1 // ng
n.Node.Node.Val = 1 // ng
n.ChildNode = &Node{Val: 1} // ok
n.PVal = &num // ok
}
}
type Node struct {
Val int
Pval *int
Node *Node
}
fish
go vet -vettool=(which nilassign) ./...
./main.go:6:3: this assignment occurs invalid memory address or nil pointer dereference
./main.go:13:3: this assignment occurs invalid memory address or nil pointer dereference
./main.go:14:3: this assignment occurs invalid memory address or nil pointer dereference
./main.go:15:3: this assignment occurs invalid memory address or nil pointer dereference
bash
$ go vet -vettool=`which nilassign` ./...
./main.go:6:3: this assignment occurs invalid memory address or nil pointer dereference
./main.go:13:3: this assignment occurs invalid memory address or nil pointer dereference
./main.go:14:3: this assignment occurs invalid memory address or nil pointer dereference
./main.go:15:3: this assignment occurs invalid memory address or nil pointer dereference
CI
CircleCI
- run:
name: Install nilassign
command: go get github.com/sivchari/nilassign
- run:
name: Run nilassign
command: go vet -vettool=`which nilassign` ./...
GitHub Actions
- name: Install nilassign
run: go get github.com/sivchari/nilassign
- name: Run nilassign
run: go vet -vettool=`which nilassign` ./...