Avoid panicking on a method-less receiver.
This is malformed Go code that go/parser accepts;
handle the situation gracefully.
Fixes #134.
diff --git a/lint.go b/lint.go
index 0501e2c..7de99bc 100644
--- a/lint.go
+++ b/lint.go
@@ -312,7 +312,7 @@
for _, f := range p.files {
f.walk(func(n ast.Node) bool {
fn, ok := n.(*ast.FuncDecl)
- if !ok || fn.Recv == nil {
+ if !ok || fn.Recv == nil || len(fn.Recv.List) == 0 {
return true
}
// TODO(dsymonds): We could check the signature to be more precise.
@@ -780,7 +780,7 @@
}
kind := "function"
name := fn.Name.Name
- if fn.Recv != nil {
+ if fn.Recv != nil && len(fn.Recv.List) > 0 {
// method
kind = "method"
recv := receiverType(fn)
@@ -1189,7 +1189,7 @@
typeReceiver := map[string]string{}
f.walk(func(n ast.Node) bool {
fn, ok := n.(*ast.FuncDecl)
- if !ok || fn.Recv == nil {
+ if !ok || fn.Recv == nil || len(fn.Recv.List) == 0 {
return true
}
names := fn.Recv.List[0].Names
diff --git a/testdata/broken.go b/testdata/broken.go
new file mode 100644
index 0000000..6543775
--- /dev/null
+++ b/testdata/broken.go
@@ -0,0 +1,9 @@
+// Test of code that is malformed, but accepted by go/parser.
+// See https://golang.org/issue/11271 for discussion.
+// OK
+
+// Package pkg ...
+package pkg
+
+// Foo is a method with a missing receiver.
+func () Foo() {}