uber-go / nilaway

Static analysis tool to detect potential nil panics in Go code
Apache License 2.0
3.17k stars 65 forks source link

False positive in tracking conversion from value to pointer receiver #284

Open sonalmahajan15 opened 1 month ago

sonalmahajan15 commented 1 month ago

NilAway reports a false positive when a value (m) calls a method (m.foo()) with a pointer receiver, not being able to understand the implicit conversion that takes place: (&m).foo().

type myInt int

func (m *myInt) foo() *myInt {
    if m == nil {
        return nil
    }
    return m
}

func test(m myInt) {
    v := m.foo()
    _ = *v // FP here
}

However, if we convert this into a regular parameter, then NilAway can reason about the flow correctly and does not report any false positive.

type myInt int

func foo(m *myInt) *myInt {
    if m == nil {
        return nil
    }
    return m
}

func test(m myInt) {
    v := foo(&m)
    _ = *v // no error -- correct behavior
}