Commit 18525735 authored by Daniel Martí's avatar Daniel Martí

go/types: sort unused declaration errors

By position, to ensure deterministic output.

Fixes #22525.

Change-Id: I28777d504a622416678b52afd6fc4c3ef32c12af
Reviewed-on: https://go-review.googlesource.com/75090
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: default avatarRobert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 856dccb1
......@@ -292,3 +292,25 @@ func main() {
f("src1", src1)
f("src2", src2)
}
func TestIssue22525(t *testing.T) {
src := `package p; func f() { var a, b, c, d, e int }`
f, err := parser.ParseFile(fset, "", src, 0)
if err != nil {
t.Fatal(err)
}
got := "\n"
conf := Config{Error: func(err error) { got += err.Error() + "\n" }}
conf.Check(f.Name.Name, fset, []*ast.File{f}, nil) // do not crash
want := `
1:27: a declared but not used
1:30: b declared but not used
1:33: c declared but not used
1:36: d declared but not used
1:39: e declared but not used
`
if got != want {
t.Errorf("got: %swant: %s", got, want)
}
}
......@@ -11,6 +11,7 @@ import (
"go/ast"
"go/constant"
"go/token"
"sort"
)
func (check *Checker) funcBody(decl *declInfo, name string, sig *Signature, body *ast.BlockStmt) {
......@@ -57,11 +58,19 @@ func (check *Checker) funcBody(decl *declInfo, name string, sig *Signature, body
}
func (check *Checker) usage(scope *Scope) {
for _, obj := range scope.elems {
if v, _ := obj.(*Var); v != nil && !v.used {
check.softErrorf(v.pos, "%s declared but not used", v.name)
var unused []*Var
for _, elem := range scope.elems {
if v, _ := elem.(*Var); v != nil && !v.used {
unused = append(unused, v)
}
}
sort.Slice(unused, func(i, j int) bool {
return unused[i].pos < unused[j].pos
})
for _, v := range unused {
check.softErrorf(v.pos, "%s declared but not used", v.name)
}
for _, scope := range scope.children {
check.usage(scope)
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment