go-critic
go-critic copied to clipboard
checker: pointer dereference requires nil check above in the same function
Hi! It would be nice to have a very opinionated linter that should report error if there is no != nil checking before pointer dereference.
Consider this code that panics:
type A struct {
b *B
}
type B struct {
c int
}
func main() {
a := A{}
fmt.Print(a.b.c)
}
It would be useful to report this dereferences and then manually add if a.b == nil { return } like statements.
AFAIK there is no linter that checks this behavior (nilness don't check it). Also I don't see a way to implement it with go-ruleguard.
I could implement it after feature discussion)
I see this implementation: for each function add fields from if .*<field> == nil {...} statements to the map and if there is pointer dereference, then check if the field exist in the map.