Repository navigation
rule: add no-increment-decrement rule - #1771
ChrisJr404 wants to merge 2 commits into
Conversation
Add a new rule that is the opposite of increment-decrement: it flags `i++` and `i--` statements and suggests replacing them with `i += 1` and `i -= 1`. Increment/decrement statements used as the post statement of a `for` loop are left untouched, since that is the idiomatic place for a loop counter. The rule is opt-in like every other revive rule, so default behaviour is unchanged. Closes revive-lint#910
chavacava
left a comment
There was a problem hiding this comment.
Hi @ChrisJr404, thanks for the PR.
I've left some comments.
| if stmt.Post != nil { | ||
| // The post statement of a for loop is the idiomatic place for a loop | ||
| // counter, so it is exempted from the rule. | ||
| w.loopPosts[stmt.Post] = struct{}{} | ||
| } |
There was a problem hiding this comment.
I think it's possible to handle the special case of increments in the loop post without keeping track of loopPosts in a list: when visiting a forStmt launch the visitor on the for body and then return nil; this will make the visitor to skip the post part of the for.
Something like:
| if stmt.Post != nil { | |
| // The post statement of a for loop is the idiomatic place for a loop | |
| // counter, so it is exempted from the rule. | |
| w.loopPosts[stmt.Post] = struct{}{} | |
| } | |
| w.Visit(stmt.Body) //maybe a check stmt.Body != nil is necessary before calling Visit | |
| return nil |
| } | ||
|
|
||
| w.onFailure(lint.Failure{ | ||
| Confidence: 0.8, |
There was a problem hiding this comment.
confidence could be 1, here we are sure it's an increment/decrement that should be replaced
| for j := 10; j > 0; j-- { | ||
| _ = j | ||
| } |
There was a problem hiding this comment.
It could be interesting to test with an increment in a for body
| for j := 10; j > 0; j-- { | |
| _ = j | |
| } | |
| for j := 10; j > 0; j-- { | |
| _ = j | |
| } | |
| x := 0 | |
| for j := 10; j > 0; j-- { | |
| _ = j | |
| x++ // MATCH /should replace x++ with x += 1/ | |
| x-- // MATCH /should replace x-- with x -= 1/ | |
| } |
|
@ChrisJr404 please also include the rule in the rule list table on the README.md |
|
Good catch - added the rule to the README rule list table (between |
|
I have a problem here. First, this rule is purely stylistic. Some may like it, some may not. I feel like 99% of people will be in the second team. But the real problem is the fact once merged this rule will be part of revive enable all rules settings. And then it would be enabled for anyone. For me, this should be a linter independent from revive. The only possible way according to me would be that this feature becomes a setting of an existing revive rule. So here, I feel a setting of increment-decrement should be the way. Also, if we now consider there is a rule for using This is the feedback I do, and I'm looking for maintainers feedback. Do not rush into doing changes. |
This adds
no-increment-decrement, a new rule that is the mirror image of the existingincrement-decrementrule, for people who prefer the expliciti += 1/i -= 1forms overi++/i--.As raised in #910,
increment-decrementis opinionated in the direction of++/--, but plenty of styles (and languages) go the other way, and in practice++/--are rarely used in Go outside of loop counters. The new rule spotsi++andi--statements and proposes replacing them withi += 1andi -= 1. Increment/decrement statements that appear as the post statement of aforloop are intentionally left alone, since that is the idiomatic place for a loop counter (see thefor i := 0; i < n; i++example in the docs).Like every other revive rule it is opt-in, so the default behaviour is unchanged. It works purely on the AST and requires no type information, so it is registered in
untyped.toml.Naming: I went with
no-increment-decrementto make it easy to find as the counterpart ofincrement-decrement, but I'm happy to rename it if you'd prefer something else.Changes:
rule/no_increment_decrement.go— the rule.test/no_increment_decrement_test.go+testdata/no_increment_decrement.go— fixtures covering standalone++/--, a selector operand, a counter incremented inside a loop body (flagged), andfor-loop counters (not flagged).config/config.goanduntyped.toml, bumped the rule count inconfig/config_test.go, and documented it inRULES_DESCRIPTIONS.md.Testing:
go test ./...passes, and bothrevive --config revive.toml ./...andgolangci-lint runare clean on the new files.Closes #910