Golangci-lint: func TestXXX is unused (unused)

Created on 7 Oct 2019  路  12Comments  路  Source: golangci/golangci-lint

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.


  1. Version of golangci-lint: golangci-lint --version (or git commit if you don't use binary distribution)

  1. Config file: cat .golangci.yml
run:
  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
  1. Go environment: 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"
false positive

Most helpful comment

Workaround in .golangci.yml:

issues:
  exclude-rules:
  - path: _test\.go
    linters:
    - unused

All 12 comments

@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 a is unused (unused)
func a() int {
^
a/a_test.go:7:6: func TestA is 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.

https://github.com/tsaikd/go-demo-unused/tree/v1.27.0

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lnshi picture lnshi  路  4Comments

alessio picture alessio  路  4Comments

cyriltovena picture cyriltovena  路  5Comments

anuaimi picture anuaimi  路  4Comments

KeepMasterBranch picture KeepMasterBranch  路  3Comments