golangci-lint/test/testdata/nilassign.go
sivchari bbf0450265
add nilassign linter (#2131)
Co-authored-by: Fernandez Ludovic <ldez@users.noreply.github.com>
2021-07-28 20:27:26 +02:00

29 lines
603 B
Go

//args: -Enilassign
package testdata
var num = 1
func pvar() {
var ii *int
*ii = 1 // ERROR "this assignment occurs invalid memory address or nil pointer dereference"
var i *int
i = &num // OK
_ = i // OK
}
func pstruct() {
n := new(Node)
*n.PVal = 1 // ERROR "this assignment occurs invalid memory address or nil pointer dereference"
*n.ChildNode.PVal = 1 // ERROR "this assignment occurs invalid memory address or nil pointer dereference"
n.ChildNode = &Node{PVal: &num} // OK
n.PVal = &num // OK
}
type Node struct {
PVal *int
ChildNode *Node
}