Nested Math.min and Math.max have their bounds swapped and always give the same value

tx0/java:inverted-clampdefaultwarningbug

What it finds

Math.min(Math.max(value, 100), 10) always gives 10: the inner call returns at least 100, and the outer one caps it at 10. The bounds are swapped; meant was Math.min(Math.max(value, 10), 100), which keeps value between 10 and 100.

Reported when both bounds are integer literals and the lower bound passed to max is above the upper bound passed to min, in either nesting.

From the rule's source, inverted_clamp.rs.

Example from the rule's tests
class Volume {  int clamp(int v) {    int a = Math.min(Math.max(v, 100), 10);tx0 reports: always gives the same value    int b = Math.max(Math.min(v, 0), 5);tx0 reports: always gives the same value    int c = Math.min(Math.max(v, 10), 100);    int d = Math.max(-5, Math.min(v, -10));tx0 reports: always gives the same value    return a + b + c + d;  }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/java:inverted-clamp" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/java:inverted-clamp] reason="..."

tx0 explain tx0/java:inverted-clamp shows the same in the terminal. Found a false positive? Report it.