Skip to content

x/tools/gopls/internal/analysis/modernize: minmax removes important comment #72727

Closed
@sbinet

Description

@sbinet

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
 }

Activity

added
ToolsThis label describes issues relating to any tools in the x/tools repository.
goplsIssues related to the Go language server, gopls.
on Mar 7, 2025
added this to the Unreleased milestone on Mar 7, 2025
added
ToolProposalIssues describing a requested change to a Go tool or command-line program.
on Mar 7, 2025
adonovan

adonovan commented on Mar 7, 2025

@adonovan
Member

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.

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[/+] on Mar 10, 2025
gopherbot

gopherbot commented on Mar 11, 2025

@gopherbot
Contributor

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

nightlyone

nightlyone commented on Mar 11, 2025

@nightlyone
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.

added
BugReportIssues describing a possible bug in the Go implementation.
on Mar 13, 2025
added theissue type on Mar 13, 2025

3 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.ToolProposalIssues describing a requested change to a Go tool or command-line program.ToolsThis label describes issues relating to any tools in the x/tools repository.goplsIssues related to the Go language server, gopls.

    Type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @sbinet@nightlyone@adonovan@gopherbot@h9jiang

        Issue actions

          x/tools/gopls/internal/analysis/modernize: minmax removes important comment · Issue #72727 · golang/go