nilaway
nilaway copied to clipboard
Support for `errors.Join`
nilaway
detects false positives when errors.Join
is used together with errors that are guaranteed to not be nil
, like errors.Join(errors.New("an error"))
.
nilaway
could have the rule: if any of the argument to errors.Join
are guaranteed to not be nil, the joined error is also guaranteed to not be nil.
The full working example:
package main
import "errors"
func main() {
printFn(true)
printFn(false)
}
func printFn(ferr bool) {
i, err := fn(ferr)
if err != nil {
println(err.Error())
} else {
println(*i)
}
}
func fn(err bool) (*int, error) {
if err {
return nil, errors.Join(errors.New("an error"), errors.New("another error"))
} else {
i := 42
return &i, nil
}
}
runs fine, but produces the nilaway
error output
main.go:15:12: error: Potential nil panic detected. Observed nil flow from source to dereference point:
-> nilaway-errors-join/main.go:21:10: literal `nil` returned from `fn()` in position 0 when the error return in position 1 is not guaranteed to be non-nil through all paths
-> nilaway-errors-join/main.go:15:12: result 0 of `fn()` dereferenced