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

unused linter in golangci-lint reports blank (_) identifier as unused for function parameters #1568

Open
kpitt opened this issue Jun 26, 2024 · 0 comments
Labels
false-positive needs-triage Newly filed issue that needs triage

Comments

@kpitt
Copy link

kpitt commented Jun 26, 2024

The unused linter accepts an unnamed function parameter as used. However, if any parameter for a function is named, then all of the parameters must be named, so it is not possible to leave a parameter unnamed if the function accepts multiple parameters and only one parameter is ignored. This can happen, for example, if an interface method takes several parameters, but some implementations of the method don't need all of the parameters. The typical way to address this is to name the unused parameters with the _ identifier to indicate that they are ignored, but the unused linter reports the _ parameters as unused when the ParametersAreUsed option is false.

I did not see this issue in earlier versions, so it may be a new behavior since the AST-based rewrite. This issue is closely related to #1567.

Note that I am running this through golangci-lint because I couldn't figure out a way to configure the ParametersAreUsed option when calling staticcheck directly. The version of golangci-lint I am using has a dependency on the honnef.co/go/tools v0.4.7 module.

Lint Command Output

$ golangci-lint run -c golangci.yml main.go
main.go:14:30: var `_` is unused (unused)
func (*StringIgnorer) Repeat(_ string, count int) {
                             ^

Test Code

main.go Source Code

package main

import "fmt"

type Repeater interface {
	// Repeat prints a string multiple times.
	// The first param is the string to print.
	// The second param is the number of times to print it.
	Repeat(string, int)
}

type StringIgnorer struct{}

func (*StringIgnorer) Repeat(_ string, count int) {
	for i := 0; i < count; i++ {
		fmt.Printf("%d: string arg is ignored\n", i+1)
	}
}

func main() {
	var r Repeater = &StringIgnorer{}
	r.Repeat("argument string", 10)
}

Declaring the Repeat implementation as Repeat(string, count int) causes the Go compiler to try to process string and count as two parameter names that are both of type int, which produces the following compile error when trying to run the program:

$ go run main.go                           
# command-line-arguments
./main.go:21:19: cannot use &StringIgnorer{} (value of type *StringIgnorer) as Repeater value in variable declaration: *StringIgnorer does not implement Repeater (wrong type for method Repeat)
		have Repeat(int, int)
		want Repeat(string, int)

golangci-list Config

linters:
  disable-all: true
  enable:
    - unused

linters-settings:
  unused:
    parameters-are-used: false

Environment Info

$ golangci-lint version
golangci-lint has version v1.59.1 built with go1.22.3 from (unknown, modified: ?, mod sum: "h1:CRRLu1JbhK5avLABFJ/OHVSQ0Ie5c4ulsOId1h3TTks=") on (unknown)

$ go version
go version go1.22.3 darwin/arm64

$ go env
GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/kenny/Library/Caches/go-build'
GOENV='/Users/kenny/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/kenny/.go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/kenny/.go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.22.3/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.22.3/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.22.3'
GCCGO='gccgo'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/tmp/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 -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/n9/s58k73qj3vg713wlsv2tr3nh0000gn/T/go-build1263784959=/tmp/go-build -gno-record-gcc-switches -fno-common'
@kpitt kpitt added false-positive needs-triage Newly filed issue that needs triage labels Jun 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
false-positive needs-triage Newly filed issue that needs triage
Projects
None yet
Development

No branches or pull requests

1 participant