From 9be0b38f5b87c386812ee70c63a3bfbf27f128ee Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Tue, 14 Oct 2014 12:55:46 -0700 Subject: [PATCH] cmd/vet: don't panic if import fails Initializing the unused variable formatterType (it will be used soon) was panicking if the import couldn't be done, but vet shouldn't be so fragile. LGTM=gri R=gri CC=dsymonds, golang-codereviews https://golang.org/cl/153480044 --- cmd/vet/types.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/cmd/vet/types.go b/cmd/vet/types.go index 5c9fc89a..45c20b03 100644 --- a/cmd/vet/types.go +++ b/cmd/vet/types.go @@ -20,10 +20,17 @@ var imports = make(map[string]*types.Package) var ( stringerMethodType = types.New("func() string") errorType = types.New("error").Underlying().(*types.Interface) - stringerType = importType("fmt", "Stringer").Underlying().(*types.Interface) - formatterType = importType("fmt", "Formatter").Underlying().(*types.Interface) + stringerType = types.New("interface{ String() string }").(*types.Interface) + formatterType *types.Interface ) +func init() { + typ := importType("fmt", "Formatter") + if typ != nil { + formatterType = typ.Underlying().(*types.Interface) + } +} + // importType returns the type denoted by the qualified identifier // path.name, and adds the respective package to the imports map // as a side effect.