imports: fix bug, where unused named import is mistaken for unnamed import.
Fixes #8149. Change-Id: Ia3d318f70981b2032a71d3fd32eaffba20cfbcbd Reviewed-on: https://go-review.googlesource.com/13371 Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
This commit is contained in:
parent
b8e61d42ca
commit
4f50f44d7a
|
@ -89,6 +89,21 @@ func fixImports(fset *token.FileSet, f *ast.File) (added []string, err error) {
|
|||
})
|
||||
ast.Walk(visitor, f)
|
||||
|
||||
// Nil out any unused ImportSpecs, to be removed in following passes
|
||||
unusedImport := map[string]bool{}
|
||||
for pkg, is := range decls {
|
||||
if refs[pkg] == nil && pkg != "_" && pkg != "." {
|
||||
unusedImport[strings.Trim(is.Path.Value, `"`)] = true
|
||||
}
|
||||
}
|
||||
for ipath := range unusedImport {
|
||||
if ipath == "C" {
|
||||
// Don't remove cgo stuff.
|
||||
continue
|
||||
}
|
||||
astutil.DeleteImport(fset, f, ipath)
|
||||
}
|
||||
|
||||
// Search for imports matching potential package references.
|
||||
searches := 0
|
||||
type result struct {
|
||||
|
@ -126,21 +141,6 @@ func fixImports(fset *token.FileSet, f *ast.File) (added []string, err error) {
|
|||
}
|
||||
}
|
||||
|
||||
// Nil out any unused ImportSpecs, to be removed in following passes
|
||||
unusedImport := map[string]bool{}
|
||||
for pkg, is := range decls {
|
||||
if refs[pkg] == nil && pkg != "_" && pkg != "." {
|
||||
unusedImport[strings.Trim(is.Path.Value, `"`)] = true
|
||||
}
|
||||
}
|
||||
for ipath := range unusedImport {
|
||||
if ipath == "C" {
|
||||
// Don't remove cgo stuff.
|
||||
continue
|
||||
}
|
||||
astutil.DeleteImport(fset, f, ipath)
|
||||
}
|
||||
|
||||
return added, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -707,6 +707,24 @@ var (
|
|||
_ p.P
|
||||
_ = regexp.Compile
|
||||
)
|
||||
`,
|
||||
},
|
||||
|
||||
// Unused named import is mistaken for unnamed import
|
||||
// golang.org/issue/8149
|
||||
{
|
||||
name: "issue 8149",
|
||||
in: `package main
|
||||
|
||||
import foo "fmt"
|
||||
|
||||
func main() { fmt.Println() }
|
||||
`,
|
||||
out: `package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() { fmt.Println() }
|
||||
`,
|
||||
},
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue