From 139d099f662071f73eab0df7b7fdf9682c8f9e91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Fri, 16 Nov 2018 15:51:55 +0000 Subject: [PATCH] go/analysis: use TypeString when matching types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As Alan rightfully guessed, porting the stdmethods check to use go/types required the use of types.TypeString not only when printing signatures in warnings, but also when matching them. Added a simple test case too. Fixes golang/go#28792. Change-Id: Ifbbdd4b1a2f1090d6f9a1674d52b8f0887a67d06 Reviewed-on: https://go-review.googlesource.com/c/149977 Run-TryBot: Daniel Martí TryBot-Result: Gobot Gobot Reviewed-by: Alan Donovan --- go/analysis/passes/stdmethods/stdmethods.go | 8 ++++++-- go/analysis/passes/stdmethods/testdata/src/a/a.go | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/go/analysis/passes/stdmethods/stdmethods.go b/go/analysis/passes/stdmethods/stdmethods.go index b61c3220..83495112 100644 --- a/go/analysis/passes/stdmethods/stdmethods.go +++ b/go/analysis/passes/stdmethods/stdmethods.go @@ -131,7 +131,7 @@ func canonicalMethod(pass *analysis.Pass, id *ast.Ident) { expectFmt += " (" + argjoin(expect.results) + ")" } - actual := types.TypeString(sign, (*types.Package).Name) + actual := typeString(sign) actual = strings.TrimPrefix(actual, "func") actual = id.Name + actual @@ -139,6 +139,10 @@ func canonicalMethod(pass *analysis.Pass, id *ast.Ident) { } } +func typeString(typ types.Type) string { + return types.TypeString(typ, (*types.Package).Name) +} + func argjoin(x []string) string { y := make([]string, len(x)) for i, s := range x { @@ -178,5 +182,5 @@ func matchParamType(fset *token.FileSet, pkg *types.Package, expect string, actu } // Overkill but easy. - return actual.String() == expect + return typeString(actual) == expect } diff --git a/go/analysis/passes/stdmethods/testdata/src/a/a.go b/go/analysis/passes/stdmethods/testdata/src/a/a.go index 9833f8fd..de240a3b 100644 --- a/go/analysis/passes/stdmethods/testdata/src/a/a.go +++ b/go/analysis/passes/stdmethods/testdata/src/a/a.go @@ -24,6 +24,10 @@ func (U) GobDecode() {} // want `should have signature GobDecode\(\[\]byte\) err // Test rendering of type names such as xml.Encoder in diagnostic. func (U) MarshalXML(*xml.Encoder) {} // want `method MarshalXML\(\*xml.Encoder\) should...` +func (U) UnmarshalXML(*xml.Decoder, xml.StartElement) error { // no error: signature matches xml.Unmarshaler + return nil +} + type I interface { ReadByte() byte // want `should have signature ReadByte\(\) \(byte, error\)` }