gordonklaus / ineffassign

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

inconsistent behavior with bool initialization #35

Closed tendervittles closed 5 years ago

tendervittles commented 5 years ago

This tool has allowed me to slay many a bug today. Thank you!!!

I also noticed this inconsistent behavior.

package main

import "fmt"

func main() {

    s := ""
    s = "hello"
    fmt.Println("s:", s)

    n := 0
    n = 42
    fmt.Println("n:", n)

    b := false
    b = true
    fmt.Println("b:", b)
}

I can init and update s and n as expected, but I'm getting ineffectual assignment to b.

I am running go1.12.5.

Thanks!

jvmatl commented 5 years ago

So, if I understand correctly, you're saying you believe booleans are being treated differently, because in all three of your test cases, you have a 'short variable declaration' that explicitly initializes to the zero value for its type, followed by an assignment that sets the variable to a non-zero value, but in the case of a boolean, the initialization to its zero-value (false) is flagged as an ineffectual assignment, whereas the other two short declarations are treated as if they had been declared uninitialized (e.g. var s string )?

tendervittles commented 5 years ago

@jvmatl

Thanks!