-
-
Notifications
You must be signed in to change notification settings - Fork 377
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
simple: enforce early return in loops #1334
Comments
Related: #187.
It should be fairly simple to implement a check that flags the code. Adding an automated suggested fix would be problematic due to the way go/ast handles comments. However, it won't be a high priority for me in the near future, as I want to concentrate on correctness issues for the time being, not stylistic ones. |
This is pretty straightforward to implement with ruleguard or semgrep. Edit: rules:
- id: loop-nesting
patterns:
- pattern: |
for ... {
if ... {
...
}
}
message: "reduce nesting in loop with continue"
languages: [go]
severity: ERROR Edit: So I ran this on all the Go code on my machine and basically triggered nothing but false positives. Probably there needs to be some logic to ignore blocks with 'break' or 'return' in the indented body, and maybe a minimum indented body length. |
@dominikh great that you agree this check is simple. I'll see if I can find time to make a PR |
Early returns help to reduce nesting levels. I wonder how much effort is it to implement with staticcheck.io. Examples are:
unwanted:
wanted:
The text was updated successfully, but these errors were encountered: