Skip to content

New lint: option_manual_map #6573

New issue

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

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

Already on GitHub? # to your account

Merged
merged 2 commits into from
Feb 23, 2021
Merged

New lint: option_manual_map #6573

merged 2 commits into from
Feb 23, 2021

Conversation

Jarcho
Copy link
Contributor

@Jarcho Jarcho commented Jan 10, 2021

fixes: #6
changelog: Added lint: match_map

@rust-highfive
Copy link

r? @ebroto

(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 Jan 10, 2021
@Jarcho Jarcho force-pushed the option_match_map branch 2 times, most recently from f727193 to 5bbfdad Compare January 10, 2021 21:39
@bors
Copy link
Contributor

bors commented Jan 15, 2021

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

@ebroto
Copy link
Member

ebroto commented Jan 16, 2021

r? @llogiq (I'm leaving the team, so I'm reassigning my PRs to other active members)

@rust-highfive rust-highfive assigned llogiq and unassigned ebroto Jan 16, 2021
@bors
Copy link
Contributor

bors commented Jan 17, 2021

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

@llogiq
Copy link
Contributor

llogiq commented Jan 17, 2021

r=me after a rebase.

@camsteffen
Copy link
Contributor

The lint could also work on Result. That could be deferred for a later enhancement, but either way, I'd suggest renaming the lint to manual_map.

@llogiq
Copy link
Contributor

llogiq commented Jan 22, 2021

I think you should be able to rebase ignoring clippy_lints/src/lib.rs, then running cargo dev update_lints, which should regenerate the lint list. That will likely get rid of the conflicts.

@llogiq
Copy link
Contributor

llogiq commented Feb 7, 2021

@Jarcho do you intend to resume work on this? Can we help you with something?

@Jarcho Jarcho force-pushed the option_match_map branch 2 times, most recently from 69a2186 to 1c0ba16 Compare February 19, 2021 17:52
@Jarcho
Copy link
Contributor Author

Jarcho commented Feb 19, 2021

Basically ended up rewriting this.

  • The lint name has been changed, but still only lints for Option.
  • Some can now contain any pattern so long as there are no binding annotations, and the other pattern is None.
  • as_ref and as_mut will be added when needed. Matching on &Option will now work.
  • No longer checks for identity

@Jarcho Jarcho force-pushed the option_match_map branch 3 times, most recently from 188df9f to 3f997c2 Compare February 19, 2021 19:58
@Jarcho
Copy link
Contributor Author

Jarcho commented Feb 19, 2021

I'm not sure why that's failing. That lint should be allowed there.

@llogiq
Copy link
Contributor

llogiq commented Feb 20, 2021

The reason appears to be that the test is declared to work with rustfix, but the fixed code fails to compile.

@bors
Copy link
Contributor

bors commented Feb 20, 2021

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

@llogiq
Copy link
Contributor

llogiq commented Feb 20, 2021

Having looked into it a bit more from my PC, I can see that line 42 of the fixed test falls afoul of clippy::option_map_unit_fn. Now we can either a) silence that lint in the test or b) add a check of the map closure's return type and skip the lint if it's unit.

@Jarcho
Copy link
Contributor Author

Jarcho commented Feb 20, 2021

There's an #[allow(clippy::option_map_unit_fn)] right before that line.

@giraffate
Copy link
Contributor

I think an #[allow(clippy::option_map_unit_fn)] before expression doesn't work. If it is before statements, it works.

@Jarcho
Copy link
Contributor Author

Jarcho commented Feb 22, 2021

It is on a statement. From tests/ui/manual_map_option.fixed (line 41 & 42):

    #[allow(clippy::option_map_unit_fn)]
    Some(String::new()).as_mut().map(|x| x.push_str(""));

Is that not supposed to work?

There is also a check to make sure option_map_unit_fn is allowed before making the suggestion, so it was fine when it was:

    #[allow(clippy::option_map_unit_fn)]
    match &mut Some(String::new()) {
        Some(x) => Some(x.push_str("")),
        None => None,
    };

@giraffate
Copy link
Contributor

Is that not supposed to work?

Yes, I think so. I think it works before statements, not a statement. I don't know the detail, but it seems to work like this when that lint (option_map_unit_fn in this case ) is called in check_stmt.

@Jarcho Jarcho force-pushed the option_match_map branch 2 times, most recently from dcf50b8 to cad28ec Compare February 22, 2021 03:37
@Jarcho
Copy link
Contributor Author

Jarcho commented Feb 22, 2021

Looks like wrapping it in a block worked.

@llogiq
Copy link
Contributor

llogiq commented Feb 23, 2021

Great! Thank you for pushing this to completion! @bors r+

@bors
Copy link
Contributor

bors commented Feb 23, 2021

📌 Commit 23aa2f8 has been approved by llogiq

@bors
Copy link
Contributor

bors commented Feb 23, 2021

⌛ Testing commit 23aa2f8 with merge a2c25fa...

@bors
Copy link
Contributor

bors commented Feb 23, 2021

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: llogiq
Pushing a2c25fa to master...

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use Option.map() instead of match { Some(x) => ..., None => None }
7 participants