mirror of
https://github.com/golang/go.git
synced 2026-06-27 03:11:23 +00:00
go/printer: fix false positive doc comment
Added the condition to check if the token is inside of statement (token.IDENT), where we should not treat comments as unindented top-level doc comments. I would also add, in another patch, functions to detect the statement, expression context to simplify conditions in printer.intersperseComments() as pointed out in TODO(gri). Fixes #52605 Change-Id: Ia111cd09821c78c5c84e53524ad0649e01f489b4 Reviewed-on: https://go-review.googlesource.com/c/go/+/579978 Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com> Reviewed-by: Russ Cox <rsc@golang.org> LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Hyang-Ah Hana Kim <hyangah@gmail.com>
This commit is contained in:
parent
44fde0fd08
commit
c1f0b9bdba
2 changed files with 43 additions and 1 deletions
|
|
@ -739,7 +739,9 @@ func (p *printer) intersperseComments(next token.Position, tok token.Token) (wro
|
|||
for p.commentBefore(next) {
|
||||
list := p.comment.List
|
||||
changed := false
|
||||
if p.lastTok != token.IMPORT && // do not rewrite cgo's import "C" comments
|
||||
|
||||
if tok != token.IDENT &&
|
||||
p.lastTok != token.IMPORT && // do not rewrite cgo's import "C" comments
|
||||
p.posFor(p.comment.Pos()).Column == 1 &&
|
||||
p.posFor(p.comment.End()+1) == next {
|
||||
// Unindented comment abutting next token position:
|
||||
|
|
|
|||
|
|
@ -479,6 +479,46 @@ func g() {
|
|||
}
|
||||
}
|
||||
|
||||
func TestIssue52605(t *testing.T) {
|
||||
const orig = `
|
||||
package p
|
||||
|
||||
// Doc
|
||||
//
|
||||
type T struct {
|
||||
// This is not
|
||||
// a doc comment.
|
||||
X int
|
||||
}
|
||||
`
|
||||
|
||||
const want = `package p
|
||||
|
||||
// Doc
|
||||
type T struct {
|
||||
// This is not
|
||||
// a doc comment.
|
||||
X int
|
||||
}
|
||||
`
|
||||
|
||||
f, err := parser.ParseFile(fset, "src.go", orig, parser.ParseComments)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
err = Fprint(&buf, fset, f)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
got := buf.String()
|
||||
if got != want {
|
||||
t.Errorf("got:\n%s\nwant:\n%s\n", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
var decls = []string{
|
||||
`import "fmt"`,
|
||||
"const pi = 3.1415\nconst e = 2.71828\n\nvar x = pi",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue