else block that wraps a single if instead of using else if
ID |
rust.maintainability.collapsible_else_if |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Readability |
Language |
Rust |
Description
Reports an else block whose whole content is one if (or if let) expression, a shape Rust writes as else if.
Rationale
Wrapping the if in a block hides that the chain is a single sequence of alternatives: every further alternative adds another level of indentation, so a chain of four tests ends up nested four deep and the reader has to check each closing brace to work out which alternative a branch belongs to. The else if form keeps the whole chain flat and at one indentation level.
The following code illustrates the pattern detected by this rule:
pub fn plain(a: bool, b: bool) {
// FLAGGED: else block that wraps a single if instead of using else if
if a {
println!("a");
} else {
if b {
println!("b");
}
}
}