dominikh / go-tools

Staticcheck - The advanced Go linter
https://staticcheck.dev
MIT License
6.09k stars 368 forks source link

stylecheck: decide fate of CheckIncDec #483

Open dominikh opened 5 years ago

dominikh commented 5 years ago

CheckIncDec flags x += 1 and recommends using x++ instead. The check exists, but isn't made available, not even as an optional check. We haven't enabled it so far because it can produce unhelpful warnings for code like the following:

x += 3
...
x += 2
...
x += 1

Either enable the check (optionally with some improvements) or remove it.

bcmills commented 4 years ago

Sadly, my related proposal https://github.com/golang/go/issues/21263 was declined. 😉

mwat56 commented 4 years ago

@dominikh

Either enable the check (optionally with some improvements) or remove it.

I'd say: remove it. Using x++ (but, alas, not ++x) in Go is a bit confusing in the beginning if you've worked with ++x and --x in other languages before because it's a statement in Go instead of an expression as in other languages (not to mention the difference between ++x and x++ and what version Go actually compiles to). So you have to re-learn its use anyway in Go and probably end up with x += 1 so that recommending the use x++ wouldn't actually help. Hence leave it to the programmer and don't interfere with a linter recommendation.