Two branches of an if-elif chain have the same body, so their conditions could be joined

tx0/python:repeated-branch-bodyrecommendedwarningmaintainability/duplicationdepth fastmaintainability

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.

Example from the rule's tests
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
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/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.