gordonklaus / ineffassign

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

False positive #29

Closed rjoleary closed 6 years ago

rjoleary commented 6 years ago

Encountered this in some real code. This is the minimal repro:

package main

func main() {
    var a, b uint64
    for x, y := a, uint64(0); b < 10; b += y {
        _ = x
        y = 5
    }
}

The analysis gives:

main.go:5:9: ineffectual assignment to y

However, without the declaration of y on that line, the code will not compile. The assignment is necessary to declare y.

rjoleary commented 6 years ago

Playing around with ineffassign some more, I came up with something more general:

package main

func main() {
    x := 0
    x = 1

    y := 42
    y = 2

    z := int64(0)
    z = 3

    _, _, _ = x, y, z
}

The analysis gives:

main.go:7:2: ineffectual assignment to y
main.go:10:2: ineffectual assignment to z

It appears ineffassign treats the integer literal 0 as a special case. Perhaps int64(0) should also be a special case along with other integer literals constants for 0?

gordonklaus commented 6 years ago

Thanks for the report, this is now fixed. To avoid false positives, the tool now treats any potential conversion of zero as a zero initializer. Without type information, it can't distinguish conversions from function or method calls, but better to err on this side than to report false positives.