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: unusedparams analyser reports issues in generated files #71481

Open
samsalisbury opened this issue Jan 30, 2025 · 4 comments
Open
Labels
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

@samsalisbury
Copy link

samsalisbury commented Jan 30, 2025

gopls version

Build info
----------
golang.org/x/tools/gopls v0.17.1
    golang.org/x/tools/[email protected] h1:Mt/DSfnnSe3dyf6MH/dZZ0iww+viHNhAFc4rEYDiOAw=
    github.com/BurntSushi/[email protected] h1:pxW6RcqyfI9/kWtOwnv/G+AzdKuy2ZrqINhenH4HyNs=
    github.com/google/[email protected] h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
    golang.org/x/exp/[email protected] h1:1P7xPZEwZMoBoz0Yze5Nx2/4pxj6nw9ZqHWXqP0iRgQ=
    golang.org/x/[email protected] h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
    golang.org/x/[email protected] h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
    golang.org/x/[email protected] h1:TCDqnvbBsFapViksHcHySl/sW4+rTGNIAoJJesHRuMM=
    golang.org/x/[email protected] h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
    golang.org/x/[email protected] h1:kgwdasJRsdDWYgWcEgMF424DiXwwXHSb3V8xVTi//i8=
    golang.org/x/[email protected] h1:SP0mPeg2PmGCu03V+61EcQiOjmpri2XijexKdzv8Z1I=
    honnef.co/go/[email protected] h1:4bH5o3b5ZULQ4UrBmP+63W9r7qIkqJClEA9ko5YKx+I=
    mvdan.cc/[email protected] h1:bg91ttqXmi9y2xawvkuMXyvAA/1ZGJqYAEGjXuP0JXU=
    mvdan.cc/xurls/[email protected] h1:lyBNOm8Wo71UknhUs4QTFUNNMyxy2JEIaKKo0RWOh+8=
go: go1.23.2

go env

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/sam/Library/Caches/go-build'
GOENV='/Users/sam/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/sam/go/pkg/mod'
GONOPROXY='*'
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/sam/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Users/sam/go/pkg/mod/golang.org/[email protected]'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/sam/go/pkg/mod/golang.org/[email protected]/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.23.4'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/sam/Library/Application Support/go/telemetry'
GCCGO='gccgo'
GOARM64='v8.0'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/sam/src/github.com/golang/tools/gopls/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 -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/4w/vnc5zpq10yg0_wx7968v98340000gq/T/go-build43648965=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

Run gopls check:

$ gopls check ./generatedfile.go

Contents of generatedfile.go:

// Code generated by some-tool. DO NOT EDIT.

package main

func x(v any) {
	print("")
}

What did you see happen?

The unusedparam analyzer reported a problem:

$ gopls check generatedfile.go
/Users/sam/src/github.com/samsalisbury/unusedparams-demo/generatedfile.go:5:8-13: unused parameter: v

What did you expect to see?

I expected this problem not to be reported since the file is generated, therefore we cannot edit the file to fix it.

Note: This is inconsistent with the quickfix for this problem, which correctly does not run because the file is generated:

$ gopls codeaction -kind quickfix -exec -d ./generatedfile.go
gopls: no matching code action at /Users/sam/src/github.com/samsalisbury/unusedparams-demo/generatedfile.go

If I strip the DO NOT EDIT comment from the file, the quickfix renames v to _.

Editor and settings

Not relevant, this involves directly invoking gopls check

Logs

No response

@samsalisbury samsalisbury added gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository. labels Jan 30, 2025
@gopherbot gopherbot added this to the Unreleased milestone Jan 30, 2025
@gabyhelp gabyhelp added the ToolProposal Issues describing a requested change to a Go tool or command-line program. label Jan 30, 2025
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/645575 mentions this issue: gopls: skip unusedparam for generated files

@findleyr
Copy link
Member

Thanks. For the issue, and fix!

Agree that we should suppress these diagnostics. Will review your CL.

@findleyr findleyr modified the milestones: Unreleased, gopls/v0.18.0 Jan 30, 2025
@samsalisbury
Copy link
Author

samsalisbury commented Jan 30, 2025

No problem, thanks for the quick review @findleyr.

Thinking about this some more, it seems that most analyzers shouldn't run on generated files, since quickfixes can't be applied, so the only interesting analysis is whether or not the file breaks compilation. However when you're working on codegen itself, these analyzers might be useful to validate the generated code. So excluding them wholesale for generated files doesn't seem quite right. Likewise might there be instances when we want to run quick fixes on generated code to eliminate noise from e.g. unused parameter names? Generated code does end up being read by humans sometimes...

I like the idea of this not being configurable, for simplicity, but part of me thinks that there should be an option to analyse generated files, and be able to apply quickfixes to them as well, when appropriate.

I'm not suggesting I'll try to make this change now, but I'm thinking about opening a separate issue to track this idea, unless there are obvious objections that I'm not seeing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

4 participants