Skip to content

False positive "this value of x is never used (SA4006)", related to iter and break #1654

@SimonMTS

Description

@SimonMTS

The following code causes an incorrect this value of result is never used (SA4006), when running staticcheck ./...

package main

import "iter"

type Thing struct{}

func (Thing) Iter() iter.Seq[int] {
	return func(yield func(int) bool) {
	}
}

func main() {
	result := 123
	println(result) // 123
	for range 1 {
		num := 987

		for range (Thing{}).Iter() {
		}

		result = num // incorrect: this value of result is never used (SA4006)
		break
	}

	println(result) // 987
}

Removing the break or the .Iter() loop fixes the false positive.

Versions

$ staticcheck -debug.version
staticcheck 2025.1.1 (0.6.1)

Compiled with Go version: go1.24.3
Main module:
        honnef.co/go/tools@v0.6.1 (sum: h1:R094WgE8K4JirYjBaOpz/AvTyUu/3wbmAoskKN/pxTI=)
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.23.0 (sum: h1:Zb7khfcRGKk+kqfxFaP5tZqCnDZMjC5VtUBs87Hr6QM=)
        golang.org/x/sync@v0.11.0 (sum: h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=)
        golang.org/x/tools@v0.30.0 (sum: h1:BgcpHewrV5AUp2G9MebG4XPFI1E2W41zU1SaqVA9vJY=)
$ go version
go version go1.24.3 linux/amd64

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions