Match arm that only forwards its binding to a second match
ID |
rust.maintainability.collapsible_match |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Complexity |
Language |
Rust |
Description
Reports a match (or if let) whose arm does nothing but destructure a value and hand the binding straight to a second match or if let, while both levels fall back to the same code. The two patterns describe one nested pattern.
Rationale
The outer level makes no decision of its own: it exists only to peel one layer off the value, so the reader has to hold the outer binding in mind while working out which inner arm applies, and every combination of the two levels needs its own test. Rust patterns nest, so the same decision is expressible once, in one arm, with the compiler still checking exhaustiveness.
The following code illustrates the pattern detected by this rule:
pub fn forwarding_match(r: Result<Option<i32>, ()>) {
// FLAGGED: Match arm that only forwards its binding to a second match
match r {
Ok(val) => match val {
Some(n) => println!("{}", n),
_ => return,
},
_ => return,
}
}