Both branches of an if-else or a conditional expression are the same

tx0/c:identical-branchesrecommendedwarningbug

What it finds

if (c) f(x); else f(x); and c ? a : a: both branches do the same, so the condition decides nothing. Usually one branch was copied and not changed.

An else if is not compared with the branch before it, only an else that ends a chain. Branches with a directive inside are left alone, since what is compiled in each may differ, and so are empty ones.

From the rule's source, identical_branches.rs.

Example from the rule's tests
void log_value(int v); int choose(int c, int a, int b){  if (c)tx0 reports: both branches of the if do the same    log_value(a);  else    log_value(a);   if (c > 1) {tx0 reports: both branches of the if do the same    a++;    b--;  } else {    a++;    b--;  }   if (c)    a = 1;  else if (b)    a = 1;   if (c)    a = 2;  else    a = 3;   if (c) {  } else {  }   if (c)    log_value(a);#ifdef DEBUG  else    log_value(a);#endif   return c ? a : a;tx0 reports: both branches of the conditional expression are 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 from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/c:identical-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/c:identical-branches] reason="..."

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