golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
120.1k stars 17.24k forks source link

x/tools/go/analysis/passes/unusedwrite: detect dead stores to local vars #67109

Open adonovan opened 2 weeks ago

adonovan commented 2 weeks ago

During review of https://go.dev/cl/581555, @timothy-king pointed out that I had defined a local variable using an if/else statement and then failed to use it after the statement, something like this:

 func f(pass *Pass), filename string) {
    readFile := pass.ReadFile
    if readFile == nil {
        readFile = os.ReadFile
    }
    os.ReadFile(filename) // oops, I meant readFile
 }

The assignment to readFile is a dead store. We should flag this is a likely error.