gordonklaus / ineffassign

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

Incorrect ineffectual assignment in switch #57

Closed glooms closed 3 years ago

glooms commented 3 years ago

I found a false ineffectual assignment in a case similar to this:

package main

import (
        "fmt"
)

func main() {
        var a int
        switch n := -1; {
        case n == -1:
                n += 1
                fallthrough
        case n == 0:
                a = 10
        }
        fmt.Println(a)
}

It says that n += 1 is ineffectual when it is very much effectual. Without it a is 0, with it it's 10. Same example on go playground here.

(Also, thanks for good linting in general, very useful for catching bugs. :) )

gordonklaus commented 3 years ago

I tried your playground example and got 10 both with and without n += 1. Which is to be expected; fallthrough always passes control to the next case block regardless of the next case condition.

glooms commented 3 years ago

Ah! Didn't know that, but it makes sense. I guess there was a bug in my code after-all then ^^ Apologies, and thanks! :)

I'll close this issue