From: Daniel Martí Date: Wed, 1 Nov 2017 15:47:46 +0000 (+0000) Subject: go/types: sort unused declaration errors X-Git-Tag: go1.10beta1~473 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=185257352189939e77de0172e17aadce2c355704;p=gostls13.git 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í Reviewed-by: Robert Griesemer TryBot-Result: Gobot Gobot --- diff --git a/src/go/types/issues_test.go b/src/go/types/issues_test.go index 3884735118..02af0cf51b 100644 --- a/src/go/types/issues_test.go +++ b/src/go/types/issues_test.go @@ -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) + } +} diff --git a/src/go/types/stmt.go b/src/go/types/stmt.go index f4feabefdd..618d1e5fbf 100644 --- a/src/go/types/stmt.go +++ b/src/go/types/stmt.go @@ -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) }