Skip to content

New lints iter_filter_is_some and iter_filter_is_ok #12004

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 4 commits into from
Dec 26, 2023
Merged

New lints iter_filter_is_some and iter_filter_is_ok #12004

merged 4 commits into from
Dec 26, 2023

Conversation

PartiallyUntyped
Copy link
Contributor

@PartiallyUntyped PartiallyUntyped commented Dec 24, 2023

Adds a pair of lints that check for cases of an iterator over Result and Option followed by filter without being followed by map as that is covered already by a different, specialized lint.

Fixes #11843

PS, I also made some minor documentations fixes in a case where a double tick (`) was included.


changelog: New Lint: [iter_filter_is_some]
#12004
changelog: New Lint: [iter_filter_is_ok]
#12004

Adds a pair of lints that check for cases of an iterator over `Result`
and `Option` followed by `filter` without being followed by `map` as
that is covered already by a different, specialized lint.

changelog: New Lint: [`iter_filter_is_some`]
changelog: New Lint: [`iter_filter_is_ok`]
@rustbot
Copy link
Collaborator

rustbot commented Dec 24, 2023

r? @xFrednet

(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 Dec 24, 2023
@PartiallyUntyped
Copy link
Contributor Author

PS, sorry for the double PR, I was doing Christmas cleaning and deleted my fork. Didn't know GH wouldn't allow me to reopen the previous one 😓

Copy link
Member

@xFrednet xFrednet left a comment

Choose a reason for hiding this comment

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

Generally looks good to me, a few NITs and then it should be good to go. (I hope my review is not too nit-picky.)

@xFrednet
Copy link
Member

Looks good to me, thank you for the new lints :D

@bors r+

@bors
Copy link
Contributor

bors commented Dec 26, 2023

📌 Commit 85e1646 has been approved by xFrednet

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Dec 26, 2023

⌛ Testing commit 85e1646 with merge cf7e34d...

bors added a commit that referenced this pull request Dec 26, 2023
New lints `iter_filter_is_some` and `iter_filter_is_ok`

Adds a pair of lints that check for cases of an iterator over `Result` and `Option` followed by `filter` without being followed by `map` as that is covered already by a different, specialized lint.

Fixes #11843

PS, I also made some minor documentations fixes in a case where a double tick (`) was included.

---

* changelog: New Lint: [`iter_filter_is_some`]
[#12004](rust-lang/rust#12004)
* changelog: New Lint: [`iter_filter_is_ok`]
[#12004](rust-lang/rust#12004)
@bors
Copy link
Contributor

bors commented Dec 26, 2023

💔 Test failed - checks-action_test

@xFrednet
Copy link
Member

Woops, looks like I invalidated the changelog entries. Let's try it again:

@bors retry

@bors
Copy link
Contributor

bors commented Dec 26, 2023

⌛ Testing commit 85e1646 with merge 9dd2252...

@bors
Copy link
Contributor

bors commented Dec 26, 2023

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: xFrednet
Pushing 9dd2252 to master...

@bors bors merged commit 9dd2252 into rust-lang:master Dec 26, 2023
@PartiallyUntyped
Copy link
Contributor Author

Woops, seems that I didn't squash and force push 😬😔

@PartiallyUntyped PartiallyUntyped deleted the 11843 branch December 26, 2023 16:13
bors added a commit that referenced this pull request Jan 7, 2024
Fixed ICE introduced in #12004

Issue: in #12004, we emit a lint for `filter(Option::is_some)`. If the
parent expression is a `.map` we don't emit that lint as there exists a
more specialized lint for that.

The ICE introduced in #12004 is a consequence of the assumption that a
parent expression after a filter would be a method call with the filter
call being the receiver. However, it is entirely possible to have a
closure of the form

```
|| { vec![Some(1), None].into_iter().filter(Option::is_some) }
```
The previous implementation looked at the parent expression; namely the
closure, and tried to check the parameters by indexing [0] on an empty
list.

This commit is an overhaul of the lint with significantly more FP tests
and checks.

Impl details:

1. We verify that the filter method we are in is a proper trait method
   to avoid FPs.
2. We check that the parent expression is not a map by checking whether
   it exists; if is a trait method; and then a method call.
3. We check that we don't have comments in the span.
4. We verify that we are in an Iterator of Option and Result.
5. We check the contents of the filter.
   1. For closures we peel it. If it is not a single expression, we don't
     lint. We then try again by checking the peeled expression.
   2. For paths, we do a typecheck to avoid FPs for types that impl
     functions with the same names.
   3. For calls, we verify the type, via the path, and that the param of
     the closure is the single argument to the call.
   4. For method calls we verify that the receiver is the parameter of
     the closure. Since we handle single, non-block exprs, the
     parameter can't be shadowed, so no FP.

This commit also adds additional FP tests.

Fixes: #12058

Adding `@xFrednet` as you've the most context for this as you reviewed it last time.

`@rustbot` r? `@xFrednet`

---

changelog: none
(Will be backported and therefore don't effect stable)
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.

Suggest flatten over filter(Option::is_some)
4 participants