-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new rule: nested-structs
- Loading branch information
Showing
6 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |