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
This commit is contained in:
parent
11cfd4a636
commit
036b7d3489
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue