Skip to content

Fix manual_pop_if captured collection suggestions - #17793

Open
zjubiology wants to merge 1 commit into
rust-lang:masterfrom
zjubiology:my_feature
Open

zjubiology wants to merge 1 commit into
rust-lang:masterfrom
zjubiology:my_feature

Conversation

@zjubiology

@zjubiology zjubiology commented Sep 26, 2026 •

Copy link
Copy Markdown

changelog: [manual_pop_if]: avoid invalid automatic suggestions when the predicate borrows the collection

manual_pop_if could emit a machine-applicable suggestion when the predicate also referenced the collection being popped from.

For example, rewriting:

if vec
    .last()
    .is_some_and(|x| vec.len() > 1 && *x > 10)
{
    vec.pop().unwrap();
}

to vec.pop_if(...) requires a mutable borrow of vec while the predicate closure also immutably borrows it, causing E0502.

Only provide an automatic suggestion when the collection is a simple local path and the predicate does not reference that local. Continue emitting the lint with a help message for captured or non-local collection expressions.

Also retain the parameter pattern and avoid automatic rewrites for non-default binding modes such as ref and mut, since the predicate parameter changes from &T to &mut T.

Add UI coverage for all supported condition forms, complex collection expressions, and non-default parameter bindings.

@rustbot rustbot added the S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. label Sep 26, 2026
@rustbot

rustbot commented Sep 26, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome!

You should hear from one of our reviewers after this PR gets at least 2 reviews from the community.

Please see the contribution instructions for more information.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Sep 26, 2026
Comment thread clippy_lints/src/manual_pop_if.rs Outdated
Comment thread clippy_lints/src/manual_pop_if.rs Outdated

@notriddle notriddle left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I’m not going to pretend to understand all of the code in here, but the test cases are improved, and the general idea of scanning for usages makes sense.

View changes since this review

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. 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.

3 participants