Skip to content

x/tools/gopls/internal/analysis/modernize: minmax doesn't work with return statements #72830

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

Open
dominikh opened this issue Mar 12, 2025 · 2 comments
Labels
FeatureRequest Issues asking for a new feature that does not need a proposal. gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@dominikh
Copy link
Member

dominikh commented Mar 12, 2025

gopls isn't emitting any diagnostics for the following code:

func Mymin(a, b int) int {
	if a < b {
		return a
	} else {
		return b
	}
}

but it could suggest replacing the if/else with return min(a, b).

@gopherbot gopherbot added the gopls Issues related to the Go language server, gopls. label Mar 12, 2025
@gabyhelp gabyhelp added the FeatureRequest Issues asking for a new feature that does not need a proposal. label Mar 12, 2025
@seankhliao seankhliao changed the title gopls/internal/analysis/modernize: minmax doesn't work with return statements x/tools/gopls/internal/analysis/modernize: minmax doesn't work with return statements Mar 12, 2025
@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Mar 12, 2025
@gopherbot gopherbot added this to the Unreleased milestone Mar 12, 2025
@Jorropo
Copy link
Member

Jorropo commented Mar 13, 2025

Also

func Mymin(a, b int) int {
	if a < b {
		return a
	}
	return b
}

@h9jiang h9jiang modified the milestones: Unreleased, Backlog Mar 13, 2025
@adonovan
Copy link
Member

I agree this would be a good feature, but it is surprisingly fiddly to get even these elementary transformations right. (Witness the number of issues filed about minmax.) I wish we had good abstractions for generalizing these patterns, so that minmax could share logic with the slicescontains modernizer, which handles a variety of control flow patterns including {return false} and {x=false; break}.

@adonovan adonovan modified the milestones: Backlog, gopls/backlog Apr 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest Issues asking for a new feature that does not need a proposal. gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

6 participants