To detect unused func, I enable only unused linter. However, there are many detections func TestXXXX is unused (unused)
I expect any tests are used and wanna skip any TestXXX func from unused.
golangci-lint --version (or git commit if you don't use binary distribution)
cat .golangci.ymlrun:
deadline: 10m
build-tags:
- integration
skip-dirs:
- vendor
linters-settings:
misspell:
locale: US
unused:
check-exported: true
linters:
disable-all: true
enable:
- unused
presets:
- format
- unused
fast: false
go version && go env$ go version go version go1.13.1 darwin/amd64
$ go env GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/myname/Library/Caches/go-build"
GOENV="/Users/myname/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/myname/work/go"
GOPRIVATE=""
GOPROXY="direct"
GOROOT="/usr/local/Cellar/go/1.13.1/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.13.1/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/myname/work/go/src/github.com/myproject/repository/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/9n/zjbb8_mx08nc4q3q9q6bck916qy5dd/T/go-build706206117=/tmp/go-build -gno-record-gcc-switches -fno-common"
@hiromaily This sounds like a possible bug but I think we might need a bit more details to reproduce. Would it be possible to provide a minimal example test.go file and the golangci-lint command line you are using that triggers the incorrect behavior?
@tpounds
One of func which is detected as unused is
func TestHealthChecker_Check_Timeout(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(1*time.Second))
defer cancel()
hc := NewHealthChecker(&config.Root{}, heavyCheck)
err := hc.Check(ctx)
assert.EqualError(t, err, context.DeadlineExceeded.Error())
}
run command line is
golangci-lint run
Not only Test func, there are many unexposed funcs and types are detected as unused.
We are seeing this problem with versions 1.19.0 and above. No false positives for unused test functions with 1.18.0, tested with go 1.13.
@SimonT90poe
Is it regresssion error?
Is it regresssion error? Anyway it should be worked on latest version is 1.21.
Every version between 1.19 and 1.21 inclusive exhibits this behaviour.
It was a surprise that TestMain() and most of the test functions it executed are considered unused. Note that --tests=false hid the problem, but obviously didn't check the test code.
Ideally, *_test.go code should be checked likewise.
It is very annoying.
@tpounds Do you still need more info? Maybe that label can be removed.
@AlekSi This definitely sounds like a bug. Can someone provide a minimal test case that can be used to reproduce the issue?
a.go:
package a
func a() int {
return 0
}
a_test.go:
package a
import (
"testing"
)
func TestA(t *testing.T) {
if a() != 0 {
t.Error()
}
}
result:
a/a.go:3:6: func
ais unused (unused)
func a() int {
^
a/a_test.go:7:6: funcTestAis unused (unused)
func TestA(t *testing.T) {
^
Workaround in .golangci.yml:
issues:
exclude-rules:
- path: _test\.go
linters:
- unused
An interesting thing: ExampleXXX will pass the lint, but TestXXX and BenchmarkXXX will fail.
Here is a reproducible demo project.
Most helpful comment
Workaround in .golangci.yml: