Repository: mgechev/revive
Pull request #531 (2021-06-10 18:01:53)

Update module golang.org/x/tools to v0.1.3
[![WhiteSource Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [golang.org/x/tools](https://togithub.com/golang/tools) | require | patch | `v0.1.2` -> `v0.1.3` |

---

### Release Notes

<details>
<summary>golang/tools</summary>

### [`v0.1.3`](https://togithub.com/golang/tools/compare/v0.1.2...v0.1.3)

[Compare Source](https://togithub.com/golang/tools/compare/v0.1.2...v0.1.3)

</details>

---

### Configuration

📅 **Schedule**: At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box.

---

This PR has been generated by [WhiteSource Renovate](https://renovate.whitesourcesoftware.com). View repository job log [here](https://app.renovatebot.com/dashboard#github/mgechev/revive).


Patch:
diff --git a/README.md b/README.md
index 11810f9..fc4d5b3 100644
--- a/README.md
+++ b/README.md
@@ -452,6 +452,7 @@ List of all available rules. The rules ported from `golint` are left unchanged a
 | [`defer`](./RULES_DESCRIPTIONS.md#defer)          |  map   |  Warns on some [defer gotchas](https://blog.learngoprogramming.com/5-gotchas-of-defer-in-go-golang-part-iii-36a1ab3d6ef1)       |    no    |  no   |
 | [`unexported-naming`](./RULES_DESCRIPTIONS.md#unexported-naming)          |  n/a   |  Warns on wrongly named un-exported symbols       |    no    |  no   |
 | [`function-length`](./RULES_DESCRIPTIONS.md#function-length)          |  n/a   |  Warns on functions exceeding the statements or lines max |    no    |  no   |
+| [`nested-structs`](./RULES_DESCRIPTIONS.md#nested-structs)          |  n/a   |  Warns on structs within structs |    no    |  no   |
 
 ## Configurable rules
 
diff --git a/RULES_DESCRIPTIONS.md b/RULES_DESCRIPTIONS.md
index f1bd389..2a8a093 100644
--- a/RULES_DESCRIPTIONS.md
+++ b/RULES_DESCRIPTIONS.md
@@ -45,6 +45,7 @@ List of all available rules.
   - [max-public-structs](#max-public-structs)
   - [modifies-parameter](#modifies-parameter)
   - [modifies-value-receiver](#modifies-value-receiver)
+  - [nested-structs](#nested-structs)
   - [package-comments](#package-comments)
   - [range](#range)
   - [range-val-in-closure](#range-val-in-closure)
@@ -444,6 +445,12 @@ This rule warns when a method modifies its receiver.
 
 _Configuration_: N/A
 
+## nested-structs
+
+_Description_: Packages declaring structs that contain other inline struct definitions can be hard to understand/read for other developers.
+
+_Configuration_: N/A
+
 ## package-comments
 
 _Description_: Packages should have comments. This rule warns on undocumented packages and when packages comments are detached to the `package` keyword.
diff --git a/config/config.go b/config/config.go
index 3231434..6b01003 100644
--- a/config/config.go
+++ b/config/config.go
@@ -79,6 +79,7 @@ var allRules = append([]lint.Rule{
 	&rule.DeferRule{},
 	&rule.UnexportedNamingRule{},
 	&rule.FunctionLength{},
+	&rule.NestedStructs{},
 }, defaultRules...)
 
 var allFormatters = []lint.Formatter{
diff --git a/go.mod b/go.mod
index e85e0b7..4b6c888 100644
--- a/go.mod
+++ b/go.mod
@@ -11,5 +11,5 @@ require (
 	github.com/mitchellh/go-homedir v1.1.0
 	github.com/olekukonko/tablewriter v0.0.5
 	github.com/pkg/errors v0.9.1
-	golang.org/x/tools v0.1.2
+	golang.org/x/tools v0.1.3
 )
diff --git a/go.sum b/go.sum
index 8c53ba9..70b0940 100644
--- a/go.sum
+++ b/go.sum
@@ -70,6 +70,8 @@ golang.org/x/tools v0.1.1 h1:wGiQel/hW0NnEkJUk8lbzkX2gFJU6PFxf1v5OlCfuOs=
 golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
 golang.org/x/tools v0.1.2 h1:kRBLX7v7Af8W7Gdbbc908OJcdgtK8bOz9Uaj8/F1ACA=
 golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
+golang.org/x/tools v0.1.3 h1:L69ShwSZEyCsLKoAxDKeMvLDZkumEe8gXUZAjab0tX8=
+golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
 golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
 golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
 golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
diff --git a/rule/nested-structs.go b/rule/nested-structs.go
new file mode 100644
index 0000000..cfe9648
--- /dev/null
+++ b/rule/nested-structs.go
@@ -0,0 +1,61 @@
+package rule
+
+import (
+	"go/ast"
+
+	"github.com/mgechev/revive/lint"
+)
+
+// NestedStructs lints nested structs.
+type NestedStructs struct{}
+
+// Apply applies the rule to given file.
+func (r *NestedStructs) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
+	var failures []lint.Failure
+
+	if len(arguments) > 0 {
+		panic(r.Name() + " doesn't take any arguments")
+	}
+
+	walker := &lintNestedStructs{
+		fileAST: file.AST,
+		onFailure: func(failure lint.Failure) {
+			failures = append(failures, failure)
+		},
+	}
+
+	ast.Walk(walker, file.AST)
+
+	return failures
+}
+
+// Name returns the rule name.
+func (r *NestedStructs) Name() string {
+	return "nested-structs"
+}
+
+type lintNestedStructs struct {
+	fileAST   *ast.File
+	onFailure func(lint.Failure)
+}
+
+func (l *lintNestedStructs) Visit(n ast.Node) ast.Visitor {
+	switch v := n.(type) {
+	case *ast.FuncDecl:
+		if v.Body != nil {
+			ast.Walk(l, v.Body)
+		}
+		return nil
+	case *ast.Field:
+		if _, ok := v.Type.(*ast.StructType); ok {
+			l.onFailure(lint.Failure{
+				Failure:    "no nested structs are allowed",
+				Category:   "style",
+				Node:       v,
+				Confidence: 1,
+			})
+			break
+		}
+	}
+	return l
+}


Test patch:
diff --git a/test/nested-structs_test.go b/test/nested-structs_test.go
new file mode 100644
index 0000000..733521a
--- /dev/null
+++ b/test/nested-structs_test.go
@@ -0,0 +1,12 @@
+package test
+
+import (
+	"testing"
+
+	"github.com/mgechev/revive/lint"
+	"github.com/mgechev/revive/rule"
+)
+
+func TestNestedStructs(t *testing.T) {
+	testRule(t, "nested-structs", &rule.NestedStructs{}, &lint.RuleConfig{})
+}
diff --git a/testdata/nested-structs.go b/testdata/nested-structs.go
new file mode 100644
index 0000000..3c6a974
--- /dev/null
+++ b/testdata/nested-structs.go
@@ -0,0 +1,32 @@
+package fixtures
+
+type Foo struct {
+	Bar struct { // MATCH /no nested structs are allowed/
+		Baz struct { // MATCH /no nested structs are allowed/
+			b   bool
+			Qux struct { // MATCH /no nested structs are allowed/
+				b bool
+			}
+		}
+	}
+}
+
+type Quux struct {
+	Quuz Quuz
+}
+
+type Quuz struct {
+}
+
+func waldo() (s struct{ b bool }) { return s }
+
+func fred() interface{} {
+	s := struct {
+		b bool
+		t struct { // MATCH /no nested structs are allowed/
+			b bool
+		}
+	}{}
+
+	return s
+}