Skip to content

cmd/gofmt: -r strips some comments when pattern matching #57574

@rski

Description

@rski

What version of Go are you using (go version)?

$ go version
rski@rski ~/C/g/go (master)> go version                                                                  
go version go1.19.4 linux/amd64

Does this issue reproduce with the latest release?

yup, tried gofmt built from 9955a7e

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/rski/.cache/go-build"
GOENV="/home/rski/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/rski/Code/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/rski/Code/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/nix/store/a7875alzpnr46z6mv4ssymfdwmvr6xbq-go-1.19.4/share/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/nix/store/a7875alzpnr46z6mv4ssymfdwmvr6xbq-go-1.19.4/share/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19.4"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build158369519=/tmp/go-build -gno-record-gcc-switches"

What did you do?

input file:

package main

import "errors"

func errorWithOneArg1(a func() error, v int) error { return a() }
func errorWithOneArg2(a func() error) error        { return a() }

func layer(f func() error, s string) error { return f() }

func anotherLayer() error {
	return layer(
		func() error {
			return errorWithOneArg1(func() error {
				// This comment is erased
				return errors.New("abc")
			}, 30)
		}, "test")
}

command and result:

gofmt -r 'errorWithOneArg1(a,b) -> errorWithOneArg2(a)' test.go                      
package main

import (
	"errors"
)

func errorWithOneArg1(a func() error, v int) error { return a() }
func errorWithOneArg2(a func() error) error        { return a() }

func layer(f func() error, s string) error { return f() }

func anotherLayer() error {
	return layer(
		func() error {
			return errorWithOneArg2(func() error {

				return errors.New("abc")
			})

		}, "test")
}

Note how the comment was replaced with a blank line.

This doesn't happen when not doing matching i.e.

gofmt -r 'errorWithOneArg1 -> errorWithOneArg2' test.go  | grep -En "return errorWith|com
ment"
15:			return errorWithOneArg2(func() error {
16:				// This comment is erased

the code is rewritten and the comment is kept

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions