rust-lang / rust-lang/rust-clippy
Suggest changing `result.map_or_else(|error| Err(whatever(error)), Ok)` to `result.map_err(whatever)`
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
While refactoring a project of mine, I found that I did something weird:
https://github.com/conqp/ansi-color-codec/commit/f5b90aea8da2f680f4a595ac508102ab267cba5b
However, though running clippy in pedantic mode, it did not nag about me using map_or_else() where I just could have used map_err(). Maybe this is worth a new lint.
Advantage
- Arguably more idiomatic use of
Result's mapping functions - More concise code
Drawbacks
- None
Example
fn read_color_code(&mut self) -> Result<u8, Error> {
let digits = self.read_digits()?;
self.bytes.next(); // Discard bg-color encoded char
digits
.parse::<u8>()
.map_or_else(|error| Err(Error::InvalidCodeValue(error)), Ok)
}
Could be written as:
fn read_color_code(&mut self) -> Result<u8, Error> {
let digits = self.read_digits()?;
self.bytes.next(); // Discard bg-color encoded char
digits.parse::<u8>().map_err(Error::InvalidCodeValue)
}
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 reviewing the Rust examples in the issue and Clippy's existing lint patterns for analogous Result transformations. The work is done when a lint detects the map_or_else(|error| Err(whatever(error)), Ok) form and recommends the equivalent map_err(whatever) form without flagging unrelated code.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100