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

U1000 is not reported when identifier is used only by its own test #475

Closed
dmitshur opened this issue May 10, 2019 · 3 comments

Comments

Projects
None yet
3 participants
@dmitshur
Copy link
Contributor

commented May 10, 2019

Consider a package with a single .go file:

package p

func Foo() error {
	return nil
}

func bar() error {
	return nil
}

staticcheck correctly reports that bar is unused:

$ staticcheck 
p.go:7:6: func bar is unused (U1000)

Now consider if both Foo and bar have tests:

package p

import "testing"

func TestFoo(t *testing.T) {
	got := Foo()
	if got != nil {
		t.Error("got a non-nil error:", got)
	}
}

func TestBar(t *testing.T) {
	got := bar()
	if got != nil {
		t.Error("got a non-nil error:", got)
	}
}

staticcheck no longer reports bar as unused:

$ staticcheck 
$ echo $?
0

That makes it harder to tell that an identifier with test coverage is no longer used. Perhaps it's possible to treat a test named TestSomething as not using an identifier or type something or Something for the purposes of U1000.

@ainar-g

This comment has been minimized.

Copy link

commented May 11, 2019

See #289, #304, and #94.

@dominikh

This comment has been minimized.

Copy link
Owner

commented May 11, 2019

In my experience, tests are rarely named after exact identifiers, especially when they test individual methods. I can only suggest running staticcheck with tests disabled. See also the issues Ainar linked.

@dominikh dominikh closed this May 11, 2019

@dmitshur

This comment has been minimized.

Copy link
Contributor Author

commented May 12, 2019

I can only suggest running staticcheck with tests disabled.

I didn't realize that would help (I thought it would just not report problems in tests). Thanks, that sounds good.

In my experience, tests are rarely named after exact identifiers

This is true. It's only examples that have a well-specified naming pattern (https://godoc.org/testing#hdr-Examples).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.