gordonklaus / ineffassign

Detect ineffectual assignments in Go code.
MIT License
397 stars 23 forks source link

Why the two cases have different results? #72

Closed SMLsk closed 2 years ago

SMLsk commented 2 years ago
func main() {
        // case 1
    var err = errors.New("1")
    println(err.Error())
    err = errors.New("2") // uncaught ineffectual assignment

        // case 2
    var err2 = errors.New("1")
    if err2 != nil {
        println("ok")
    }
    err2 = errors.New("2") // report: ineffectual assignment to err2
}

ineffassign version: last version available go version: go1.18.2

I thought err = errors.New("2") and err2 = errors.New("2") would report the ineffectual assignment, but actually only the second case will report.

gordonklaus commented 2 years ago

Hi. The first case is possibly not ineffectual because the analyzer has no idea what the call to err.Error() might do; it might capture the address of err and store it for later use, making the next assignment effectual.

Of course, we know that this doesn't happen because we know how errors.New and err.Error work. But ineffassign doesn't look at types or inter-function data flow.

SMLsk commented 2 years ago

Thanks!