mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cmd/doc: don't print a declaration twice
That can occur if we have -u set and there is an upper- and lower-case name of the same spelling in a single declaration. A rare corner case but easy to fix. Fix by remembering what we've printed. Fixes #21797. Change-Id: Ie0b681ae8c277fa16e9635ba594c1dff272b8aeb Reviewed-on: https://go-review.googlesource.com/78715 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
363a5da3b8
commit
337f04bd6c
3 changed files with 25 additions and 1 deletions
|
|
@ -445,6 +445,19 @@ var tests = []test{
|
||||||
`CaseMatch`,
|
`CaseMatch`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// No dups with -u. Issue 21797.
|
||||||
|
{
|
||||||
|
"case matching on, no dups",
|
||||||
|
[]string{"-u", p, `duplicate`},
|
||||||
|
[]string{
|
||||||
|
`Duplicate`,
|
||||||
|
`duplicate`,
|
||||||
|
},
|
||||||
|
[]string{
|
||||||
|
"\\)\n+const", // This will appear if the const decl appears twice.
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDoc(t *testing.T) {
|
func TestDoc(t *testing.T) {
|
||||||
|
|
|
||||||
|
|
@ -594,6 +594,11 @@ func (pkg *Package) symbolDoc(symbol string) bool {
|
||||||
// Constants and variables behave the same.
|
// Constants and variables behave the same.
|
||||||
values := pkg.findValues(symbol, pkg.doc.Consts)
|
values := pkg.findValues(symbol, pkg.doc.Consts)
|
||||||
values = append(values, pkg.findValues(symbol, pkg.doc.Vars)...)
|
values = append(values, pkg.findValues(symbol, pkg.doc.Vars)...)
|
||||||
|
// A declaration like
|
||||||
|
// const ( c = 1; C = 2 )
|
||||||
|
// could be printed twice if the -u flag is set, as it matches twice.
|
||||||
|
// So we remember which declarations we've printed to avoid duplication.
|
||||||
|
printed := make(map[*ast.GenDecl]bool)
|
||||||
for _, value := range values {
|
for _, value := range values {
|
||||||
// Print each spec only if there is at least one exported symbol in it.
|
// Print each spec only if there is at least one exported symbol in it.
|
||||||
// (See issue 11008.)
|
// (See issue 11008.)
|
||||||
|
|
@ -628,7 +633,7 @@ func (pkg *Package) symbolDoc(symbol string) bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(specs) == 0 {
|
if len(specs) == 0 || printed[value.Decl] {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
value.Decl.Specs = specs
|
value.Decl.Specs = specs
|
||||||
|
|
@ -636,6 +641,7 @@ func (pkg *Package) symbolDoc(symbol string) bool {
|
||||||
pkg.packageClause(true)
|
pkg.packageClause(true)
|
||||||
}
|
}
|
||||||
pkg.emit(value.Doc, value.Decl)
|
pkg.emit(value.Doc, value.Decl)
|
||||||
|
printed[value.Decl] = true
|
||||||
found = true
|
found = true
|
||||||
}
|
}
|
||||||
// Types.
|
// Types.
|
||||||
|
|
|
||||||
5
src/cmd/doc/testdata/pkg.go
vendored
5
src/cmd/doc/testdata/pkg.go
vendored
|
|
@ -193,3 +193,8 @@ var LongLine = newLongLine(
|
||||||
type T2 int
|
type T2 int
|
||||||
|
|
||||||
type T1 = T2
|
type T1 = T2
|
||||||
|
|
||||||
|
const (
|
||||||
|
Duplicate = iota
|
||||||
|
duplicate
|
||||||
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue