gordonklaus / ineffassign

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

fails to flag ineffectual reassignment after usage of waitgroups/errorgroups #81

Closed wkalt closed 1 year ago

wkalt commented 1 year ago

This program ineffectually reassigns a value after modification within a waitgroup:

func test() error {
    var sharedValue int
    mtx := &sync.Mutex{}
    wg := &sync.WaitGroup{}
    for i := 0; i < 3; i++ {
        wg.Add(1)
        go func() {
            mtx.Lock()
            sharedValue++
            mtx.Unlock()
            wg.Done()
        }()
    }

    wg.Wait()

    // should be flagged
    sharedValue = 0

    return nil
}

The commented reassignment seems like it should be flagged. I suspect the rationale for not flagging it may be that it is modified within a goroutine, and therefor could be reused. However due to the usage of a waitgroup, this assignment is in fact ineffectual.

The same issue can exist with errgroups and with goroutines coordinated over a channel. Is this a bug?

gordonklaus commented 1 year ago

You are correct: Any variable accessed in another goroutine is thereafter ignored by the tool. In general, it may be arbitrarily complex to determine in what order such a variable is accessed.