nilaway
nilaway copied to clipboard
False positive with closures
In the following example containing the falsePos
clousure, nilaway reports the error shown below:
func main() {
falsePos := func() (*bool, error) {
b := false
if rand.Float64() < 0.5 {
return nil, fmt.Errorf("error: %v", b)
}
return &b, nil
}
res, err := falsePos()
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(*res)
}
error main.go:22:15: error: Potential nil panic detected. Observed nil flow from source to dereference point:
-> nilaway-issue/main.go:22:15: unassigned variable `res` dereferenced
Whereas, the same function taken out of the main, causes no error:
func falsePos() (*bool, error) {
b := false
if rand.Float64() < 0.5 {
return nil, fmt.Errorf("error: %v", b)
}
return &b, nil
}
func main() {
res, err := falsePos()
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(*res)
}
You're right! We currently only have limited support for anonymous functions and closures (in fact they are currently hidden under a feature flag that can be enabled if you add <nilaway anonymous function enable>
to the file docstring).
We are prioritizing on performance validations / polishing up those features to move them to main NilAway. I'm keeping this issue open to track the work.
Thanks for the report!