Skip to content

Commit 557244c

Browse files
go/printer: if comment gets formatted away, don't squash
We were dropping the preceding whitespace. Fixes #53059 Change-Id: Ide0d6a76c463a7481ec4883a8568f7f7b5e87e9f Reviewed-on: https://go-review.googlesource.com/c/go/+/408394 Reviewed-by: Robert Griesemer <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]>
1 parent 757f3c8 commit 557244c

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

src/go/printer/printer.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,18 @@ func (p *printer) intersperseComments(next token.Position, tok token.Token) (wro
747747
// a top-level doc comment.
748748
list = formatDocComment(list)
749749
changed = true
750+
751+
if len(p.comment.List) > 0 && len(list) == 0 {
752+
// The doc comment was removed entirely.
753+
// Keep preceding whitespace.
754+
p.writeCommentPrefix(p.posFor(p.comment.Pos()), next, last, tok)
755+
// Change print state to continue at next.
756+
p.pos = next
757+
p.last = next
758+
// There can't be any more comments.
759+
p.nextComment()
760+
return p.writeCommentSuffix(false)
761+
}
750762
}
751763
for _, c := range list {
752764
p.writeCommentPrefix(p.posFor(c.Pos()), next, last, tok)

src/go/printer/testdata/comments.golden

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,4 +767,8 @@ var _ = []T{ // lone comments
767767
// in composite lit
768768
}
769769

770+
func _() {}
771+
772+
func _() {}
773+
770774
/* This comment is the last entry in this file. It must be printed and should be followed by a newline */

src/go/printer/testdata/comments.input

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,4 +765,9 @@ var _ = []T{// lone comments
765765
// in composite lit
766766
}
767767

768+
func _() {}
769+
770+
//
771+
func _() {}
772+
768773
/* This comment is the last entry in this file. It must be printed and should be followed by a newline */

0 commit comments

Comments
 (0)