Skip to content

x/tools/gopls: extract function produces invalid code #77240

@fatanugraha

Description

@fatanugraha

gopls version

golang.org/x/tools/gopls v0.0.0-20260120163254-613c127fdd11

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.25.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-build628802563=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/home/fata/Documents/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/.local/share/mise/installs/go/1.25.6'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/fata/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/fata/.local/share/mise/installs/go/1.25.6/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.25.6'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

func Fun(v2 int) (int, int, error) {
    // selection starts 
	switch v2 { 
	case 1:
		return doOne() 
	case 2:
		return doTwo()
	}
    // selection ends -- run extract function

	return 1, 3, nil
}

func doOne() (int, int, error) {
	return 0, 1, nil
}

func doTwo() (int, int, error) {
	return 0, 2, nil
}

What did you see happen?

func Fun(v2 int) (int, int, error) {
	i, i1, err, shouldReturn := newFunction(v2)
	if shouldReturn {
		return i, i1, err
	}

	return 1, 3, nil
}

func newFunction(v2 int) (int, int, error, bool) {
	switch v2 {
	case 1
		return doOne(), true  // [!] notice that this is invalid syntax
	case 2:
		return doTwo(), true  // [!] notice that this is invalid syntax
	}
	return 0, 0, nil, false
}

func doOne() (int, int, error) {
	return 0, 1, nil
}

func doTwo() (int, int, error) {
	return 0, 2, nil
}

What did you expect to see?

anything as long as it produces a valid Go code. this what i will do if i do it manually:

func Fun(v2 int) (int, int, error) {
	i, i1, err, shouldReturn := newFunction(v2)
	if shouldReturn {
		return i, i1, err
	} 

	return 1, 3, nil
}

func newFunction(v2 int) (int, int, error, bool) {
	switch v2 { 
	case 1:
		v1, v3, v4 := doOne()
		return v1, v3, v4, true
	case 2:
		v1, v3, v4 := doTwo()
		return v1, v3, v4, true
	}
	return 0, 0, nil, false
}

func doOne() (int, int, error) {
	return 0, 1, nil
}

func doTwo() (int, int, error) {
	return 0, 2, nil
}

Editor and settings

vscode with Go extension (vanilla)

Logs

No response

Metadata

Metadata

Assignees

Labels

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

Relationships

None yet

Development

No branches or pull requests

Issue actions