diff options
author | David R. Jenni <david.r.jenni@gmail.com> | 2015-09-01 22:26:39 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2015-09-01 22:26:39 +0000 |
commit | 894e0d66f8c08d2f239a5f544e2753ba93ca5c5f (patch) | |
tree | 42d9f0fdf315a6a92ac10b541f5f14dc9d754159 | |
parent | 19742a3fac260b8f426ce55ca6306b1d2728cc24 (diff) | |
parent | 4f50f44d7a3206e9e28b984e023efce2a4a75369 (diff) | |
download | tools-894e0d66f8c08d2f239a5f544e2753ba93ca5c5f.tar.gz |
imports: fix bug, where unused named import is mistaken for unnamed import.
automerge: 4f50f44
* commit '4f50f44d7a3206e9e28b984e023efce2a4a75369':
imports: fix bug, where unused named import is mistaken for unnamed import.
-rw-r--r-- | imports/fix.go | 30 | ||||
-rw-r--r-- | imports/fix_test.go | 18 |
2 files changed, 33 insertions, 15 deletions
diff --git a/imports/fix.go b/imports/fix.go index 22fde6c..3ccee0e 100644 --- a/imports/fix.go +++ b/imports/fix.go @@ -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 } diff --git a/imports/fix_test.go b/imports/fix_test.go index 6be1d57..f087bc7 100644 --- a/imports/fix_test.go +++ b/imports/fix_test.go @@ -709,6 +709,24 @@ var ( ) `, }, + + // 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() } +`, + }, } func TestFixImports(t *testing.T) { |