go-tools icon indicating copy to clipboard operation
go-tools copied to clipboard

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

Open hkeide opened this issue 1 year ago • 0 comments

$ 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/[email protected] (sum: h1:4bH5o3b5ZULQ4UrBmP+63W9r7qIkqJClEA9ko5YKx+I=)
Dependencies:
	github.com/BurntSushi/[email protected] (sum: h1:pxW6RcqyfI9/kWtOwnv/G+AzdKuy2ZrqINhenH4HyNs=)
	golang.org/x/exp/[email protected] (sum: h1:1P7xPZEwZMoBoz0Yze5Nx2/4pxj6nw9ZqHWXqP0iRgQ=)
	golang.org/x/[email protected] (sum: h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=)
	golang.org/x/[email protected] (sum: h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=)
	golang.org/x/[email protected] (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.

hkeide avatar Oct 06 '24 19:10 hkeide