A useful (would have saved a minor production incident at my org, at least) extension to SA5011 could be to recognize that the returned value of a checked lookup of a pointer-valued map is nil on the !ok path. For example, given these similar functions:
package example
func Foo(k string, m map[string]*int) {
v, ok := m[k]
if !ok {
println("not found")
}
println(*v)
}
func Bar(k string, m map[string]*int) {
v := m[k]
if v == nil {
println("not found")
}
println(*v)
}
A warning is generated for the second function but not the first one:
$ staticcheck ./...
test.go:16:10: possible nil pointer dereference (SA5011)
test.go:13:5: this check suggests that the pointer can be nil
Version info:
$ staticcheck -version
staticcheck 2025.1.1 (0.6.1)
$ staticcheck -debug.version
staticcheck 2025.1.1 (0.6.1)
Compiled with Go version: go1.25.6
Main module:
honnef.co/go/[email protected] (sum: h1:R094WgE8K4JirYjBaOpz/AvTyUu/3wbmAoskKN/pxTI=)
Dependencies:
github.com/BurntSushi/[email protected] (sum: h1:pxW6RcqyfI9/kWtOwnv/G+AzdKuy2ZrqINhenH4HyNs=)
golang.org/x/exp/[email protected] (sum: h1:1P7xPZEwZMoBoz0Yze5Nx2/4pxj6nw9ZqHWXqP0iRgQ=)
golang.org/x/[email protected] (sum: h1:Zb7khfcRGKk+kqfxFaP5tZqCnDZMjC5VtUBs87Hr6QM=)
golang.org/x/[email protected] (sum: h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=)
golang.org/x/[email protected] (sum: h1:BgcpHewrV5AUp2G9MebG4XPFI1E2W41zU1SaqVA9vJY=)
$ go version
go version go1.25.6 linux/amd64
A useful (would have saved a minor production incident at my org, at least) extension to
SA5011could be to recognize that the returned value of a checked lookup of a pointer-valued map isnilon the!okpath. For example, given these similar functions:A warning is generated for the second function but not the first one:
Version info: