Skip to content

x/tools/gopls: unusedparams analyser reports issues in generated files #71481

Closed
@samsalisbury

Description

@samsalisbury

gopls version

Build info
----------
golang.org/x/tools/gopls v0.17.1
    golang.org/x/tools/gopls@v0.17.1 h1:Mt/DSfnnSe3dyf6MH/dZZ0iww+viHNhAFc4rEYDiOAw=
    github.com/BurntSushi/toml@v1.4.1-0.20240526193622-a339e1f7089c h1:pxW6RcqyfI9/kWtOwnv/G+AzdKuy2ZrqINhenH4HyNs=
    github.com/google/go-cmp@v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
    golang.org/x/exp/typeparams@v0.0.0-20231108232855-2478ac86f678 h1:1P7xPZEwZMoBoz0Yze5Nx2/4pxj6nw9ZqHWXqP0iRgQ=
    golang.org/x/mod@v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
    golang.org/x/sync@v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
    golang.org/x/telemetry@v0.0.0-20241106142447-58a1122356f5 h1:TCDqnvbBsFapViksHcHySl/sW4+rTGNIAoJJesHRuMM=
    golang.org/x/text@v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
    golang.org/x/tools@v0.27.1-0.20241219162658-575221bfbda3 h1:kgwdasJRsdDWYgWcEgMF424DiXwwXHSb3V8xVTi//i8=
    golang.org/x/vuln@v1.0.4 h1:SP0mPeg2PmGCu03V+61EcQiOjmpri2XijexKdzv8Z1I=
    honnef.co/go/tools@v0.5.1 h1:4bH5o3b5ZULQ4UrBmP+63W9r7qIkqJClEA9ko5YKx+I=
    mvdan.cc/gofumpt@v0.7.0 h1:bg91ttqXmi9y2xawvkuMXyvAA/1ZGJqYAEGjXuP0JXU=
    mvdan.cc/xurls/v2@v2.5.0 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/toolchain@v0.0.1-go1.23.4.darwin-arm64'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/sam/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.23.4.darwin-arm64/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

Activity

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

gopherbot commented on Jan 30, 2025

@gopherbot
Contributor

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

findleyr

findleyr commented on Jan 30, 2025

@findleyr
Member

Thanks. For the issue, and fix!

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

samsalisbury

samsalisbury commented on Jan 30, 2025

@samsalisbury
Author

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?

added a commit that references this issue on Feb 4, 2025
0556adb
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

    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

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @samsalisbury@gopherbot@findleyr@gabyhelp

        Issue actions

          x/tools/gopls: unusedparams analyser reports issues in generated files · Issue #71481 · golang/go