1
0
mirror of https://github.com/golang/go synced 2024-09-30 20:28:32 -06:00

go/analysis: use TypeString when matching types

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í <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
Daniel Martí 2018-11-16 15:51:55 +00:00
parent 70b12541d3
commit 139d099f66
2 changed files with 10 additions and 2 deletions

View File

@ -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
}

View File

@ -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\)`
}