]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/vet: refactor to support alternative importers
authorJosh Bleecher Snyder <josharian@gmail.com>
Thu, 2 Mar 2017 15:56:53 +0000 (07:56 -0800)
committerJosh Bleecher Snyder <josharian@gmail.com>
Thu, 2 Mar 2017 18:43:12 +0000 (18:43 +0000)
Instead of constructing the importer in init, do it lazily as needed.
This lets us select the importer using a command line flag.
The addition of the command line flag will come in a follow-up CL.

Change-Id: Ieb3a5f01a34fb5bd220a95086daf5d6b37e83bb5
Reviewed-on: https://go-review.googlesource.com/37669
Reviewed-by: Rob Pike <r@golang.org>
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/vet/httpresponse.go
src/cmd/vet/types.go

index f667edb515f35dbb483d531f3a96b4bd090d9ad1..ce5ae468ab87719853987b9399a4cfdb5fbd3587 100644 (file)
@@ -12,29 +12,17 @@ import (
        "go/types"
 )
 
-var (
-       httpResponseType types.Type
-       httpClientType   types.Type
-)
-
 func init() {
-       if typ := importType("net/http", "Response"); typ != nil {
-               httpResponseType = typ
-       }
-       if typ := importType("net/http", "Client"); typ != nil {
-               httpClientType = typ
-       }
-       // if http.Response or http.Client are not defined don't register this check.
-       if httpResponseType == nil || httpClientType == nil {
-               return
-       }
-
        register("httpresponse",
                "check errors are checked before using an http Response",
                checkHTTPResponse, callExpr)
 }
 
 func checkHTTPResponse(f *File, node ast.Node) {
+       // If http.Response or http.Client are not defined, skip this check.
+       if httpResponseType == nil || httpClientType == nil {
+               return
+       }
        call := node.(*ast.CallExpr)
        if !isHTTPFuncOrMethodOnClient(f, call) {
                return // the function call is not related to this check.
index 8357d3c2bf79c7bd7a63ff647d625976f4e7e549..f1927738c4eacb9156d53164bd8f2f15e6f4accc 100644 (file)
@@ -14,26 +14,32 @@ import (
 )
 
 // stdImporter is the importer we use to import packages.
-// It is created during initialization so that all packages
-// are imported by the same importer.
-var stdImporter = importer.Default()
+// It is shared so that all packages are imported by the same importer.
+var stdImporter types.Importer
 
 var (
-       errorType     *types.Interface
-       stringerType  *types.Interface // possibly nil
-       formatterType *types.Interface // possibly nil
+       errorType        *types.Interface
+       stringerType     *types.Interface // possibly nil
+       formatterType    *types.Interface // possibly nil
+       httpResponseType types.Type       // possibly nil
+       httpClientType   types.Type       // possibly nil
 )
 
-func init() {
+func inittypes() {
        errorType = types.Universe.Lookup("error").Type().Underlying().(*types.Interface)
 
        if typ := importType("fmt", "Stringer"); typ != nil {
                stringerType = typ.Underlying().(*types.Interface)
        }
-
        if typ := importType("fmt", "Formatter"); typ != nil {
                formatterType = typ.Underlying().(*types.Interface)
        }
+       if typ := importType("net/http", "Response"); typ != nil {
+               httpResponseType = typ
+       }
+       if typ := importType("net/http", "Client"); typ != nil {
+               httpClientType = typ
+       }
 }
 
 // importType returns the type denoted by the qualified identifier
@@ -54,6 +60,10 @@ func importType(path, name string) types.Type {
 }
 
 func (pkg *Package) check(fs *token.FileSet, astFiles []*ast.File) error {
+       if stdImporter == nil {
+               stdImporter = importer.Default()
+               inittypes()
+       }
        pkg.defs = make(map[*ast.Ident]types.Object)
        pkg.uses = make(map[*ast.Ident]types.Object)
        pkg.selectors = make(map[*ast.SelectorExpr]*types.Selection)