go-misc icon indicating copy to clipboard operation
go-misc copied to clipboard

deadcode: TestXXX is unused ?

Open Dieterbe opened this issue 8 years ago • 4 comments

Hello Rémy, first of all, deadcode is a neat tool. thank you. i've used it to find some deadcode that i was able to remove.

I found a problem though. It doesn't recognize Test methods. see this example: (or am i using the tool wrong?)

$ pwd
/home/dieter/go/src/github.com/Dieterbe/deadtest
$ ls *.go
main.go  main_test.go
$ cat main.go
package main

import "fmt"

func main() {
	fmt.Println(foo(10))
}

func foo(in int) int {
	return in / 10
}
$ cat main_test.go
package main

import "testing"

func TestFoo(t *testing.T) {
	if foo(10) != 1 {
		t.Errorf("foo(10) should be 1")
	}
}
$ go test -v
=== RUN   TestFoo
--- PASS: TestFoo (0.00s)
PASS
ok  	github.com/Dieterbe/deadtest	0.001s
$ deadcode -test .
main_test.go:5:6: TestFoo is unused
$                                                                      

Btw happy holidays! hope you are doing well.

Dieterbe avatar Dec 27 '17 11:12 Dieterbe

When I reorganize the code a bit, it works fine:

$ pwd
/home/dieter/go/src/github.com/Dieterbe/deadtest
$ find .
.
./main.go
./foo
./foo/f.go
./foo/f_test.go
$ cat main.go
package main

import (
	"fmt"

	"github.com/Dieterbe/deadtest/foo"
)

func main() {
	fmt.Println(foo.F(10))
}
$ cat ./foo/f.go
package foo

func F(in int) int {
	return in / 10
}
$ cat ./foo/f_test.go
package foo

import "testing"

func TestF(t *testing.T) {
	if F(10) != 1 {
		t.Errorf("F(10) should be 1")
	}
}
$ go test -v github.com/Dieterbe/deadtest/foo
=== RUN   TestF
--- PASS: TestF (0.00s)
PASS
ok  	github.com/Dieterbe/deadtest/foo	0.001s
$ deadcode -test .
$ deadcode -test github.com/Dieterbe/deadtest/foo
$

Dieterbe avatar Dec 27 '17 12:12 Dieterbe

I have the same issue.

caarlos0 avatar Jun 06 '18 12:06 caarlos0

I'm experiencing the same issue. Tests in the main package are falsely reported as unused.

ryboe avatar Jun 17 '18 16:06 ryboe

This issue has been fixed in golangci-lint. I recommend using the forked-and-fixed deadcode in that tool.

golangci-lint run --disable-all --enable deadcode

ryboe avatar Jun 30 '18 05:06 ryboe