google / google/error-prone

A comparison operator (< > <= >=) between distinct integral types might hide a bug

Open
#5,331 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
7.2k
Forks
820
Avg merge
5h 9m
Merged PRs (30d)
50

Description

An expression like `someInt < someLong` is at least "fishy" (no offense to our aquatic friends), because assuming that the wider type actually _needs_ to be wider (can't be safely cast down to the narrower type), then this expression will always have the same result every time it executes. This seems to have some reasonable chance of being not-what-the-user-really-intended.

Example - this loop might never terminate:
```
long limit = ...
for (int i = 0 ; i < limit ; i++) {
...
}
```

(EDIT: if either operand is a constant expression, this issue doesn't really apply in the same way; we should reason based on its actual value instead of worrying about ranges. Also, this issue doesn't apply in the same when comparing say an int to a double; I was thinking of integral types.)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.