Skip to content

Commit

Permalink
new rule: nested-structs (#530)
Browse files Browse the repository at this point in the history
new rule: nested-structs
  • Loading branch information
rdeusser authored Jun 15, 2021
1 parent 0ade5dc commit 575ce5f
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions RULES_DESCRIPTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ var allRules = append([]lint.Rule{
&rule.DeferRule{},
&rule.UnexportedNamingRule{},
&rule.FunctionLength{},
&rule.NestedStructs{},
}, defaultRules...)

var allFormatters = []lint.Formatter{
Expand Down
61 changes: 61 additions & 0 deletions rule/nested-structs.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 12 additions & 0 deletions test/nested-structs_test.go
Original file line number Diff line number Diff line change
@@ -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{})
}
32 changes: 32 additions & 0 deletions testdata/nested-structs.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 575ce5f

Please sign in to comment.