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

[manual_filter_map]: lint on matches and pattern matching #10949

Merged
merged 1 commit into from Jul 19, 2023

Conversation

y21
Copy link
Member

@y21 y21 commented Jun 14, 2023

Fixes #8010

Previously this lint only worked specifically for a very limited set of methods on the filter call (.filter(|opt| opt.is_some()) and .filter(|res| res.is_ok())). This PR extends it to also recognize matches! in the filter and pattern matching with if let or match in the map.

Example:

enum Enum {
  A(i32),
  B,
}

let _ = [Enum::A(123), Enum::B].into_iter()
  .filter(|x| matches!(x, Enum::A(_)))
  .map(|x| if let Enum::A(s) = x { s } else { unreachable!() });

Now suggests:

-  .filter(|x| matches!(x, Enum::A(_))).map(if let Enum::A(s) = x { s } else { unreachable!() })
+  .filter_map(|x| match x { Enum::A(s) => Some(s), _ => None })

Adding this required a somewhat large change in code because it originally seemed to be specifically written with only method calls in the filter in mind, and matches! has different behavior in the map, so this new setup should make it possible to support more "generic" cases that need different handling for the filter and map calls.

changelog: [manual_filter_map]: lint on matches and pattern matching (and some internal refactoring)

@rustbot
Copy link
Collaborator

rustbot commented Jun 14, 2023

r? @Alexendoo

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 14, 2023
clippy_lints/src/methods/filter_map.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/filter_map.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/filter_map.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/filter_map.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/filter_map.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/filter_map.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/filter_map.rs Outdated Show resolved Hide resolved
Copy link
Member

@Alexendoo Alexendoo left a comment

Choose a reason for hiding this comment

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

Whoops, forgot to submit these 👀

image

clippy_lints/src/methods/filter_map.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/filter_map.rs Outdated Show resolved Hide resolved
@y21
Copy link
Member Author

y21 commented Jun 30, 2023

oops, forgot to bless the tests

@Alexendoo
Copy link
Member

Thanks, looks good!

@bors r+

@bors
Copy link
Collaborator

bors commented Jul 19, 2023

📌 Commit 648d1ae has been approved by Alexendoo

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Jul 19, 2023

⌛ Testing commit 648d1ae with merge 0b63e95...

@bors
Copy link
Collaborator

bors commented Jul 19, 2023

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: Alexendoo
Pushing 0b63e95 to master...

@bors bors merged commit 0b63e95 into rust-lang:master Jul 19, 2023
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in 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.

Missed manual_filter_map with matches!
4 participants