Skip to content

Commit 575ce5f

Browse files
authored
new rule: nested-structs (#530)
new rule: nested-structs
1 parent 0ade5dc commit 575ce5f

File tree

6 files changed

+114
-0
lines changed

6 files changed

+114
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ List of all available rules. The rules ported from `golint` are left unchanged a
452452
| [`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 |
453453
| [`unexported-naming`](./RULES_DESCRIPTIONS.md#unexported-naming) | n/a | Warns on wrongly named un-exported symbols | no | no |
454454
| [`function-length`](./RULES_DESCRIPTIONS.md#function-length) | n/a | Warns on functions exceeding the statements or lines max | no | no |
455+
| [`nested-structs`](./RULES_DESCRIPTIONS.md#nested-structs) | n/a | Warns on structs within structs | no | no |
455456

456457
## Configurable rules
457458

RULES_DESCRIPTIONS.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ List of all available rules.
4545
- [max-public-structs](#max-public-structs)
4646
- [modifies-parameter](#modifies-parameter)
4747
- [modifies-value-receiver](#modifies-value-receiver)
48+
- [nested-structs](#nested-structs)
4849
- [package-comments](#package-comments)
4950
- [range](#range)
5051
- [range-val-in-closure](#range-val-in-closure)
@@ -444,6 +445,12 @@ This rule warns when a method modifies its receiver.
444445

445446
_Configuration_: N/A
446447

448+
## nested-structs
449+
450+
_Description_: Packages declaring structs that contain other inline struct definitions can be hard to understand/read for other developers.
451+
452+
_Configuration_: N/A
453+
447454
## package-comments
448455

449456
_Description_: Packages should have comments. This rule warns on undocumented packages and when packages comments are detached to the `package` keyword.

config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ var allRules = append([]lint.Rule{
7979
&rule.DeferRule{},
8080
&rule.UnexportedNamingRule{},
8181
&rule.FunctionLength{},
82+
&rule.NestedStructs{},
8283
}, defaultRules...)
8384

8485
var allFormatters = []lint.Formatter{

rule/nested-structs.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package rule
2+
3+
import (
4+
"go/ast"
5+
6+
"github.com/mgechev/revive/lint"
7+
)
8+
9+
// NestedStructs lints nested structs.
10+
type NestedStructs struct{}
11+
12+
// Apply applies the rule to given file.
13+
func (r *NestedStructs) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
14+
var failures []lint.Failure
15+
16+
if len(arguments) > 0 {
17+
panic(r.Name() + " doesn't take any arguments")
18+
}
19+
20+
walker := &lintNestedStructs{
21+
fileAST: file.AST,
22+
onFailure: func(failure lint.Failure) {
23+
failures = append(failures, failure)
24+
},
25+
}
26+
27+
ast.Walk(walker, file.AST)
28+
29+
return failures
30+
}
31+
32+
// Name returns the rule name.
33+
func (r *NestedStructs) Name() string {
34+
return "nested-structs"
35+
}
36+
37+
type lintNestedStructs struct {
38+
fileAST *ast.File
39+
onFailure func(lint.Failure)
40+
}
41+
42+
func (l *lintNestedStructs) Visit(n ast.Node) ast.Visitor {
43+
switch v := n.(type) {
44+
case *ast.FuncDecl:
45+
if v.Body != nil {
46+
ast.Walk(l, v.Body)
47+
}
48+
return nil
49+
case *ast.Field:
50+
if _, ok := v.Type.(*ast.StructType); ok {
51+
l.onFailure(lint.Failure{
52+
Failure: "no nested structs are allowed",
53+
Category: "style",
54+
Node: v,
55+
Confidence: 1,
56+
})
57+
break
58+
}
59+
}
60+
return l
61+
}

test/nested-structs_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/mgechev/revive/lint"
7+
"github.com/mgechev/revive/rule"
8+
)
9+
10+
func TestNestedStructs(t *testing.T) {
11+
testRule(t, "nested-structs", &rule.NestedStructs{}, &lint.RuleConfig{})
12+
}

testdata/nested-structs.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package fixtures
2+
3+
type Foo struct {
4+
Bar struct { // MATCH /no nested structs are allowed/
5+
Baz struct { // MATCH /no nested structs are allowed/
6+
b bool
7+
Qux struct { // MATCH /no nested structs are allowed/
8+
b bool
9+
}
10+
}
11+
}
12+
}
13+
14+
type Quux struct {
15+
Quuz Quuz
16+
}
17+
18+
type Quuz struct {
19+
}
20+
21+
func waldo() (s struct{ b bool }) { return s }
22+
23+
func fred() interface{} {
24+
s := struct {
25+
b bool
26+
t struct { // MATCH /no nested structs are allowed/
27+
b bool
28+
}
29+
}{}
30+
31+
return s
32+
}

0 commit comments

Comments
 (0)