error-prone icon indicating copy to clipboard operation
error-prone copied to clipboard

Adding or subtracting a number and itself

Open cpovirk opened this issue 11 years ago • 2 comments

Over a year ago, I ran a check over Google code for two patterns:

  • x - x
  • (x + x) / 2 (but probably even x + x is suspicious)

Results are in internal CL 49788955. I see some of the same kinds of things you see for x == x (e.g., foo1.getX() - foo1.getX() instead of foo1.getX() - foo2.getX()). I also see some people using bad date APIs that require January = 0, for which they write 1 - 1 for January, 12 - 1 for December, etc. There may be other patterns where we wouldn't want to do this. Maybe we can just throw out anything with literals?

cpovirk avatar Dec 09 '14 21:12 cpovirk

Making it an error might be too aggressive, but this could be a useful warning.

I agree that we'd probably want to ignore literals, and maybe also floating point.

cushon avatar Jan 23 '15 18:01 cushon

IdentityBinaryExpression exists but currently lets both of the patterns above through. I suspect that we could find a useful heuristic (e.g., "anything with literals"), but I don't know how hard it would end up being.

cpovirk avatar Jun 09 '23 17:06 cpovirk