An if returns true in one branch and false in the other instead of returning its condition
What it finds
if (valid) { return true; } else { return false; } spends five lines on return valid;. The same holds without the else, when return false; follows the if, and for the opposite, which returns the negated condition.
The fix returns the condition, or its negation. It is unsafe because a condition of type Boolean would then be returned as it is, null included, where the if unboxes it; it is left out when comments would be lost.
From the rule's source, boolean_return_if.rs.
class Checks { boolean valid(int a) { if (a > 0) {tx0 reports: this if returns true and false; return a > 0; says the same return true; } else { return false; } } boolean empty(String s) { if (s.length() > 0)tx0 reports: this if returns false and true; return !(s.length() > 0); says the same return false; return true; } boolean mixed(int a) { if (a > 0) { return true; } return a < -10; }} 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/java:boolean-return-if" = "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/java:boolean-return-if] reason="..."tx0 explain tx0/java:boolean-return-if shows the same in the terminal. Found a false positive? Report it.