Nested if whose outer branch holds nothing else
ID |
rust.maintainability.collapsible_if |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Complexity |
Language |
Rust |
Description
Reports an if whose block contains only another if, with neither of the two carrying an else branch. The two conditions can be merged into a single test.
Rationale
The outer if adds a decision point and a level of indentation without expressing a separate decision, because both conditions have to hold before anything runs. Flattening removes a branch from the function’s cyclomatic complexity, shortens the path a reader has to follow, and makes it plain that the two tests belong together.
The following code illustrates the pattern detected by this rule:
pub fn both_set(a: bool, b: bool) {
// FLAGGED: Nested if whose outer branch holds nothing else
if a {
if b {
println!("both");
}
}
}