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

cmd/gofmt: inconsistent behavior on removing unnecessary type names with -s arg #64966

Open
fishy opened this issue Jan 4, 2024 · 1 comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@fishy
Copy link

fishy commented Jan 4, 2024

Go version

go version go1.21.5 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/fishy/.cache/go-build'
GOENV='/home/fishy/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/fishy/.gopath/pkg/mod'
GONOPROXY='omitted'
GONOSUMDB='omitted'
GOOS='linux'
GOPATH='/home/fishy/.gopath'
GOPRIVATE='omitted'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/lib/go-1.21'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/lib/go-1.21/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.21.5'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/fishy/test/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1401412523=/tmp/go-build -gno-record-gcc-switches'

What did you do?

main.go:

package main

import (
	"fmt"
)

type Data struct {
	Foo int
}

type Response struct {
	Item  []*Data
	Items [][]*Data
}

var s = Response{
	Item: []*Data{
		&Data{
			Foo: 1,
		},
	},
	Items: [][]*Data{
		{
			&Data{
				Foo: 2,
			},
			&Data{
				Foo: 3,
			},
		},
	},
}

func main() {
	fmt.Println(s)
}

gofmt -s -d main.go will only suggest removing &Data from Item, but not from Items:

$ gofmt -s -d main.go
diff main.go.orig main.go
--- main.go.orig
+++ main.go
@@ -15,7 +15,7 @@
 
 var s = Response{
        Item: []*Data{
-               &Data{
+               {
                        Foo: 1,
                },
        },

But removing both are perfectly fine: https://go.dev/play/p/pEfXLBwuwL1

What did you see happen?

gofmt -s is inconsistent as it fail to suggest the same thing when it's under two or more layers of slices.

What did you expect to see?

gofmt -s being consistent.

@dmitshur dmitshur changed the title cmd/gofmt: Inconsistent behavior on removing unnecessary type names with -s arg cmd/gofmt: inconsistent behavior on removing unnecessary type names with -s arg Jan 9, 2024
@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 9, 2024
@dmitshur dmitshur added this to the Backlog milestone Jan 9, 2024
@dmitshur
Copy link
Contributor

dmitshur commented Jan 9, 2024

CC @griesemer, @mvdan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

2 participants