1
0
mirror of https://github.com/golang/go synced 2024-10-02 02:28:32 -06:00

cmd/vet: replace hasMethod with isFormatter

The former checks if a type has a method called "Format". The latter
checks if a type satisfies fmt.Formatter.

isFormatter does exactly what we want, so it's both simpler and more
accurate. Remove the only use of hasMethod in its favor.

Change-Id: Idc156a99081c3308f98512b87011a04aa8c6638d
Reviewed-on: https://go-review.googlesource.com/91215
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
Daniel Martí 2018-01-31 20:53:44 +00:00
parent 7ddd467ef3
commit c0b248c234
2 changed files with 1 additions and 13 deletions

View File

@ -657,8 +657,7 @@ func (f *File) recursiveStringer(e ast.Expr) bool {
// It's unlikely to be a recursive stringer if it has a Format method. // It's unlikely to be a recursive stringer if it has a Format method.
if typ := f.pkg.types[e].Type; typ != nil { if typ := f.pkg.types[e].Type; typ != nil {
// Not a perfect match; see issue 6259. if f.isFormatter(typ) {
if f.hasMethod(typ, "Format") {
return false return false
} }
} }

View File

@ -310,15 +310,4 @@ func (f *File) matchStructArgType(t printfArgType, typ *types.Struct, arg ast.Ex
return true return true
} }
// hasMethod reports whether the type contains a method with the given name.
// It is part of the workaround for Formatters and should be deleted when
// that workaround is no longer necessary.
// TODO: This could be better once issue 6259 is fixed.
func (f *File) hasMethod(typ types.Type, name string) bool {
// assume we have an addressable variable of type typ
obj, _, _ := types.LookupFieldOrMethod(typ, true, f.pkg.typesPkg, name)
_, ok := obj.(*types.Func)
return ok
}
var archSizes = types.SizesFor("gc", build.Default.GOARCH) var archSizes = types.SizesFor("gc", build.Default.GOARCH)