From 036b7d3489ead40684bfe8f08b50208b8286c132 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Wed, 11 Dec 2013 21:12:08 -0500 Subject: [PATCH] go.tools/types: skip "imported but not used" check if IgnoreFuncBodies. Obviously in that mode, we can't correctly diagnose such errors, so we shouldn't attempt it (and emit false positives). R=gri CC=golang-dev https://golang.org/cl/41080043 --- go/types/resolver.go | 52 ++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/go/types/resolver.go b/go/types/resolver.go index 6594334a..dc6c15a0 100644 --- a/go/types/resolver.go +++ b/go/types/resolver.go @@ -462,33 +462,37 @@ func (check *checker) resolveFiles(files []*ast.File) { // any of its exported identifiers. To import a package solely for its side-effects // (initialization), use the blank identifier as explicit package name." - for i, scope := range fileScopes { - var usedDotImports map[*Package]bool // lazily allocated - for _, obj := range scope.elems { - switch obj := obj.(type) { - case *PkgName: - // Unused "blank imports" are automatically ignored - // since _ identifiers are not entered into scopes. - if !obj.used { - check.errorf(obj.pos, "%q imported but not used", obj.pkg.path) - } - default: - // All other objects in the file scope must be dot- - // imported. If an object was used, mark its package - // as used. - if obj.isUsed() { - if usedDotImports == nil { - usedDotImports = make(map[*Package]bool) + // We must skip this check if we didn't look at function bodies. + + if !check.conf.IgnoreFuncBodies { + for i, scope := range fileScopes { + var usedDotImports map[*Package]bool // lazily allocated + for _, obj := range scope.elems { + switch obj := obj.(type) { + case *PkgName: + // Unused "blank imports" are automatically ignored + // since _ identifiers are not entered into scopes. + if !obj.used { + check.errorf(obj.pos, "%q imported but not used", obj.pkg.path) + } + default: + // All other objects in the file scope must be dot- + // imported. If an object was used, mark its package + // as used. + if obj.isUsed() { + if usedDotImports == nil { + usedDotImports = make(map[*Package]bool) + } + usedDotImports[obj.Pkg()] = true } - usedDotImports[obj.Pkg()] = true } } - } - // Iterate through all dot-imports for this file and - // check if the corresponding package was used. - for pkg, pos := range dotImports[i] { - if !usedDotImports[pkg] { - check.errorf(pos, "%q imported but not used", pkg.path) + // Iterate through all dot-imports for this file and + // check if the corresponding package was used. + for pkg, pos := range dotImports[i] { + if !usedDotImports[pkg] { + check.errorf(pos, "%q imported but not used", pkg.path) + } } } }