Two-arm match whose catch-all arm is a block, expressible as if let else
ID |
rust.maintainability.single_match_else |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Control Flow |
Language |
Rust |
Description
Reports a match with two arms where the first destructures a variant and the second is a catch-all (_ or None) carrying a block of work. The construct decides one thing, so if let … else expresses it directly.
Rationale
The match form gives the two branches equal visual weight and buries the destructuring in arm syntax, when the code is really a single conditional binding with a fallback. if let puts the bound value in the condition where the reader looks for it, and keeps the fallback where every other else in the function is.
The following code illustrates the pattern detected by this rule:
pub fn wildcard_else(o: Option<i32>) {
// FLAGGED: Two-arm match whose catch-all arm is a block, expressible as if let else
match o {
Some(v) => println!("{}", v),
_ => {
println!("nothing to print");
println!("giving up");
}
}
}