manual_pop_if: avoid invalid automatic suggestions - #17561
Conversation
|
Thanks for the pull request. A reviewer will take a look after it receives 2 community reviews. In the meantime, we would highly appreciate if you could try to review any of PRs waiting on community reviews. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
dd2bc7a to
92276c9
Compare
This comment has been minimized.
This comment has been minimized.
Signed-off-by: cuishuang <imcusg@gmail.com>
92276c9 to
9731f93
Compare
|
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. |
|
@CommanderStorm Thanks for the detailed feedback. I addressed the comments as follows:
I also rebased onto the latest upstream master and resolved the merge conflict. Please review it again. |
| LL | if vec.last().is_some_and(|x| vec.len() > 1 && *x > 10) { | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | | ||
| | the predicate borrows the collection here |
There was a problem hiding this comment.
is the span here entierly correct? It also includes the if...
There was a problem hiding this comment.
Yes, the secondary span is intended to cover only the vec path in vec.len(). The surrounding ^ characters belong to the primary lint span, which covers the whole if condition. The --- section is the secondary label for the collection borrow, so it does not include the if.
I agree that the overlapping spans make the rendered output a little confusing.
| LL | if vec.len() > 1 && *x > 10 { | ||
| | ^^^---^^^^^^^^^^^^^^^^^^^^^ | ||
| | | | ||
| | the predicate borrows the collection here |
There was a problem hiding this comment.
the span here also seems a bit suspect.. You sure that this is correct?
There was a problem hiding this comment.
The same applies here. The secondary span is taken from the HIR span of the vec path used in the predicate. The ^^^ before it belongs to the existing primary span for the inner condition, while the --- marks only the collection use.
So the labeled span is limited to vec, not the surrounding if. I can adjust the diagnostic presentation if the overlapping spans are considered too confusing.
changelog: [
manual_pop_if]: avoid invalid automatic suggestions when the predicate borrows the collectionmanual_pop_ifcould emit a machine-applicable suggestion when the predicate also referenced the collection being popped from.For example, rewriting:
to
vec.pop_if(...)requires a mutable borrow ofvecwhile 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
refandmut, since the predicate parameter changes from&Tto&mut T.Add UI coverage for all supported condition forms, complex collection expressions, and non-default parameter bindings.