From c2ef61f450233d3c629ade8eb2083e43fa75c97a Mon Sep 17 00:00:00 2001 From: Rhys Hiltner Date: Fri, 23 Sep 2016 14:57:19 -0700 Subject: [PATCH] go/ast/astutil: fix loop logic in AddNamedImport When merging import declarations into a single block, AddNamedImport modifies the list of declarations in the provided file while iterating over the list. Take care to adjust the index into the list so as to not skip entries or fall off the end. Fixes golang/go#17213 Change-Id: I807246f762c965ea1fc51eb57759f6088336db86 Reviewed-on: https://go-review.googlesource.com/29681 Reviewed-by: Brad Fitzpatrick --- go/ast/astutil/imports.go | 4 +++- go/ast/astutil/imports_test.go | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/go/ast/astutil/imports.go b/go/ast/astutil/imports.go index cfaa864a..070ff35d 100644 --- a/go/ast/astutil/imports.go +++ b/go/ast/astutil/imports.go @@ -150,7 +150,8 @@ func AddNamedImport(fset *token.FileSet, f *ast.File, name, ipath string) (added // Merge all the import declarations into the first one. var first *ast.GenDecl - for i, decl := range f.Decls { + for i := 0; i < len(f.Decls); i++ { + decl := f.Decls[i] gen, ok := decl.(*ast.GenDecl) if !ok || gen.Tok != token.IMPORT || declImports(gen, "C") { continue @@ -165,6 +166,7 @@ func AddNamedImport(fset *token.FileSet, f *ast.File, name, ipath string) (added first.Specs = append(first.Specs, spec) } f.Decls = append(f.Decls[:i], f.Decls[i+1:]...) + i-- } return true diff --git a/go/ast/astutil/imports_test.go b/go/ast/astutil/imports_test.go index 3f6d00dc..eb00e47b 100644 --- a/go/ast/astutil/imports_test.go +++ b/go/ast/astutil/imports_test.go @@ -509,6 +509,25 @@ import "C" // Comment import "C" import "bufio" +`, + }, + { + name: `issue 17213 many single-import lines`, + pkg: "fmt", + in: `package main + +import "bufio" +import "bytes" +import "errors" +`, + out: `package main + +import ( + "bufio" + "bytes" + "errors" + "fmt" +) `, }, }