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

Add real suggestion to option_map_unwrap_or #4634

Merged
merged 1 commit into from
Dec 30, 2019

Conversation

m-ober
Copy link
Contributor

@m-ober m-ober commented Oct 5, 2019

changelog: Add real suggestion to option_map_unwrap_or

Fixes #2320

@flip1995 flip1995 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Oct 5, 2019
clippy_lints/src/methods/option_map_unwrap_or.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/option_map_unwrap_or.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/option_map_unwrap_or.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/option_map_unwrap_or.rs Outdated Show resolved Hide resolved
@llogiq llogiq self-assigned this Oct 5, 2019
@llogiq
Copy link
Contributor

llogiq commented Oct 5, 2019

I'm afraid even with the above suggestions, we will still have to wait for the rustup.

@llogiq
Copy link
Contributor

llogiq commented Oct 9, 2019

Now that the rustup is through, we can continue.

@m-ober
Copy link
Contributor Author

m-ober commented Oct 14, 2019

Thanks for the feedback @llogiq - I applied your suggestions. I also added the macro context check, doing nothing in case they differ. Not sure if this is the correct behaviour (but it looks like this is how it's handled in other tests).

@llogiq
Copy link
Contributor

llogiq commented Oct 14, 2019

Ok, the build is blocked on the rustfmt check. You can call util/dev fmt from the clippy project root to call rustfmt on all subprojects. Also please prepend a //run-rustfix line to the UI test, run cargo test and bless the result (this will call rustfix to actually apply our suggestion). Otherwise it looks good. Good job for a first-time contribution! 👍

@m-ober
Copy link
Contributor Author

m-ober commented Oct 15, 2019

Thanks again. Should be good now 😀

@flip1995
Copy link
Member

Impl looks really good!

As @llogiq suggested:

Also please prepend a //run-rustfix line to the UI test

Since the tests for this lint are in the pile of tests in methods.rs you first have to extract them in a new file. (This would also help with #2038) Just move these tests:

/// Checks implementation of the following lints:
/// * `OPTION_MAP_UNWRAP_OR`
/// * `OPTION_MAP_UNWRAP_OR_ELSE`
#[rustfmt::skip]
fn option_methods() {
let opt = Some(1);
// Check `OPTION_MAP_UNWRAP_OR`.
// Single line case.
let _ = opt.map(|x| x + 1)
// Should lint even though this call is on a separate line.
.unwrap_or(0);
// Multi-line cases.
let _ = opt.map(|x| {
x + 1
}
).unwrap_or(0);
let _ = opt.map(|x| x + 1)
.unwrap_or({
0
});
// Single line `map(f).unwrap_or(None)` case.
let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
// Multi-line `map(f).unwrap_or(None)` cases.
let _ = opt.map(|x| {
Some(x + 1)
}
).unwrap_or(None);
let _ = opt
.map(|x| Some(x + 1))
.unwrap_or(None);
// macro case
let _ = opt_map!(opt, |x| x + 1).unwrap_or(0); // should not lint
// Should not lint if not copyable
let id: String = "identifier".to_string();
let _ = Some("prefix").map(|p| format!("{}.{}", p, id)).unwrap_or(id);
// ...but DO lint if the `unwrap_or` argument is not used in the `map`
let id: String = "identifier".to_string();
let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id);

in a new test file named tests/ui/option_map_unwrap_or.rs. Then add // run-rustfix to the top of the file and go through the test update process again.

@bors
Copy link
Collaborator

bors commented Oct 18, 2019

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

@llogiq
Copy link
Contributor

llogiq commented Oct 18, 2019

@m-ober is there anything you need besides time to get this PR to a mergeable state? If we can somehow help, feel free to ask.

@m-ober
Copy link
Contributor Author

m-ober commented Oct 19, 2019

I tried to finish the PR according to @flip1995's instructions but it seems there is a problem:

error: called `map(f).unwrap_or(None)` on an Option value. This can be done more directly by calling `and_then(f)` instead
  --> $DIR/option_map_unwrap_or.rs:33:13
   |
LL |     let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: use `map_or` instead
   |
LL |     let _ = opt.map_or(None, |x| Some(x + 1));
   |                 ^^^^^^ ^^^^^                --

The former suggestion was to use and_then here, but my implementation just replaces everything with map_or 😦 I guess I should extend the lint to correctly apply and_then for these cases?

Looking at option_map_unwrap_or.fixed it seems that nothing at all is changed. Why is that? Did I forget something?

@llogiq
Copy link
Contributor

llogiq commented Oct 19, 2019

No idea, perhaps the applicability is not set correctly?

@flip1995
Copy link
Member

Looking at option_map_unwrap_or.fixed it seems that nothing at all is changed. Why is that? Did I forget something?

Oh multipart suggestions can't be applied currently. See rust-lang/rustfix#162, rust-lang/rust#53934

Just remove the // run-rustfix comment and add a FIXME comment to add run-rustfix once this is supported by rustfix.

@m-ober m-ober changed the title WIP: Add real suggestion to option_map_unwrap_or Add real suggestion to option_map_unwrap_or Oct 22, 2019
@m-ober
Copy link
Contributor Author

m-ober commented Oct 22, 2019

I have removed the "WIP" tag as I think the PR should be in good shape now. The failing pipeline should not be related to my changes.

Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

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

LGTM now. ping @llogiq

@flip1995
Copy link
Member

ping from triage @m-ober. It seems the only open comment is #4634 (comment). Do you need some help with that?

@m-ober
Copy link
Contributor Author

m-ober commented Dec 6, 2019

@flip1995 Sorry, I hope I will find some time on the weekend to finally finish this pr 😃

@bors
Copy link
Collaborator

bors commented Dec 27, 2019

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

@m-ober m-ober force-pushed the feature/2320-map-or branch 2 times, most recently from 63b54ed to 24a3c64 Compare December 28, 2019 22:23
@m-ober
Copy link
Contributor Author

m-ober commented Dec 28, 2019

I have implemented the change suggested by @llogiq - I hope everything is fine now :)

@m-ober m-ober requested a review from llogiq December 28, 2019 22:42
@llogiq
Copy link
Contributor

llogiq commented Dec 29, 2019

I'll look into that later today or tomorrow. Thank you!

@llogiq
Copy link
Contributor

llogiq commented Dec 30, 2019

LGTM, but we're blocked on #4972.

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.

Ok, this is ready to merge now.

@llogiq
Copy link
Contributor

llogiq commented Dec 30, 2019

@bors: r+

@bors
Copy link
Collaborator

bors commented Dec 30, 2019

📌 Commit c5046fd has been approved by llogiq

@bors
Copy link
Collaborator

bors commented Dec 30, 2019

⌛ Testing commit c5046fd with merge 0ddccc4...

bors added a commit that referenced this pull request Dec 30, 2019
Add real suggestion to option_map_unwrap_or

Fixes #2320
@bors
Copy link
Collaborator

bors commented Dec 30, 2019

💔 Test failed - checks-travis

@m-ober
Copy link
Contributor Author

m-ober commented Dec 30, 2019

Oops, added the "changelog:" line.

@llogiq
Copy link
Contributor

llogiq commented Dec 30, 2019

No biggie! @bors retry

@bors
Copy link
Collaborator

bors commented Dec 30, 2019

⌛ Testing commit c5046fd with merge cecaca3...

bors added a commit that referenced this pull request Dec 30, 2019
Add real suggestion to option_map_unwrap_or

changelog: Add real suggestion to `option_map_unwrap_or`

Fixes #2320
@bors
Copy link
Collaborator

bors commented Dec 30, 2019

☀️ Test successful - checks-travis, status-appveyor
Approved by: llogiq
Pushing cecaca3 to master...

@bors bors merged commit c5046fd into rust-lang:master Dec 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add real suggestion to option_map_unwrap_or
4 participants