dominikh / go-tools

Staticcheck - The advanced Go linter
https://staticcheck.dev
MIT License
6.21k stars 377 forks source link

U1000 false positive if variable is used as part of initialization expression with side effects #1610

Open hkeide opened 1 month ago

hkeide commented 1 month ago
$ staticcheck -version
staticcheck 2024.1.1 (0.5.1)

$ staticcheck -debug.version
staticcheck 2024.1.1 (0.5.1)

Compiled with Go version: go1.23.0
Main module:
    honnef.co/go/tools@v0.5.1 (sum: h1:4bH5o3b5ZULQ4UrBmP+63W9r7qIkqJClEA9ko5YKx+I=)
Dependencies:
    github.com/BurntSushi/toml@v1.4.1-0.20240526193622-a339e1f7089c (sum: h1:pxW6RcqyfI9/kWtOwnv/G+AzdKuy2ZrqINhenH4HyNs=)
    golang.org/x/exp/typeparams@v0.0.0-20231108232855-2478ac86f678 (sum: h1:1P7xPZEwZMoBoz0Yze5Nx2/4pxj6nw9ZqHWXqP0iRgQ=)
    golang.org/x/mod@v0.17.0 (sum: h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=)
    golang.org/x/sync@v0.7.0 (sum: h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=)
    golang.org/x/tools@v0.21.1-0.20240531212143-b6235391adb3 (sum: h1:SHq4Rl+B7WvyM4XODon1LXtP7gcG49+7Jubt1gWWswY=)

$ go version
go version go1.23.0 darwin/arm64

$ staticcheck main.go
main.go:5:5: var integer is unused (U1000)
main.go:7:5: var ret is unused (U1000)
main.go:9:6: func f is unused (U1000)

$ go run main.go
[1]

main.go:

package main

import "fmt"

var integer = 1

var ret = f([]int{integer})

func f(slice []int) int {
    fmt.Println(slice)
    return 0
}

func main() {

}

Why it's wrong: the function f() is executed and has side effects, but its input and the function itself is marked as unused.