if/else that yields only the boolean literals true and false
ID |
rust.maintainability.simplify_boolean_expression |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Redundancy |
Language |
Rust |
Description
Reports an if/else whose branches produce nothing but true and false, either as the trailing expression or through return. The condition already is the value being computed.
Rationale
Five lines of branching stand in for the condition itself, so the reader has to check both branches to work out whether the result follows the condition or negates it. The branching also blocks the obvious next step - the expression cannot be combined with &&, || or ! without first being read and rewritten - and an edit that flips one branch silently inverts the result.
The following code illustrates the pattern detected by this rule:
pub fn positive(x: i32) -> bool {
// FLAGGED: if/else that yields only the boolean literals true and false
if x > 0 {
true
} else {
false
}
}