Both operands of an operator or comparison are the same
What it finds
a && a, x < x, n - n, s.equals(s): an operator or comparison applied to the same operand twice gives a fixed or redundant result, which usually means one side was meant to be something else.
Where floating-point numbers behave differently, the operand's type must be known not to be one: x != x is the test for NaN, and x - x is zero only for finite x, so ==, !=, <=, >=, -, / and % are reported only for operands whose declared type is integral, boolean or a reference. Left out are + and *, which double and square, and the shifts. Operands that call methods, create objects or assign may give different values each time, and so may volatile fields; operands without names are constants that read as intended. A comparison among the arguments of a method whose name starts with assert, as in a test of equals itself, is left alone.
From the rule's source, identical_operands.rs.
import java.util.Objects; class Ranges { int lower; volatile int state; boolean check(int a, long b, double d, Float f, String s, Object o, boolean flag) { boolean r = a < a;tx0 reports: both operands of < are a r = flag && flag;tx0 reports: both operands of && are flag r = a == a;tx0 reports: both operands of == are a r = s != s;tx0 reports: both operands of != are s long zero = b - b;tx0 reports: both operands of - are b r = this.lower >= this.lower;tx0 reports: both operands of >= are this.lower r = d != d; r = f == f; double nan = d - d; r = d < d;tx0 reports: both operands of < are d r = state == state; r = next() == next(); r = a + a > 0 || a * a > 0; r = 1 == 1; r = o.equals(o);tx0 reports: o is compared with itself r = Objects.equals(s, s);tx0 reports: s is compared with itself r = o.equals(s); assertTrue(o.equals(o)); return r; } int next() { return lower++; } static void assertTrue(boolean value) {}} 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:
[rules]
"tx0/java:identical-operands" = "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:identical-operands] reason="..."tx0 explain tx0/java:identical-operands shows the same in the terminal. Found a false positive? Report it.