1
0
mirror of https://github.com/golang/go synced 2024-10-01 03:18:33 -06:00

go/ast/astutil: fix DeleteImport SEGV when Rparen is invalid

Updates #36383

Change-Id: I04b33810c16f4fb7871f5a6a8207b1c159cbc65f
GitHub-Last-Rev: 791fb862709c9a210bedde524555c5a6a92d9c51
GitHub-Pull-Request: golang/tools#196
Reviewed-on: https://go-review.googlesource.com/c/tools/+/214340
Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
itchyny 2020-01-14 23:15:50 +00:00 committed by Michael Matloob
parent 39095c1d17
commit 43d5027782
2 changed files with 16 additions and 2 deletions

View File

@ -275,9 +275,10 @@ func DeleteNamedImport(fset *token.FileSet, f *ast.File, name, path string) (del
// We deleted an entry but now there may be
// a blank line-sized hole where the import was.
if line-lastLine > 1 {
if line-lastLine > 1 || !gen.Rparen.IsValid() {
// There was a blank line immediately preceding the deleted import,
// so there's no need to close the hole.
// so there's no need to close the hole. The right parenthesis is
// invalid after AddImport to an import statement without parenthesis.
// Do nothing.
} else if line != fset.File(gen.Rparen).LineCount() {
// There was no blank line. Close the hole.

View File

@ -1684,6 +1684,19 @@ func TestDeleteImport(t *testing.T) {
}
}
func TestDeleteImportAfterAddImport(t *testing.T) {
file := parse(t, "test", `package main
import "os"
`)
if got, want := AddImport(fset, file, "fmt"), true; got != want {
t.Errorf("AddImport: got: %v, want: %v", got, want)
}
if got, want := DeleteImport(fset, file, "fmt"), true; got != want {
t.Errorf("DeleteImport: got: %v, want: %v", got, want)
}
}
type rewriteTest struct {
name string
srcPkg string