Gianguido Sorà 72e137e344 Added "bodyclose" support
This commit adds full support for bodyclose linter
(https://github.com/timakin/bodyclose), which checks if an `http.Body`
element is correctly closed after usage.

Since it can be used via `go/analysis', I followed the `govet' example
as suggested by https://github.com/golangci/golangci-lint/wiki/How-to-add-a-custom-linter.

This commit is fully tested, and contains a (flawed) test program which
calls `http.Get()' on `https://google.com' and does not closes its
corresponding `http.Body'.
2019-06-09 13:04:30 +03:00

27 lines
562 B
Go

package analysisutil
import (
"go/types"
"strings"
)
// RemoVendor removes vendoring infomation from import path.
func RemoveVendor(path string) string {
i := strings.Index(path, "vendor")
if i >= 0 {
return path[i+len("vendor")+1:]
}
return path
}
// LookupFromImports finds an object from import paths.
func LookupFromImports(imports []*types.Package, path, name string) types.Object {
path = RemoveVendor(path)
for i := range imports {
if path == RemoveVendor(imports[i].Path()) {
return imports[i].Scope().Lookup(name)
}
}
return nil
}