else block after a branch that always diverges
ID |
rust.maintainability.redundant_else |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Redundancy |
Language |
Rust |
Description
Reports an else block whose matching if branch ends in return, break, continue or a panicking macro, so control can only reach the else when the condition is false anyway.
Rationale
The else adds a level of indentation and a second branch to follow for a decision the return has already made, and every statement after it inherits that nesting. Guard clauses read the other way round: the exceptional case is handled and dismissed at the top, and the rest of the function is the normal path at one level of indentation.
The following code illustrates the pattern detected by this rule:
fn classify(value: i32) -> &'static str {
// FLAGGED: else block after a branch that always diverges
if value < 0 {
return "negative";
} else {
"non-negative"
}
}