golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
121.21k stars 17.37k forks source link

x/tools/go/analysis/passes/nilness: check pointers inside structs as well #43301

Open ainar-g opened 3 years ago

ainar-g commented 3 years ago

nilness currently (as of golang/tools@ae774e9781d2 with Go 1.15.6) correctly reports this issue:

if pmax == nil {
        max = *pmax
}

But not this one:

if v.max == nil {
        max = *v.max
}

Full reproduction code.

cagedmantis commented 3 years ago

/cc @matloob

adonovan commented 1 year ago

This would require implementing the GVN (global value numbering) optimization in golang.org/x/tools/go/ssa to recognize that both occurrences of the repeated expression v.max must have the same value. This is not trivial in general: any operations in between the two statements could change the address of v, or assign to its max field.