Skip to content

feat(manual_is_variant): Check for .iter().any() - #15989

Closed
qmwrygdxpzc wants to merge 3 commits into
rust-lang:masterfrom
ceptontech:manual-is-variant
Closed

feat(manual_is_variant): Check for .iter().any()#15989
qmwrygdxpzc wants to merge 3 commits into
rust-lang:masterfrom
ceptontech:manual-is-variant

Conversation

@qmwrygdxpzc

@qmwrygdxpzc qmwrygdxpzc commented Oct 30, 2025

Copy link
Copy Markdown

View all comments

Now the linter checks for calling the iter or into_iter method
followed by calling the all or any method.

We have used these methods by mistake ourselves. The developer was not
familiar with the is_some_and method. He tried to use the any
method on an option. The compiler suggested using iter first. The
developer did not get to learn about the idiomatic method.

The lint search_is_some involves converting a closure that takes the
reference into one that takes the value. The code is split between the
function for the lint and a shared function. The new feature that we
are adding involves the same transformation. To do so it needs some of
the same code that is currently in the search_is_some lint. So it is
moved from the lint into the shared function.

Before this patch, line 799 in the file clippy_utils/src/sugg.rs had a
check. It made the function return none in case the closure did not
dereference the argument. However, the code for the lint
search_is_some would then fall back to using the original closure.
That achieved the same effect as the check not existing. So the check
is now removed.

changelog: [manual_is_variant]: check for calling the iter or
into_iter method followed by calling the all or any method

changelog: [manual_is_variant]: skip the check when the MSRV is before
1.70

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Oct 30, 2025
@rustbot

rustbot commented Oct 30, 2025

Copy link
Copy Markdown
Collaborator

r? @Alexendoo

rustbot has assigned @Alexendoo.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@qmwrygdxpzc

Copy link
Copy Markdown
Author

If the input uses .iter().any() on a value that is not Copy, the current version of the patch makes the linter suggest using .as_ref().is_some_and(). The suggestion is longer than the original code. Should the linter skip it instead?

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

Comment thread clippy_utils/src/sugg.rs
Comment on lines +780 to +792
// suggest `any(|x| ..)` instead of `any(|&x| ..)` for `find(|&x| ..).is_some()`
if matches!(closure_arg.pat.kind, PatKind::Ref(..)) {
let mut applicability = Applicability::MachineApplicable;
let suggestion =
snippet_with_applicability(cx, closure.span, "..", &mut applicability).replacen('&', "", 1);
return Some(DerefClosure {
applicability,
suggestion,
});
}
if !matches!(strip_pat_refs(closure_arg.pat).kind, PatKind::Binding(..)) {
return None;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I believe this should still live outside of the clippy_utils method, though it's not currently used elsewhere it's specified as being generally applicable

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

With this patch, both search_is_some and manual_is_variant_and use it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah for those it might be fine, but may not be applicable to other future uses

cc @Jarcho on this though since I'm not 100% sure on what deref_closure_args is doing

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The deref_closure_args function changes a closure that closure that takes the reference into one that takes the value. A closure can take a reference in these forms: |x| *x, or |&x| x. Without my patch, only the search_is_some lint does so to the closure. The first form is handled in the deref_closure_args function. The second form is handled in the file for the lint. It makes sense for other lints, such as the manual_is_variant that I am modifying, to modify both forms if it needs to make the closure take the value. So I moved the logic for handling both forms into the library function.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@qmwrygdxpzc

Copy link
Copy Markdown
Author

@profetia Can you please take a look? You worked on the same check recently.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

ceptontech added 3 commits March 6, 2026 09:11
The lint `search_is_some` involves converting a closure that takes the
reference into one that takes the value.  The code is split between the
function for the lint and a shared function.  The new feature that we
are adding involves the same transformation.  To do so it needs some of
the same code that is currently in the `search_is_some` lint.  So it is
moved from the lint into the shared function.

Before this patch, line 799 in the file `clippy_utils/src/sugg.rs` had a
check.  It made the function return none in case the closure did not
dereference the argument.  However, the code for the lint
`search_is_some` would then fall back to using the original closure.
That achieved the same effect as the check not existing.  So the check
is now removed.
Now the linter checks for calling the `iter` or `into_iter` method
followed by calling the `all` or `any` method.

We have used these methods by mistake ourselves.  The developer was not
familiar with the `is_some_and` method.  He tried to use the `any`
method on an option.  The compiler suggested using `iter` first.  The
developer did not get to learn about the idiomatic method.

changelog: [`manual_is_variant`]: check for calling the `iter` or
`into_iter` method followed by calling the `all` or `any` method
changelog: [`manual_is_variant`]: skip the check when the MSRV is before
1.70
@rustbot

rustbot commented Mar 6, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@qmwrygdxpzc

Copy link
Copy Markdown
Author

No reviewer has said things for a long time. Does that mean we had better give up on the pull request?

@qmwrygdxpzc

Copy link
Copy Markdown
Author

@Alexendoo

No reviewer has said things for a long time. Does that mean we had better give up on the pull request?

@profetia

Copy link
Copy Markdown
Member

You can use r? clippy to find a new reviewer

@qmwrygdxpzc

Copy link
Copy Markdown
Author

r? clippy

@rustbot rustbot assigned ada4a and unassigned Alexendoo Mar 27, 2026
@qmwrygdxpzc

Copy link
Copy Markdown
Author

@ada4a This patch has taken a long time. Is the reviewer busy or is there a problem with the patch?

@profetia

Copy link
Copy Markdown
Member

You might use r? clippy to find a new reviewer

@qmwrygdxpzc

Copy link
Copy Markdown
Author

It takes too long to get this patch reviewed.

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label May 15, 2026
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.

5 participants