A condition of an if-else chain repeats an earlier one

tx0/ts:duplicate-conditiondefaultwarningbug

What it finds

if (a) {} else if (a) {}: a condition that repeats an earlier one of the same chain is false whenever it is reached, so its branch never runs. Usually one of the two was meant to test something else.

Between two conditions of a chain only the conditions run, so the chain is followed as long as none of them can change what a later one reads: it ends at a condition that calls, constructs, awaits, yields or assigns anything.

From the rule's source, duplicate_condition.rs.

Example from the rule's tests
function grade(score: number, user: { admin: boolean }, poll: () => boolean) {  if (score > 90) {    return "a";  } else if (score > 80) {    return "b";  } else if (score > 90) {tx0 reports: score > 90 was already tested    return "c";  }  if (user.admin) {    return "admin";  } else if (poll()) {    return "polled";  } else if (poll()) {    return "again";  }  return "none";}
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/ts:duplicate-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/ts:duplicate-condition] reason="..."

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