Fix crash on ill-formed receiver type. Signed-off-by: Joe Tsai <joetsai@google.com>
diff --git a/lint.go b/lint.go index bbfdaf9..3aaaf2a 100644 --- a/lint.go +++ b/lint.go
@@ -1437,14 +1437,19 @@ }) } +// receiverType returns the named type of the method receiver, sans "*", +// or "invalid-type" if fn.Recv is ill formed. func receiverType(fn *ast.FuncDecl) string { switch e := fn.Recv.List[0].Type.(type) { case *ast.Ident: return e.Name case *ast.StarExpr: - return e.X.(*ast.Ident).Name + if id, ok := e.X.(*ast.Ident); ok { + return id.Name + } } - panic(fmt.Sprintf("unknown method receiver AST node type %T", fn.Recv.List[0].Type)) + // The parser accepts much more than just the legal forms. + return "invalid-type" } func (f *file) walk(fn func(ast.Node) bool) {
diff --git a/testdata/receiver-names.go b/testdata/receiver-names.go index e5f3fa2..5fce725 100644 --- a/testdata/receiver-names.go +++ b/testdata/receiver-names.go
@@ -39,3 +39,6 @@ func (_ *bar) f7() { // MATCH /receiver name should not be an underscore/ } + +// Regression test for a panic caused by ill-formed receiver type. +func (recv []*x.y) f()