analysis/passes: consolidate imports helper
Completes a TODO from CL 212920 to consolidate
all the sites that used a custom "imports" helper.
Change-Id: I4ad11a1d90d616102085dfe6f10578da22164e7c
Reviewed-on: https://go-review.googlesource.com/c/tools/+/214857
Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
diff --git a/go/analysis/passes/atomicalign/atomicalign.go b/go/analysis/passes/atomicalign/atomicalign.go
index 7387df0..e2e1a4f 100644
--- a/go/analysis/passes/atomicalign/atomicalign.go
+++ b/go/analysis/passes/atomicalign/atomicalign.go
@@ -16,6 +16,7 @@
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
+ "golang.org/x/tools/go/analysis/passes/internal/analysisutil"
"golang.org/x/tools/go/ast/inspector"
)
@@ -32,7 +33,7 @@
if 8*pass.TypesSizes.Sizeof(types.Typ[types.Uintptr]) == 64 {
return nil, nil // 64-bit platform
}
- if imports(pass.Pkg, "sync/atomic") == nil {
+ if !analysisutil.Imports(pass.Pkg, "sync/atomic") {
return nil, nil // doesn't directly import sync/atomic
}
@@ -114,15 +115,3 @@
pass.ReportRangef(arg, "address of non 64-bit aligned field .%s passed to atomic.%s", tvar.Name(), funcName)
}
-
-// imports reports whether pkg has path among its direct imports.
-// It returns the imported package if so, or nil if not.
-// copied from passes/cgocall.
-func imports(pkg *types.Package, path string) *types.Package {
- for _, imp := range pkg.Imports() {
- if imp.Path() == path {
- return imp
- }
- }
- return nil
-}
diff --git a/go/analysis/passes/cgocall/cgocall.go b/go/analysis/passes/cgocall/cgocall.go
index 1b7b8d2..5768d0b 100644
--- a/go/analysis/passes/cgocall/cgocall.go
+++ b/go/analysis/passes/cgocall/cgocall.go
@@ -40,7 +40,7 @@
}
func run(pass *analysis.Pass) (interface{}, error) {
- if imports(pass.Pkg, "runtime/cgo") == nil {
+ if !analysisutil.Imports(pass.Pkg, "runtime/cgo") {
return nil, nil // doesn't use cgo
}
@@ -374,15 +374,3 @@
}
return obj.(*types.PkgName).Imported()
}
-
-// imports reports whether pkg has path among its direct imports.
-// It returns the imported package if so, or nil if not.
-// TODO(adonovan): move to analysisutil.
-func imports(pkg *types.Package, path string) *types.Package {
- for _, imp := range pkg.Imports() {
- if imp.Path() == path {
- return imp
- }
- }
- return nil
-}
diff --git a/go/analysis/passes/httpresponse/httpresponse.go b/go/analysis/passes/httpresponse/httpresponse.go
index ec335d3..fd9e2af 100644
--- a/go/analysis/passes/httpresponse/httpresponse.go
+++ b/go/analysis/passes/httpresponse/httpresponse.go
@@ -12,6 +12,7 @@
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
+ "golang.org/x/tools/go/analysis/passes/internal/analysisutil"
"golang.org/x/tools/go/ast/inspector"
)
@@ -43,7 +44,7 @@
// Fast path: if the package doesn't import net/http,
// skip the traversal.
- if !imports(pass.Pkg, "net/http") {
+ if !analysisutil.Imports(pass.Pkg, "net/http") {
return nil, nil
}
@@ -166,12 +167,3 @@
obj := n.Obj()
return obj.Name() == name && obj.Pkg() != nil && obj.Pkg().Path() == path
}
-
-func imports(pkg *types.Package, path string) bool {
- for _, imp := range pkg.Imports() {
- if imp.Path() == path {
- return true
- }
- }
- return false
-}
diff --git a/go/analysis/passes/internal/analysisutil/util.go b/go/analysis/passes/internal/analysisutil/util.go
index 13a458d..80c9476 100644
--- a/go/analysis/passes/internal/analysisutil/util.go
+++ b/go/analysis/passes/internal/analysisutil/util.go
@@ -104,3 +104,13 @@
}
}
}
+
+// Imports returns true if path is imported by pkg.
+func Imports(pkg *types.Package, path string) bool {
+ for _, imp := range pkg.Imports() {
+ if imp.Path() == path {
+ return true
+ }
+ }
+ return false
+}
diff --git a/go/analysis/passes/testinggoroutine/testinggoroutine.go b/go/analysis/passes/testinggoroutine/testinggoroutine.go
index 2f6604d..d2b9a56 100644
--- a/go/analysis/passes/testinggoroutine/testinggoroutine.go
+++ b/go/analysis/passes/testinggoroutine/testinggoroutine.go
@@ -6,10 +6,10 @@
import (
"go/ast"
- "go/types"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
+ "golang.org/x/tools/go/analysis/passes/internal/analysisutil"
"golang.org/x/tools/go/ast/inspector"
)
@@ -46,7 +46,7 @@
func run(pass *analysis.Pass) (interface{}, error) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
- if !imports(pass.Pkg, "testing") {
+ if !analysisutil.Imports(pass.Pkg, "testing") {
return nil, nil
}
@@ -86,17 +86,6 @@
return nil, nil
}
-func imports(pkg *types.Package, path string) bool {
- // TODO: (@odeke-em) perhaps move this function into a
- // a library since it is commonly used in x/tools/go/analysis.
- for _, imp := range pkg.Imports() {
- if imp.Path() == path {
- return true
- }
- }
- return false
-}
-
func hasBenchmarkOrTestParams(fnDecl *ast.FuncDecl) bool {
// Check that the function's arguments include "*testing.T" or "*testing.B".
params := fnDecl.Type.Params.List