Skip to content

Commit

Permalink
Clarify why our check for < and friends works.
Browse files Browse the repository at this point in the history
(very belated followup to #274)

PiperOrigin-RevId: 703135821
  • Loading branch information
cpovirk authored and Error Prone Team committed Dec 5, 2024
1 parent 8ec552e commit 53c638d
Showing 1 changed file with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ Description matchConstantResult() {
return switch (tree.getKind()) {
case EQUAL_TO -> matchOutOfBounds(/* willEvaluateTo= */ false);
case NOT_EQUAL_TO -> matchOutOfBounds(/* willEvaluateTo= */ true);
case LESS_THAN -> matchMinAndMaxHaveSameResult(cmp -> cmp < 0);
case LESS_THAN_EQUAL -> matchMinAndMaxHaveSameResult(cmp -> cmp <= 0);
case GREATER_THAN -> matchMinAndMaxHaveSameResult(cmp -> cmp > 0);
case GREATER_THAN_EQUAL -> matchMinAndMaxHaveSameResult(cmp -> cmp >= 0);
case LESS_THAN -> matchDoesNotSplitNumberLine(cmp -> cmp < 0);
case LESS_THAN_EQUAL -> matchDoesNotSplitNumberLine(cmp -> cmp <= 0);
case GREATER_THAN -> matchDoesNotSplitNumberLine(cmp -> cmp > 0);
case GREATER_THAN_EQUAL -> matchDoesNotSplitNumberLine(cmp -> cmp >= 0);
default -> NO_MATCH;
};
}
Expand All @@ -118,13 +118,14 @@ Description matchOutOfBounds(boolean willEvaluateTo) {
}

/*
* If `minValue < constant` and `maxValue < constant` are both true, then `anything <
* constant` is true.
* A proper "<" comparison to `constant` splits the number line into two pieces: On one
* side, the comparison is true; on the other, false. (Ditto for "<=," ">," and ">=.")
*
* The same holds if we replace "<" with another inequality operator, if we replace
* "true" with "false," or if we move "constant" to the left operand.
* So, if the comparison returns the *same* value for the far left side of the number
* line as it does for the far right side, then the comparison is out of range (e.g.,
* "someInt < Long.MAX_VALUE").
*/
Description matchMinAndMaxHaveSameResult(IntPredicate op) {
Description matchDoesNotSplitNumberLine(IntPredicate op) {
boolean minResult;
boolean maxResult;
if (constant == tree.getRightOperand()) {
Expand Down

0 comments on commit 53c638d

Please sign in to comment.