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/internal/analysis/modernize: minmax removes important comment #72727

Closed
sbinet opened this issue Mar 7, 2025 · 4 comments
Closed
Labels
BugReport Issues describing a possible bug in the Go implementation. gopls Issues related to the Go language server, gopls. ToolProposal Issues describing a requested change to a Go tool or command-line program. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@sbinet
Copy link
Member

sbinet commented Mar 7, 2025

Go version

go version go1.24.1 linux/amd64

Output of go env in your module/workspace:

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/binet/work/gonum/bin'
GOCACHE='/home/binet/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/binet/.config/go/env'
GOEXE=''
GOEXPERIMENT='rangefunc'
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3343970528=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/home/binet/work/gonum/src/gonum.org/v1/gonum/go.mod'
GOMODCACHE='/home/binet/work/gonum/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/binet/work/gonum'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/binet/sdk/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/binet/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/binet/sdk/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.24.1'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

I ran the following command to modernize a Go code base:

$> go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...

What did you see happen?

it modernized code but removed some comment that explained stuff.

ie: on the following code:

package modernize

var CutOff float64

func F(v float64) float64 {
	o := v - 42
	// some important comment. DO NOT REMOVE.
	if o < CutOff {
		o = CutOff
	}
	return o
}

it generated the following diff:

diff --git a/old.go b/old.go
index f8d7b3d..7758b71 100644
--- a/old.go
+++ b/old.go
@@ -3,10 +3,6 @@ package modernize
 var CutOff float64
 
 func F(v float64) float64 {
-       o := v - 42
-       // some important comment. DO NOT REMOVE.
-       if o < CutOff {
-               o = CutOff
-       }
+       o := max(v-42, CutOff)
        return o
 }

What did you expect to see?

the comment should have been kept:

diff --git a/old.go b/old.go
index f8d7b3d..a67cbc1 100644
--- a/old.go
+++ b/old.go
@@ -3,10 +3,7 @@ package modernize
 var CutOff float64
 
 func F(v float64) float64 {
-       o := v - 42
        // some important comment. DO NOT REMOVE.
-       if o < CutOff {
-               o = CutOff
-       }
+       o := max(v-42, CutOff)
        return o
 }
@gopherbot gopherbot added Tools This label describes issues relating to any tools in the x/tools repository. gopls Issues related to the Go language server, gopls. labels Mar 7, 2025
@gopherbot gopherbot added this to the Unreleased milestone Mar 7, 2025
@gabyhelp gabyhelp added the ToolProposal Issues describing a requested change to a Go tool or command-line program. label Mar 7, 2025
@adonovan
Copy link
Member

adonovan commented Mar 7, 2025

We should probably preserve all comments when replacing a block with a function call, even when that means moving them to places where they no longer make perfect sense.

@adonovan adonovan changed the title x/tools/gopls/internal/analysis/modernize/cmd/modernize: removes important comment when modernizing code x/tools/gopls/internal/analysis/modernize: minmax removes important comment Mar 10, 2025
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/656655 mentions this issue: gopls/internal/analysis/modernize: preserves comments in minmax

@nightlyone
Copy link
Contributor

I am not sure it makes sense to rewrite to min/max in those cases. The comments may be talking about an else branch or something like this. It probably stays more readable, when not rewritten.

I would flag that code as should be changed, but not automatically rewrite it.

@h9jiang h9jiang modified the milestones: Unreleased, gopls/v0.19.0 Mar 13, 2025
@h9jiang h9jiang added the BugReport Issues describing a possible bug in the Go implementation. label Mar 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BugReport Issues describing a possible bug in the Go implementation. gopls Issues related to the Go language server, gopls. ToolProposal Issues describing a requested change to a Go tool or command-line program. 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