Both operands of an operator or comparison are the same

tx0/kotlin:identical-operandsdefaultwarningreliability/logicdepth fastbug

What it finds

a && a, x < x, n - n or s == s: an operator 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 must be known not to be one: x != x is the test for NaN, so ==, !=, <=, >=, -, / and % are reported only for variables whose declared type or literal initializer is not Double or Float. Operands that call functions or create objects may give different values each time and are left alone, as are operands without names.

From the rule's source, identical_operands.rs.

Example from the rule's tests
fun compare(a: Int, b: Boolean, x: Double, items: List<Int>) {    if (a == a) println("always")tx0 reports: both operands of == are a    if (b && b) println("redundant")tx0 reports: both operands of && are b    if (a < a) println("never")tx0 reports: both operands of < are a    val zero = a - atx0 reports: both operands of - are a    if (x != x) println("NaN")    if (items.size == items.size) println("calls are left alone")    if (1 == 1) println("constants")    val sum = a + a}
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/kotlin: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/kotlin:identical-operands] reason="..."

tx0 explain tx0/kotlin:identical-operands shows the same in the terminal. Found a false positive? Report it.