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

tx0/c: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 conditions run, so the chain is followed as long as no condition can change what a later one reads: it ends at a condition that calls something, which may be a macro with effects, assigns, increments or decrements. Names defined as macros in the file and names of volatile objects end it too, and so does a conditional directive between two conditions, since the two may never be compiled together.

From the rule's source, duplicate_condition.rs.

Example from the rule's tests
#define READY (status & 1) extern volatile int hw;int poll(void); int classify(int n, int status, const char *s){  if (n == 1)    return 1;  else if (n == 2)    return 2;  else if (n == 1)tx0 reports: the condition n == 1 repeats an earlier one of the chain    return 3;   if (s[0] == 'a') {    return 4;  } else if (s[0] == 'b') {    return 5;  } else if ((s[0] == 'a')) {tx0 reports: the condition s[0] == 'a' repeats an earlier one    return 6;  }   if (poll())    return 7;  else if (poll())    return 8;   if (READY)    return 9;  else if (READY)    return 10;   if (hw)    return 11;  else if (hw)    return 12;   if (n++ > 3)    return 13;  else if (n++ > 3)    return 14;  return 0;}
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/c: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/c:duplicate-condition] reason="..."

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