Two branches of an if-elif chain have the same body, so their conditions could be joined
What it finds
When two branches of a chain do exactly the same, either one of them was meant to differ or the conditions can be combined with or. Bodies of a single simple statement such as pass, continue or return are common and left alone, as is a chain whose branches are all the same, which another rule reports.
From the rule's source, repeated_branch_body.rs.
def kind(x): if x == 1: log("one") return "small" elif x == 2: log("two") return "medium" elif x == 3: log("one")tx0 reports: this branch does the same as an earlier one return "small" else: return "large" def simple(x): if x == 1: pass elif x == 2: return None elif x == 3: pass 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/python:repeated-branch-body" = "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/python:repeated-branch-body] reason="..."tx0 explain tx0/python:repeated-branch-body shows the same in the terminal. Found a false positive? Report it.