Skip to content

WIP: map_identity: suggest making the variable mutable when necessary #15268

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ada4a
Copy link
Contributor

@ada4a ada4a commented Jul 13, 2025

While working on #15229, I noticed a test case in map_identity which avoids linting in the case where removing the .map() would require the iterator variable to be made mutable. But then I saw #14140, and thought I'd try to adapt its approach, by suggesting both removing the .map() and making the variable mutable.

This is WIP only because I'm not sure about the very last diag.span_note -- I think having a method chained immediately after the .map() is the only case which requires adding mut, so it should be safe to say that method_requiring_mut is always Some. I'd like to do a lintcheck run to see if that's actually true.

@samueltardieu do you think this is a good approach? Maybe there are more lints where we could lint + suggest making the variable mut, instead of not linting?

changelog:map_identity: suggest making the variable mutable when necessary

@rustbot
Copy link
Collaborator

rustbot commented Jul 13, 2025

r? @y21

rustbot has assigned @y21.
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

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jul 13, 2025
@ada4a ada4a force-pushed the map-identity-sugg-on-mut branch from cb16cf2 to 4ae3029 Compare July 13, 2025 18:22
@ada4a ada4a marked this pull request as draft July 13, 2025 20:53
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jul 13, 2025
@ada4a ada4a changed the title WIP: map_identity: suggest making the variable mutable when necessary map_identity: suggest making the variable mutable when necessary Jul 13, 2025
@ada4a ada4a changed the title map_identity: suggest making the variable mutable when necessary WIP: map_identity: suggest making the variable mutable when necessary Jul 13, 2025
@ada4a ada4a marked this pull request as ready for review July 13, 2025 20:54
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jul 13, 2025
@ada4a
Copy link
Contributor Author

ada4a commented Jul 29, 2025

@y21 friendly ping

@ada4a ada4a force-pushed the map-identity-sugg-on-mut branch from 4ae3029 to 806ce8d Compare August 2, 2025 11:57
Copy link
Member

@y21 y21 left a comment

Choose a reason for hiding this comment

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

Looks good to me, I have one suggestion that could possibly make one more case autofixable

I think having a method chained immediately after the .map() is the only case which requires adding mut, so it should be safe to say that method_requiring_mut is always Some. I'd like to do a lintcheck run to see if that's actually true.

&mut iter.map(...) and let ref mut x = iter.map(..) are some other cases that would need it, though IMO not really worth exhaustively handling all other arbitrary ways since this is just for a specialized note. So that note seems fine to me.

let mut sugg = vec![(call_span, String::new())];
let mut apply = true;
if !is_mutable(cx, caller) {
if let Some(hir_id) = path_to_local(caller)
Copy link
Member

@y21 y21 Aug 2, 2025

Choose a reason for hiding this comment

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

I wonder if it'd be worth moving this function to clippy_utils (as path_to_local_with_projections), and using that one instead of path_to_local. That would make the test case in _unfixable fixable right?

The lint where this is from is also doing the same kind of thing as this PR is, so it seems like this is not too uncommon of an issue that it could justify adding a new util function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds like a good idea!

In fact, I was already able to find one more place that could make use of this function:

/// Checks if an expression is a path to a local variable (with optional projections), e.g.
/// `x.field[0].field2` would return true.
fn is_local_with_projections(expr: &Expr<'_>) -> bool {
match expr.kind {
ExprKind::Path(_) => path_to_local(expr).is_some(),
ExprKind::Field(expr, _) | ExprKind::Index(expr, ..) => is_local_with_projections(expr),
_ => false,
}
}

could be just path_to_local_with_projections(expr).is_some().

So maybe it would make sense to open a separate PR for creating this function and finding all the places it could be used in?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I went ahead and opened a PR at #15396, let me know what you think of it

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.

3 participants