Skip to content

Commit 7b0f14c

Browse files
author
Eva Charlotte Mayer
committed
go/ast: improve comment associations in empty function/loop/if bodies
Fixes golang#39753 Signed-off-by: Eva Charlotte Mayer <eva.mayer@tum.de>
1 parent b137746 commit 7b0f14c

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

src/go/ast/commentmap.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ func NewCommentMap(fset *token.FileSet, node Node, comments []*CommentGroup) Com
204204
assoc = pg
205205
case p != nil &&
206206
(pend.Line == r.pos.Line ||
207+
pend.Line-1 == r.pos.Line && r.end.Line+1 < qpos.Line ||
207208
pend.Line+1 == r.pos.Line && r.end.Line+1 < qpos.Line ||
208209
q == nil):
209210
// same rules apply as above for p rather than pg,

src/go/ast/commentmap_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func f1() {
6161
6262
// f2
6363
func f2() {
64+
// associated with f2.body
6465
}
6566
6667
func f3() {
@@ -88,10 +89,11 @@ var res = map[string]string{
8889
"29: *ast.FuncDecl": "f1\nassociated with f1\nalso associated with f1\n",
8990
"31: *ast.ExprStmt": " associated with s1\nalso associated with s1\n",
9091
"37: *ast.ExprStmt": "associated with s2\nalso associated with s2\nline comment for s2\n",
92+
"45: *ast.BlockStmt": "associated with f2.body\n",
9193
"45: *ast.FuncDecl": "associated with f2\nf2\n",
92-
"49: *ast.AssignStmt": "addition\n",
93-
"49: *ast.BasicLit": " 1\n",
94-
"50: *ast.Ident": "the very last comment\n",
94+
"50: *ast.AssignStmt": "addition\n",
95+
"50: *ast.BasicLit": " 1\n",
96+
"51: *ast.Ident": "the very last comment\n",
9597
}
9698

9799
func ctext(list []*CommentGroup) string {

0 commit comments

Comments
 (0)