Skip to content

x/tools/gopls: Extract function not handle anonymous functions correctly #64821

Closed
@rogeryk

Description

@rogeryk

gopls version

v14.2

go env

unimportant

What did you do?

In the following example, select all main function body, then toggle "Extract function"

func main() {
	var s []string
	slices.SortFunc(s, func(a, b string) int {
		return cmp.Compare(a, b)
	})
	println(s)
}

What did you expect to see?

func main() {
	newFunction()
}

func newFunction() {
	var s []string
	slices.SortFunc(s, func(a, b string) int {
		return cmp.Compare(a, b)
	})
	println(s)
}

What did you see instead?

func main() {
	shouldReturn := newFunction()
	if shouldReturn {
		return
	}
}

func newFunction() bool {
	var s []string
	slices.SortFunc(s, func(a, b string) int {
		return true, cmp.Compare(a, b)
	})
	println(s)
	return false
}

Editor and settings

No response

Logs

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    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

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions