Both branches of an if do the same thing

tx0/kotlin:identical-if-branchesrecommendedwarningreliability/control-flowdepth fastbug

What it finds

if (fast) { run(x) } else { run(x) }: the condition makes no difference, which usually means that one branch was copied and not changed. Chains with else if are compared at their last link only.

From the rule's source, identical_if_branches.rs.

Example from the rule's tests
fun run(fast: Boolean, x: Int) {    if (fast) {tx0 reports: both branches of the if are the same        work(x)    } else {        work(x)    }    val y = if (fast) x + 1 else x + 1tx0 reports: both branches of the if are the same    if (fast) work(x) else work(x + 1)    if (fast) { } else { }}
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 from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/kotlin:identical-if-branches" = "warning"

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-if-branches] reason="..."

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