gordonklaus / ineffassign

Detect ineffectual assignments in Go code.
MIT License
394 stars 22 forks source link

false positive: assigning to return variable with panic #22

Closed ianrose14 closed 6 years ago

ianrose14 commented 6 years ago

example program:

package main

import (
    "log"
)

func main() {
    out := func() (ok bool) {
        ok = true
        defer func() {
            if r := recover(); r != nil {
                log.Println("panic happened!")
            }
        }()

        panic("oh crud")
        return false
    }()

    log.Println(out)
}

ineffassign prints: ineffectual assignment to ok

but if you remove that assignment (line 9), the program will print false instead of true

gordonklaus commented 6 years ago

Thanks for the report. The tool does not correctly handle named results in the face of possible panics. A fix is forthcoming.