rust-lang / rust-lang/rust-clippy
Improve suggestions for option_map_unit_fn
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
The suggestion for option_map_unit_fn could include more details and options for solving the lint.
Let's consider an invocation chain on Option which consumes the value, if any, at the end. Clippy issues the warning for such a piece of code.
give_me_some_iterator()
.next()
.filter(|&n| n % 2 == 0)
.map(|n| n * 2)
.filter(|&n| n > 100)
.map(|n| println!("And the winning number is… {}", n));
The lint's suggestion in such situations does not look very appealing. Using if let on the whole statement does not improve readability, IMO. I guess that the next thing to try would be using a helper variable:
let n = give_me_some_iterator()
.next()
.filter(|&n| n % 2 == 0)
.map(|n| n * 2)
.filter(|&n| n > 100);
if let Some(found) = n {
println!("And the winning number is… {}", n);
}
Obviously, it breaks the natural chained flow, which does not look very good either. The next step could be:
let _ = give_me_some_iterator()
.next()
.filter(|&n| n % 2 == 0)
.map(|n| n * 2)
.filter(|&n| n > 100)
.map(|n| println!("And the winning number is… {}", n));
Although it is close to the original, the assignment feels like a workaround just to mute Clippy. I was pointed out that there is yet another way, which I – as a newbie – would hardly figure out:
give_me_some_iterator()
.next()
.filter(|&n| n % 2 == 0)
.map(|n| n * 2)
.filter(|&n| n > 100)
.map_or((), |n| println!("And the winning number is… {}", n));
This is quite nice, but I'm not sure how much idiomatic it is considered or how much known and used it is. There is a topic on Rust Internals with a few more details and a proposal of an extension of Option to express better the intent of consuming the value. Anyway, I think that an improved lint would resolve some concerns shown in the discussion and offer an idiomatic alternative that keeps the original readability and compactness.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the option_map_unit_fn lint and inspect how its suggestion is produced for chained Option expressions. Compare the shown if let, helper-variable, let _, and map_or forms, then define and test an improved suggestion that preserves the chain's readability.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100