Skip to content

Add a suggestion to replace map(f).unwrap_or(None) with and_then(f). #2112

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

Merged
merged 3 commits into from
Oct 8, 2017

Conversation

topecongiro
Copy link
Contributor

Closes #2109.

Add a suggestion to replace `map(f).unwrap_or(None)` with `and_then(f)`.
Copy link
Contributor

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

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

I'd like a comment explaining the "None" comparison, so we know why we decided it's fine.

Can you also add a few more tests (multiline closure)?

// get snippets for args to map() and unwrap_or()
let map_snippet = snippet(cx, map_args[1].span, "..");
let unwrap_snippet = snippet(cx, unwrap_args[1].span, "..");
// lint message
let arg = if unwrap_snippet == "None" { "None" } else { "a" };
Copy link
Contributor

Choose a reason for hiding this comment

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

My first thought was "oh no no, don't compare snippets to raw text". But in this one case almost nothing can go wrong, since we already checked the type. Well, the only thing that can go wrong is someone creating const None: Option<Foo> = Some(Foo);. I think there's a special place in hell for anyone doing that. 😈

I'm so torn between "do it right" and "doing it right is less readable"!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey sorry for horrifying you - I had no hesitant on comparing snippets to raw text 😛

Copy link
Contributor Author

Choose a reason for hiding this comment

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

While adding more tests, I found that comparing snippets to raw text does not work in some trivial cases. I copied them from tests for OPTION_MAP_UNWRAP_OR. e.g.

    let _ = opt.map(|x| Some(x + 1))
        .unwrap_or({
            None
        });

Is this acceptable, or should we "do it right"?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yea that's fine

@topecongiro
Copy link
Contributor Author

Added a comment and tests.

@oli-obk oli-obk merged commit a54baad into rust-lang:master Oct 8, 2017
@oli-obk
Copy link
Contributor

oli-obk commented Oct 8, 2017

Yay! So... now that this one is through, I noticed the lint (the original one already had this issue) does not have a structured suggestion (span_suggestion). If you are looking for something to do, this would be something fairly easy, plus you already know the code here. Adding structured suggestions will allow RLS to offer to fix the issue with a click instead of having to do it manually

@topecongiro topecongiro deleted the issue-2109 branch October 8, 2017 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants