Skip to content

x/tools/go/analysis/passes/modernize: stringsbuilder breaks code when GenDecl is a block declaration #76934

@rbrady98

Description

@rbrady98

Go version

go version go1.25.4 darwin/arm64

Output of go env in your module/workspace:

AR='ar'
CC='cc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='c++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/Users/bian/Library/Caches/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/Users/bian/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/dt/w56r82l955v2r8yndkc5p2lc0000gn/T/go-build2386555638=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD='/Users/bian/tmp/go.mod'
GOMODCACHE='/Users/bian/go/pkg/mod'
GOOS='darwin'
GOPATH='/Users/bian/go'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.25.4/libexec'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/bian/Library/Application Support/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.25.4/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.25.4'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

Consider this example

package main

import "fmt"

func main() {
	var (
		str = "hello world"
	)

	for range 100 {
		str += "!"
	}

	fmt.Println(str)
}

What did you see happen?

Output from running go run golang.org/x/tools/go/analysis/passes/modernize/cmd/modernize@latest -fix ./... results in non compiling code

package main

import "strings"

import "fmt"

func main() {
	var (
		str strings.Builder; str.WriteString("hello world"
	))

	for range 100 {
		str .WriteString("!")
	}

	fmt.Println(str.String())
}

What did you expect to see?

I expected to see something like

package main

import "strings"

import "fmt"

func main() {
	var (
		str strings.Builder
	)
	str.WriteString("hello world")

	for range 100 {
		str.WriteString("!")
	}

	fmt.Println(str.String())
}

where the initial expression is appended after the declaration block.

I would like to open an issue to fix this bug if I could

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.ToolsThis label describes issues relating to any tools in the x/tools repository.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions