Skip to content

x/tools/gopls/internal/analysis/modernize: forvar do not fixes redeclaration of second var #74849

@skewb1k

Description

@skewb1k

Steps to reproduce

go.mod

module main

go 1.24.5 //1.22+

main.go

package main

func main() {
	for i, v := range []string{"a", "b", "c"} {
		i := i
		v := v
		println(i, v)
	}
}

Run

modernize -fix ./...

Only the first redeclaration is removed. The second remains:

for i, v := range []string{"a", "b", "c"} {
	v := v
	println(i, v)
}

And whats even worse, if you run the tool again, it removes the remaining redeclaration, leading to a different result on the second run. This is unexpected and unacceptable for a static analysis tool - it should be idempotent and consistent across runs.

This issue also affected the gopls source itself, which is how I discovered it. See this change:
https://go-review.googlesource.com/c/tools/+/665215/1/gopls/internal/golang/references.go

There is a test case that expects this behavior, but I believe it is flawed.
The correct behavior should be to remove both redundant redeclarations in a single pass.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.ToolsThis label describes issues relating to any tools in the x/tools repository.goplsIssues related to the Go language server, gopls.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions