Repository: mgechev/revive (go)

Issue:
nested-structs does not detect a slice of structs
**Describe the bug**
Rule `nested-structs` detects this:
```go
type Good struct {
  Field struct{}
}
```
but not this:
```go
type Bad struct {
  Field []struct{}
}
```

**To Reproduce**
Steps to reproduce the behavior:
1. I updated revive `go get -u github.com/mgechev/revive`
2. I run it with the following flags & configuration file:

```shell
$ cat bug.go
package bug

type Good struct {
	Field struct{}
}

type Bad struct {
	Field []struct{}
}
$ revive -config <(echo '[rule.nested-structs]') bug.go
bug.go:4:2: no nested structs are allowed
```

**Expected behavior**
```
$ revive -config <(echo '[rule.nested-structs]') bug.go
bug.go:4:2: no nested structs are allowed
bug.go:8:2: no nested structs are allowed
```

**Desktop (please complete the following information):**
 - OS: Gentoo Linux
 - Go: 1.18

Pull request description:
fix issue 664
Closes #664


Patch:
diff --git a/rule/nested-structs.go b/rule/nested-structs.go
index cfe9648..deb202e 100644
--- a/rule/nested-structs.go
+++ b/rule/nested-structs.go
@@ -47,15 +47,25 @@ func (l *lintNestedStructs) Visit(n ast.Node) ast.Visitor {
 		}
 		return nil
 	case *ast.Field:
-		if _, ok := v.Type.(*ast.StructType); ok {
+		filter := func(n ast.Node) bool {
+			switch n.(type) {
+			case *ast.StructType:
+				return true
+			default:
+				return false
+			}
+		}
+		structs := pick(v, filter, nil)
+		for _, s := range structs {
 			l.onFailure(lint.Failure{
 				Failure:    "no nested structs are allowed",
 				Category:   "style",
-				Node:       v,
+				Node:       s,
 				Confidence: 1,
 			})
-			break
 		}
+		return nil // no need to visit (again) the field
 	}
+
 	return l
 }


Test patch:
diff --git a/testdata/nested-structs.go b/testdata/nested-structs.go
index 3c6a974..7e5abaf 100644
--- a/testdata/nested-structs.go
+++ b/testdata/nested-structs.go
@@ -30,3 +30,8 @@ func fred() interface{} {
 
 	return s
 }
+
+// issue 664
+type Bad struct {
+	Field []struct{} // MATCH /no nested structs are allowed/
+}