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

func TestXXX is unused (unused) #791

Closed
hiromaily opened this issue Oct 7, 2019 · 14 comments
Closed

func TestXXX is unused (unused) #791

hiromaily opened this issue Oct 7, 2019 · 14 comments
Labels
false positive An error is reported when one does not exist

Comments

@hiromaily
Copy link

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)
7577d548a3893f86cb9aa479b88c52ff5d437893
  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"
@tpounds tpounds added false positive An error is reported when one does not exist feedback required Requires additional feedback labels Oct 7, 2019
@tpounds
Copy link
Contributor

tpounds commented Oct 7, 2019

@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?

@hiromaily
Copy link
Author

@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

@hiromaily
Copy link
Author

Not only Test func, there are many unexposed funcs and types are detected as unused.

@SimonT90poe
Copy link

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.

@hiromaily
Copy link
Author

hiromaily commented Nov 1, 2019

@SimonT90poe
Is it regresssion error?

@SimonT90poe
Copy link

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.

@hiromaily
Copy link
Author

Ideally, *_test.go code should be checked likewise.

@AlekSi
Copy link
Contributor

AlekSi commented Nov 4, 2019

It is very annoying.
@tpounds Do you still need more info? Maybe that label can be removed.

@tpounds
Copy link
Contributor

tpounds commented Nov 4, 2019

@AlekSi This definitely sounds like a bug. Can someone provide a minimal test case that can be used to reproduce the issue?

@cjyar
Copy link

cjyar commented Nov 12, 2019

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) {
^

@cjyar
Copy link

cjyar commented Nov 12, 2019

Workaround in .golangci.yml:

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

theckman pushed a commit to theckman/golangci-lint that referenced this issue May 3, 2020
$ git cherry --abbrev -v 0af0999fabfb ee9bf5809ead
+ abd8436 all: enable Go modules on CI (golangci#753)
+ 3c9d0fb checkers: recognize //line and //nolint in commentFormatting (golangci#756)
+ 0b517d7 checkers: extend deprecatedComment patterns (golangci#757)
+ 09100f6 checkers: use astcast package instead of coerce.go (golangci#758)
+ 2e9e97f checker: simplify boolExprSimplify (golangci#759)
+ 575701e make: add go-consistent to CI checks list (golangci#761)
+ b55f431 checkers: fix unlambda handling of builtins (golangci#763)
+ 5a7dee3 checker: handle lambdas properly in boolExprSimplify (golangci#765)
+ 5ce3939 checkers: teach boolExprSimplify a few new tricks (golangci#766)
+ 04d160f checkers: add new patterns to boolExprSimplify (golangci#768)
+ 09582e2 make: collect coverprofile separately from goveralls (golangci#769)
+ d8d0ee4 checkers: recognize NOTE pattern in deprecatedComment (golangci#770)
+ 12f0f85 Update copyright notice to 2019 (golangci#771)
+ f54bdb6 checkers: add stringXbytes checker
+ 170d65c checkers: followup for golangci#773 (golangci#774)
+ 84e9e83 checkers: make stringXbytes more linear (golangci#775)
+ a800815 checkers: add Depreacted typo pattern (golangci#776)
+ 6751be9 checkers: add hexLiterals (golangci#772)
+ ac61906 checkers: add typeAssertChain checker (golangci#782)
+ d19dbf1 checkers: add codegenComment checker (golangci#783)
+ d82b576 checkers: proper pkg/obj check for flagName (golangci#786)
+ dfcf754 ci: enable integration tests (golangci#787)
+ 5dafc45 checkers: fix equalFold false positive (golangci#788)
+ ed5e8e7 checkers: refactor and fix hexLiteral checker (golangci#789)
+ e704e07 checkers: add argOrder checker (golangci#790)
+ 34c1dc8 checkers: add Split handling to argOrder checker (golangci#791)
+ cbe095d checkers: add math.Max and math.Min to dupArg (golangci#792)
+ c986ee5 checkers: add checkers info fields test (golangci#794)
+ 66e5832 cmd/makedocs: use lintpack, fix build (golangci#793)
+ 6bce9d0 cmd/makedocs: add enabled/disabled by default info (golangci#795)
+ 4adbf9a checkers: simplify flagName (golangci#799)
+ 07de34a checkers: add octalLiteral checker (golangci#798)
+ 765907a cmd/makedocs: add checker param docs (golangci#796)
+ ee9bf58 cmd/makedocs: fix headers formatting (golangci#803)
@tsaikd
Copy link

tsaikd commented Jun 8, 2020

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

@mislav
Copy link

mislav commented Jan 4, 2021

This setting:

linters-settings:
  unused:
    check-exported: true

translates to the "WholeProgram" setting of honnef.co/go/tools/unused, which was removed upstream due to memory consumption and bugs like these. So it's likely that this will never be fixed with the unused linter.

I'm unaware of another linter tool that can check for unused structs and functions that might have been exported (e.g. run in "app" rather than in "library" mode).

@ldez
Copy link
Member

ldez commented Mar 25, 2021

fixed by #1756

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
false positive An error is reported when one does not exist
Projects
None yet
Development

No branches or pull requests

8 participants