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 the pointer is nulled after the dereferencing #257

Closed SongShawn closed 5 months ago

SongShawn commented 5 months ago
type User struct {
    Name *string
}

func DumpUserName(user *User) {
        // readnil.go:8:8: error: Potential nil panic detected. Observed nil flow from source to dereference point
        // readnil.go:8:8: field `Name` dereferenced
    a := *user.Name

        // readnil.go:9:14: literal `nil` assigned into field `Name`
    user.Name = nil
    _ = a
}

func DumpUserName2() {
    n := "ssss"
    u := User{Name: &n}
    DumpUserName(&u)
}

Detailed error information readnil.go:8:8: error: Potential nil panic detected. Observed nil flow from source to dereference point:

After dereferencing user.Name and assigning user.Name to nil, I don't think it should throw an error

SongShawn commented 5 months ago

set -experimental-struct-init=true, the error disappears.