-
-
Notifications
You must be signed in to change notification settings - Fork 407
Open
Description
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/tools@v0.6.1 (sum: h1:R094WgE8K4JirYjBaOpz/AvTyUu/3wbmAoskKN/pxTI=)
Dependencies:
github.com/BurntSushi/toml@v1.4.1-0.20240526193622-a339e1f7089c (sum: h1:pxW6RcqyfI9/kWtOwnv/G+AzdKuy2ZrqINhenH4HyNs=)
golang.org/x/exp/typeparams@v0.0.0-20231108232855-2478ac86f678 (sum: h1:1P7xPZEwZMoBoz0Yze5Nx2/4pxj6nw9ZqHWXqP0iRgQ=)
golang.org/x/mod@v0.23.0 (sum: h1:Zb7khfcRGKk+kqfxFaP5tZqCnDZMjC5VtUBs87Hr6QM=)
golang.org/x/sync@v0.11.0 (sum: h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=)
golang.org/x/tools@v0.30.0 (sum: h1:BgcpHewrV5AUp2G9MebG4XPFI1E2W41zU1SaqVA9vJY=)
$ go version
go version go1.25.6 linux/amd64
Reactions are currently unavailable