Skip to content

cmd/fix,x/tools/go/analysis/passes/inline: adds unnecessary import for argument type #77610

@ddddddO

Description

@ddddddO

Go version

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

What did you do?

go:fix inline The package for the function argument marked in the comment was imported unnecessarily. Is this by design?
Here is a sample.

$ tree
.
├── go.mod
├── internal
│   └── a.go
└── main.go

2 directories, 3 files

internal/a.go

package internal

import (
	"io"
)

//go:fix inline
func OldHello(w io.Writer) error {
	return NewHello(w)
}

func NewHello(w io.Writer) error {
	_, err := w.Write([]byte("Hello, World!"))
	return err
}

main.go

package main

import (
	"strings"

	"github.com/ddddddO/sample/internal"
)

func main() {
	var buf strings.Builder
	internal.OldHello(&buf)
}

Assuming the above files exist, run go fix.

$ go fix
$ git diff
diff --git a/main.go b/main.go
index 4785a24..bdce14d 100644
--- a/main.go
+++ b/main.go
@@ -1,6 +1,7 @@
 package main
 
 import (
+       "io"
        "strings"
 
        "github.com/ddddddO/sample/internal"
@@ -8,5 +9,5 @@ import (
 
 func main() {
        var buf strings.Builder
-       internal.OldHello(&buf)
+       internal.NewHello(&buf)
 }

In this way, the “io” package is being imported even though it is not needed in main.

What did you see happen?

Omitted

What did you expect to see?

After running go fix, I expected there to be no unnecessary imports.

Metadata

Metadata

Assignees

Labels

BugReportIssues describing a possible bug in the Go implementation.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