match or if let that reimplements Option::map
ID |
rust.maintainability.manual_map |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Idiom |
Language |
Rust |
Description
Reports a match or if let over an Option whose Some arm rewraps a value in Some and whose None arm yields None - the exact behaviour of Option::map.
Rationale
Six lines of control flow stand in for one combinator, so a reader has to check both arms to confirm that the None case really is passed through unchanged, and the compiler cannot help if a later edit makes the arms disagree. The map form states the intent in one line and keeps the transformation, rather than the plumbing, in view.
The following code illustrates the pattern detected by this rule:
pub fn match_form(o: Option<i32>) -> Option<i32> {
// FLAGGED: match or if let that reimplements Option::map
match o {
Some(v) => Some(v + 1),
None => None,
}
}