Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New lint: suggest the use of Result::unwrap_or_else over Result::map_or_else #7328

Closed
Tracked by #79
jhscheer opened this issue Jun 6, 2021 · 4 comments · Fixed by #12169
Closed
Tracked by #79

New lint: suggest the use of Result::unwrap_or_else over Result::map_or_else #7328

jhscheer opened this issue Jun 6, 2021 · 4 comments · Fixed by #12169
Assignees
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy

Comments

@jhscheer
Copy link

jhscheer commented Jun 6, 2021

What it does

Suggest the use of Result::unwrap_or_else over Result::map_or_else if map_or_else is just used to unpack a successful result while handling an error.

Categories (optional)

What is the advantage of the recommended code over the original code

Code will be shorter (and cleaner).

Drawbacks

None.

Example

.map_or_else(
    |e| handle_the_error(e),
    |n| n,
)

Could be written as:

.unwrap_or_else(|e| handle_the_error(e))
@jhscheer jhscheer added the A-lint Area: New lints label Jun 6, 2021
@camsteffen
Copy link
Contributor

I think an enhancement to map_identity would do.

@camsteffen camsteffen added C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy and removed A-lint Area: New lints labels Jun 10, 2021
@Valentine-Mario
Copy link
Contributor

@rustbot claim

@KisaragiEffective
Copy link
Contributor

I've found another case.

let res: Result<usize, usize> = foo();
let v: usize = foo.map_or_else(|x| x, |x| x);

can be following code:

let v: usize = foo.unwrap_or_else(|x| x)

@GuillaumeGomez
Copy link
Member

Adding a lint for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants