Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rogeryk opened this issue Dec 20, 2023 · 1 comment
Open
Labels
gopls Issues related to the Go language server, gopls. Refactoring Issues related to refactoring tools Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@rogeryk
Copy link
Contributor

rogeryk commented Dec 20, 2023

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

@rogeryk rogeryk added gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository. labels Dec 20, 2023
@gopherbot gopherbot added this to the Unreleased milestone Dec 20, 2023
@suzmue suzmue added the Refactoring Issues related to refactoring tools label Dec 21, 2023
@suzmue suzmue modified the milestones: Unreleased, gopls/backlog Dec 21, 2023
@adonovan
Copy link
Member

adonovan commented Dec 21, 2023

The bad news is that "extract function" has a lot of serious bugs that make it a use-at-your-own-peril sort of feature: notably, it discards all comments in the code that migrates. The good news is that it is a priority for our team in 2024 to improve all of our refactoring tools.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls Issues related to the Go language server, gopls. Refactoring Issues related to refactoring tools Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants