Skip to content

x/tools/gopls: Extract Function: shouldReturn appears at wrong index of ReturnStmt.Results #75527

@fatanugraha

Description

@fatanugraha

gopls version

golang.org/x/tools/gopls v0.0.0-20250917170932-b71b35e896b5+dirty
golang.org/x/tools/[email protected]+dirty
github.com/BurntSushi/[email protected] h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
github.com/fatih/[email protected] h1:hxNvNX/xYBp0ovncs8WyWZrOrpBNub/JfaMvbURyft8=
github.com/fatih/[email protected] h1:dDSgAjoOMp8da3egfz0t2S+t8RGOpEmEXZubcGuc0Bg=
github.com/fatih/[email protected] h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
github.com/fsnotify/[email protected] h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
github.com/google/[email protected] h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/[email protected] h1:dkP3B96OtZKKFvdrUSaDkL+YDx8Uw9uC4Y+eukpCnmM=
github.com/modelcontextprotocol/[email protected] h1:WXRHx/4l5LF5MZboeIJYn7PMFCrMNduGGVapYWFgrF8=
github.com/yosida95/uritemplate/[email protected] h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4=
golang.org/x/exp/[email protected] h1:GU1ttDuJS89SePnuEsEuLj7dMMFP2JkGsDV1Z51iDXo=
golang.org/x/[email protected] h1:gQBtGhjxykdjY9YhZpSlZIsbnaE2+PgjfLWUQTnoZ1U=
golang.org/x/[email protected] h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
golang.org/x/[email protected] h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k=
golang.org/x/[email protected] h1:dHQOQddU4YHS5gY33/6klKjq7Gp3WwMyOXGNp5nzRj8=
golang.org/x/[email protected] h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk=
golang.org/x/[email protected] => ../
golang.org/x/[email protected] h1:Ju8QsuyhX3Hk8ma3CesTbO8vfJD9EvUBgHvkxHBzj0I=
honnef.co/go/[email protected] h1:fj8r9irJSpolAGUdZBxJIRY3lLc4jH2Dt4lwnWyWwpw=
mvdan.cc/[email protected] h1:nZUCeC2ViFaerTcYKstMmfysj6uhQrA2vJe+2vwGU6k=
mvdan.cc/xurls/[email protected] h1:3NTZpeTxYVWNSokW3MKeyVkz/j7uYXYiMtXRUfmjbgI=
go: go1.25.0

go env

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN='/home/fata/.local/share/mise/installs/go/1.24.6/bin'
GOCACHE='/home/fata/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/fata/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1253172891=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/home/fata/tools/gopls/go.mod'
GOMODCACHE='/home/fata/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/fata/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/fata/go/pkg/mod/golang.org/[email protected]'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/fata/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/fata/go/pkg/mod/golang.org/[email protected]/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.25.0'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

Extract function generates incorrect code when the selection has nested return

func doSomething() string {
	// selection starts -> run extract function code action
	y := 10
	if true {
		return "hi"
	}
	// selection ends

	return fmt.Sprintf("%d", y)
}

What did you see happen?

func doSomething() string {
	// selection starts
	y, s, shouldReturn := newFunction()
	if shouldReturn {
		return s
	}
	// selection ends

	return fmt.Sprintf("%d", y)
}

func newFunction() (int, string, bool) {
	y := 10
	if true {
		return 0, true, "hi"
	}
	return y, "", false
}

notice that the shouldReturn bool value is misplaced for the nested return while the non-nested one is correct

What did you expect to see?

func doSomething() string {
	// selection starts
	y, s, shouldReturn := newFunction()
	if shouldReturn {
		return s
	}
	// selection ends

	return fmt.Sprintf("%d", y)
}

func newFunction() (int, string, bool) {
	y := 10
	if true {
		return 0, "hi", true
	}
	return y, "", false
}

Editor and settings

VSCode

settings.json

    // go
    "go.formatTool": "gofumpt"

Logs

Info - 9:44:56 PM] 2025/09/18 21:44:56 Created View (#1)
directory=/home/fata/tools
view_type="GoMod"
root_dir="file:///home/fata/tools"
go_version="go version go1.24.6 linux/amd64"
build_flags=[]
env={GOOS:linux GOARCH:amd64 GOCACHE:/home/fata/.cache/go-build GOMODCACHE:/home/fata/go/pkg/mod GOPATH:/home/fata/go GOPRIVATE: GOFLAGS: GO111MODULE: GOTOOLCHAIN:auto GOROOT:/home/fata/.local/share/mise/installs/go/1.24.6 GoVersion:24 GoVersionOutput:go version go1.24.6 linux/amd64
ExplicitGOWORK: EffectiveGOPACKAGESDRIVER:}
env_overlay=[]

Request to stop language server - manual (enabled: true)[Info - 9:44:57 PM] 2025/09/18 21:44:57 Created View (#1)
directory=/home/fata/tools
view_type="GoMod"
root_dir="file:///home/fata/tools"
go_version="go version go1.24.6 linux/amd64"
build_flags=[]
env={GOOS:linux GOARCH:amd64 GOCACHE:/home/fata/.cache/go-build GOMODCACHE:/home/fata/go/pkg/mod GOPATH:/home/fata/go GOPRIVATE: GOFLAGS: GO111MODULE: GOTOOLCHAIN:auto GOROOT:/home/fata/.local/share/mise/installs/go/1.24.6 GoVersion:24 GoVersionOutput:go version go1.24.6 linux/amd64
ExplicitGOWORK: EffectiveGOPACKAGESDRIVER:}
env_overlay=[]

[Info - 9:44:58 PM] 2025/09/18 21:44:58 go/packages.Load #1
view_id="1"
snapshot=0
directory=/home/fata/tools
query=[/home/fata/tools/... builtin]
packages=527
duration=1.360005048s

[Info - 9:44:58 PM] 2025/09/18 21:44:58 go/packages.Load #1
view_id="1"
snapshot=0
directory=/home/fata/tools
query=[/home/fata/tools/... builtin]
packages=527
duration=1.807376264s

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.RefactoringIssues related to refactoring toolsToolsThis 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

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions