A condition in a chain of if and else if repeats an earlier one, so its branch never runs

tx0/kotlin:duplicate-if-conditiondefaultwarningreliability/control-flowdepth fastbug

What it finds

if (a) ... else if (b) ... else if (a) ...: the third branch runs only when a holds, but then the first branch has already run. Usually one condition was meant to be different. Conditions that call functions are left alone, as they may give different results.

From the rule's source, duplicate_if_condition.rs.

Example from the rule's tests
fun describe(code: Int, flag: Boolean): String {    return if (code == 1) {        "one"    } else if (flag) {        "flag"    } else if (code == 1) {tx0 reports: the condition code == 1 repeats an earlier one        "again"    } else if (next() == 1) {        "call"    } else if (next() == 1) {        "call again"    } else {        "other"    }}
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:duplicate-if-condition" = "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:duplicate-if-condition] reason="..."

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