Both branches of an if-else or a conditional expression are the same
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.
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} 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:
[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.