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

use of Result::unwrap_or_else over Result::map_or_else #7363

Closed

Conversation

Valentine-Mario
Copy link
Contributor

This PR is the lint suggested in #7328

@rust-highfive
Copy link

r? @llogiq

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 16, 2021
@llogiq
Copy link
Contributor

llogiq commented Jun 17, 2021

Hey there! I see you have put the basic structure together, now you may want to start fleshing out the test, then implement the actual lint.

Let me know if you need any help.

@Valentine-Mario
Copy link
Contributor Author

Hey there! I see you have put the basic structure together, now you may want to start fleshing out the test, then implement the actual lint.

Let me know if you need any help.

Thank you. I definitely would reach out when I need help

@Valentine-Mario Valentine-Mario marked this pull request as ready for review June 19, 2021 22:06
@Valentine-Mario
Copy link
Contributor Author

@llogiq can you please help me with review

Copy link
Contributor

@llogiq llogiq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel the example suggestion is worse than the code it replaces. Ultimately I don't understand the reasoning behind this lint.

Both map_or_else and unwrap_or_else are about getting the return value, yet your examples only ever return (). Worse, process::exit for control flow is not generally applicable and in most cases misguided.

clippy_lints/src/unwrap_or_else_over_map_or_else.rs Outdated Show resolved Hide resolved
@llogiq
Copy link
Contributor

llogiq commented Jun 20, 2021

Having read the underlying issue, I think the lint was meant to catch an id-implementing closure in the map part of the map_or_else.

@Valentine-Mario
Copy link
Contributor Author

I feel the example suggestion is worse than the code it replaces. Ultimately I don't understand the reasoning behind this lint.

Both map_or_else and unwrap_or_else are about getting the return value, yet your examples only ever return (). Worse, process::exit for control flow is not generally applicable and in most cases misguided.

I've updated the doc and test to give a clearer picture to the lint usage

@llogiq
Copy link
Contributor

llogiq commented Jun 20, 2021

I'm still not happy about the example.

Things I would consider lint-worthy:

a.map_or_else(|_| 0, |n| n) (but only in the absence of type adjustments) or b.map_or_else(|x| something_else(x), std::convert::identity).

Note that both are expressions that return some value (of the Ok-part of the type of a and b, respectively).

As soon as the map part does some operation, I would consider linting the expression a false positive.

@Valentine-Mario
Copy link
Contributor Author

I'm still not happy about the example.

Things I would consider lint-worthy:

a.map_or_else(|_| 0, |n| n) (but only in the absence of type adjustments) or b.map_or_else(|x| something_else(x), std::convert::identity).

Note that both are expressions that return some value (of the Ok-part of the type of a and b, respectively).

As soon as the map part does some operation, I would consider linting the expression a false positive.

Thanks for that. I'm kind of new to contributing, so how do I check if the map part performs any form of operation?

@llogiq
Copy link
Contributor

llogiq commented Jun 21, 2021

For that you need to match on the second argument expression of the map_or_else method call (args[2], because [0] is the receiver). Either it is an ExprPath, then you check if that matches std::convert::identity. Or it is an ExprClosure then you need to get the Body and see if it is an ExprPath with just one segment and the same ident as the closure's sole argument. In this case, check no type adjustments took place (see eta_reduction.rs as a reference).

@flip1995
Copy link
Member

how do I check if the map part performs any form of operation?

With #7330 you can do this with the function clippy_utils::is_expr_identity_function

@camsteffen
Copy link
Contributor

Any thoughts on enhancing map_identity instead of creating a new lint? We could also include Result::map_or, Option::map_or, Option::map_or_else.

@Valentine-Mario
Copy link
Contributor Author

For that you need to match on the second argument expression of the map_or_else method call (args[2], because [0] is the receiver). Either it is an ExprPath, then you check if that matches std::convert::identity. Or it is an ExprClosure then you need to get the Body and see if it is an ExprPath with just one segment and the same ident as the closure's sole argument. In this case, check no type adjustments took place (see eta_reduction.rs as a reference).

Thanks for this. In my case, I checked if it was an ExprKind::Lit, then I checked the adjstments

@llogiq
Copy link
Contributor

llogiq commented Jun 22, 2021

As @flip1995 said, we already have a utils function to find identities. You can simply use that one.

And @camsteffen is right, we should extend existing lints that fit before creating a new one.

@Valentine-Mario
Copy link
Contributor Author

As @flip1995 said, we already have a utils function to find identities. You can simply use that one.

And @camsteffen is right, we should extend existing lints that fit before creating a new one.

Not sure how the extending should work. Since map_identity matches for just map method name

@camsteffen
Copy link
Contributor

Not sure how the extending should work. Since map_identity matches for just map method name

You can just edit the implementation of map_identity to match map_or_else. Ultimately you just need to do span_lint_and_help(cx, MAP_IDENTITY, ..).

@giraffate
Copy link
Contributor

ping from triage @Valentine-Mario. Can you have any update on this?

@bors
Copy link
Collaborator

bors commented Aug 12, 2021

☔ The latest upstream changes (presumably #7516) made this pull request unmergeable. Please resolve the merge conflicts.

@giraffate
Copy link
Contributor

ping from triage @Valentine-Mario. According to the triage procedure, I'm closing this because 2 weeks have passed with no activity. If you have more time to work on this, feel free to reopen this.

@giraffate giraffate closed this Aug 15, 2021
@xFrednet xFrednet added S-inactive-closed Status: Closed due to inactivity and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Aug 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-inactive-closed Status: Closed due to inactivity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants