-
Notifications
You must be signed in to change notification settings - Fork 289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
new rule: nested-structs #530
Conversation
Tests pass for me locally. CI failed with this error: |
Hi (again) @rdeusser and thanks for the PR. To add a new rule to the revive machinery it must be added to the list of rules in the configuration. Updates of the |
@chavacava Hi! Added the rule to the list of rules in the config and updated the README as well as the RULES_DESCRIPTION. |
Nice, thanks. |
Hey @chavacava I think I got everything. Can you re-review? |
I've tested the rule against some codbases (kebernetes, docker), it works well. The rule spots cases like the following (from func (es EditScript) stats() (s struct{ NI, NX, NY, NM int }) { and like (from func doTestPlugin(scenario struct {
name string
vol *v1.Volume
expecteds []expectedCommand
isExpectedFailure bool
}, t *testing.T) []error { Beyond that it seems to me very hard to read, it is not a nested struct. Should we warn on cases like that? If yes, the rule is not about nested structs but something else. We can also let as is (the above cases are very rare) In the test cases we could add a case for multiple nests: type Two struct {
Bar struct { // MATCH /no nested structs are allowed/
Baz struct { // MATCH /no nested structs are allowed/
b boolean
Foo struct { // MATCH /no nested structs are allowed/
b boolean
}
}
}
} |
Hey @chavacava apologies for the late reply. Yeah those cases are a bit odd and I think there should be a rule for that. I'm of the opinion that this rule should cover nested structs and nothing else and a separate rule (potentially) for the examples you found above to maximize flexibility. I'd like to know your thoughts on this rule covering both use-cases (assuming the rule would be renamed) or remain as-is? |
I went ahead and made the change to ignore structs in func declarations and added some additional test cases for that until I hear back from you. |
rule/nested-structs.go
Outdated
case *ast.FuncDecl: | ||
return nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
case *ast.FuncDecl: | |
return nil | |
case *ast.FuncDecl: | |
if v.Body != nil { | |
ast.Walk(l, v.Body) | |
} | |
return nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oof, yeah didn't see that. Thanks.
Hi @rdeusser thanks for the new commit and sorry for the late response. The new version skips function declarations completely (function bodies are not analyzed) therefore we will not detect nested structs declared in function bodies (they were a large portion of failures detected by the previous version in code bases like kubernetes and docker) I've left a suggestion in the code to let the rule analyze function bodies while skipping function signatures. |
@chavacava Thanks for the review! I got that fixed and added an additional test case for what you mentioned. |
Thanks @rdeusser |
Please, describe in details what's your motivation for this PR
Nested structs are awful to look at and I want to disallow them entirely in my projects.
Did you add tests?
Yes.
Does your code follows the coding style of the rest of the repository?
Yes.
Does the Travis build passes?
TBD
Closes #529