dominikh / go-tools

Staticcheck - The advanced Go linter
https://staticcheck.dev
MIT License
6.15k stars 373 forks source link

unused: triggers on unexported iface that is embedded in exported iface #290

Closed quasilyte closed 5 years ago

quasilyte commented 6 years ago

megacheck "unused" linter reports unexported interfaces that are embedded inside exported interfaces as unused.

foo.go:

package foo

type embedded interface {
    Method1()
}

// IfaceUsingEmdedded is a public iface that uses private
// interface via embedding.
type IfaceUsingEmdedded interface {
    embedded // <- Usage of embedded
    Method2()
}

(replaced abs GOPATH with $GOPATH, also replaced some sensitive paths with ???)

$ gometalinter --debug --disable-all --enable=megacheck ./src/foo
DEBUG: [Jun 20 11:44:05.977] PATH=???/gotools/bin:???/CODE/go/go/bin:???/bin:???/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:$GOPATH/bin
DEBUG: [Jun 20 11:44:05.977] GOPATH=$GOPATH
DEBUG: [Jun 20 11:44:05.977] GOBIN=
DEBUG: [Jun 20 11:44:05.977] linting path ./src/foo
DEBUG: [Jun 20 11:44:05.982] [megacheck.1]: executing ???/gotools/bin/megacheck ./src/foo
DEBUG: [Jun 20 11:44:05.989] [megacheck.1]: warning: ???/gotools/bin/megacheck returned exit status 1: src/foo/foo.go:3:6: type embedded is unused (U1000)

DEBUG: [Jun 20 11:44:05.989] [megacheck.1]: megacheck hits 1: ^(?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.*)$
DEBUG: [Jun 20 11:44:05.989] [megacheck.1]: megacheck linter took 6.684575ms
DEBUG: [Jun 20 11:44:05.989] nolint: parsing src/foo/foo.go for directives
DEBUG: [Jun 20 11:44:05.989] nolint: parsing src/foo/foo.go took 118.16µs
src/foo/foo.go:3:6:warning: type embedded is unused (U1000) (megacheck)
DEBUG: [Jun 20 11:44:05.989] total elapsed time 12.032197ms
$ megacheck -version
megacheck 2017.2

Is this known issue/limitation or "working as expected"? My guess is that's false positive.

Thanks.

quasilyte commented 6 years ago

This may be a workaround for this:

var _ = embedded(nil) // replace "embedded" with "unused" iface name

Makes it possible to pass CI tests.