Skip to content
This repository has been archived by the owner on Sep 1, 2018. It is now read-only.

Commit

Permalink
Also get types from second-level imports
Browse files Browse the repository at this point in the history
In the added test (from the README), we import "os" and "io/ioutil" but
not "io", where the interface is defined.

Stop at second-level since that should be enough.

Fixes #34.
  • Loading branch information
mvdan committed Apr 5, 2017
1 parent e9406f7 commit 0e7ff92
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ also trigger a warning - if `f` were an `io.ReadCloser`, the same
message would appear.

It suggests interface types defined both in the func's package and the
package's imports.
package's imports (two levels; direct imports and their direct imports).

### False positives

Expand Down
19 changes: 13 additions & 6 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ type pkgTypes struct {
func (p *pkgTypes) getTypes(pkg *types.Package) {
p.ifaces = make(map[string]string)
p.funcSigns = make(map[string]bool)
addTypes := func(impPath string, ifs map[string]string, funs map[string]bool, top bool) {
done := make(map[*types.Package]bool)
addTypes := func(pkg *types.Package, top bool) {
if done[pkg] {
return
}
done[pkg] = true
ifs, funs := fromScope(pkg.Scope())
fullName := func(name string) string {
if !top {
return impPath + "." + name
return pkg.Path() + "." + name
}
return name
}
Expand All @@ -35,9 +41,10 @@ func (p *pkgTypes) getTypes(pkg *types.Package) {
}
}
for _, imp := range pkg.Imports() {
ifs, funs := fromScope(imp.Scope())
addTypes(imp.Path(), ifs, funs, false)
addTypes(imp, false)
for _, imp2 := range imp.Imports() {
addTypes(imp2, false)
}
}
ifs, funs := fromScope(pkg.Scope())
addTypes(pkg.Path(), ifs, funs, true)
addTypes(pkg, true)
}
16 changes: 16 additions & 0 deletions testdata/files/readme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package foo

import (
"io/ioutil"
"os"
)

func ProcessInput(f *os.File) error { // WARN f can be io.Reader
b, err := ioutil.ReadAll(f)
if err != nil {
return err
}
return processBytes(b)
}

func processBytes(b []byte) error { return nil }

0 comments on commit 0e7ff92

Please sign in to comment.