rust-lang / rust-lang/rust-clippy
Suggest replacing pattern matching with `map` or `map_err`
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
This is in some way an extension to redundant_pattern_matching lint. The lint flags pattern matching that can be trivially converted to map or map_err invocations.
Lint Name
extend redundant_pattern_matching or prefer_map_over_pattern_match
Category
style
Advantage
- Code is shorter
- Less indentation
- Reduced boilerplate of mapping enum cases as is
- In my experience this is a common pattern people new to Rust follow and it helps to discover the alternative
Drawbacks
Depending on the code base this lint can be very intrusive.
Example
let foo = match some_result_returning_fn() {
Ok(val) => Ok(some_fn(val)),
Err(e) => Err(e)
}
Could be written as:
let foo = some_result_returning_fn().map(some_fn);
let foo = match some_option_returning_fn() {
Some(val) => Some(some_fn(val)),
None => None
}
Could be written as:
let foo = some_option_returning_fn().map(some_fn);
let foo = match some_result_returning_fn() {
Ok(val) => Ok(val),
Err(e) => Err(some_fn(e))
}
Could be written as:
let foo = some_option_returning_fn().map_err(some_fn);
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 by reading the existing redundant_pattern_matching lint and compare its behavior with the three Rust examples in this issue. Determine whether the proposal belongs in that lint or a new prefer_map_over_pattern_match lint, then add coverage showing the map and map_err cases are recognized without broad, intrusive suggestions.
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
- Mostly clear
- Newbie friendliness
- 35/100