An if returns True in one branch and False in the other, instead of returning the condition

tx0/python:if-return-boolstrictnotestyle

What it finds

if x > 0: return True else: return False spells out what return x > 0 says. When the condition is not already a bool, return bool(...) keeps the result the same. Reported are if statements without elif whose if and else blocks each consist of one return of opposite boolean literals.

From the rule's source, if_return_bool.rs.

Example from the rule's tests
def positive(x):    if x > 0:tx0 reports: this if returns True or False        return True    else:        return False def odd(x):    if x % 2:        return True    elif x < 0:        return False    else:        return True def same(x):    if x:        return True    else:        return True
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 strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/python:if-return-bool" = "note"

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:if-return-bool] reason="..."

tx0 explain tx0/python:if-return-bool shows the same in the terminal. Found a false positive? Report it.