increment-decrement
What it does
Section titled “What it does”x += 1 and x -= 1, where x++ and x-- say the same. Only the literal 1 counts. Left out of a language that has no ++.
Why it matters
Section titled “Why it matters”x++ and x-- are the short forms of x += 1 and x -= 1 and are what readers look for in a loop or a counter. Using one form everywhere makes counters easy to find. The rule is off by default because the long form is a valid style in some codebases.
Example
Section titled “Example”package a
func f(x int, xs []int, m *map[string]int) { x += 1 x -= 1 xs[0] += 1 (*m)["k"] += 1 x += 2 x += 01 x *= 1 for i := 0; i < 3; i += 1 { }}
type U map[string]int
func (s *U) M() error { (*s)["called"] += 1 return nil}Configuration
Section titled “Configuration”go: lint: rules: increment-decrement: warn # off (default), warn, errorjs: lint: rules: increment-decrement: warn # off (default), warn, errorEnabled by the presets: go/revive.
Suppress one line with sudsy-disable-next-line increment-decrement in a comment of the file’s language.
Upstream equivalents
Section titled “Upstream equivalents”The same check in other tools; Sudsy reports what each upstream rule reports.
| Tool | Rule | Match |
|---|---|---|
| revive | increment-decrement |
full |