Skip to content

increment-decrement

GoJavaScript / TypeScriptstyleno fixdefault: offgo/revive

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 ++.

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.

Reported
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
}
go:
lint:
rules:
increment-decrement: warn # off (default), warn, error
js:
lint:
rules:
increment-decrement: warn # off (default), warn, error

Enabled by the presets: go/revive.

Suppress one line with sudsy-disable-next-line increment-decrement in a comment of the file’s language.

The same check in other tools; Sudsy reports what each upstream rule reports.

Tool Rule Match
revive increment-decrement full