Skip to content

Suggest use .get_mut instead of &mut when overloaded index type not impl IndexMut #144018

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

xizheyin
Copy link
Contributor

@xizheyin xizheyin commented Jul 16, 2025

Fixes #143732

r? compiler

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 16, 2025
@fee1-dead
Copy link
Member

You need to compare the defid with the defid of the HashMap/BTreeMap diagnostic item and hard code the special case

@xizheyin xizheyin changed the title Suggest use .get_mut instead of &mut when occur overloaded index expression Suggest use .get_mut instead of &mut when occur HashMap and BtreeMap Jul 16, 2025
Copy link
Contributor Author

@xizheyin xizheyin left a comment

Choose a reason for hiding this comment

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

The second commit shows the testcase difference.

@fee1-dead
Copy link
Member

fee1-dead commented Jul 17, 2025

In your first version you checked if the assignment on the type came from an overloaded index. You should check whether the overloaded index type implements IndexMut and return instead of passing the bool into the suggest_ampmut function if they don't implement IndexMut, then before returning you can suggest get_mut

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 17, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 17, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@xizheyin xizheyin force-pushed the 143732 branch 2 times, most recently from 8d3b265 to c43a91f Compare July 20, 2025 10:07
Copy link
Contributor Author

@xizheyin xizheyin left a comment

Choose a reason for hiding this comment

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

@rustbot ready

Comment on lines +16 to +18
let mut vec = vec![String::new(), String::new()];
let string = &vec[0];
string.push_str("test"); //~ ERROR cannot borrow `*string` as mutable, as it is behind a `&` reference [E0596]
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 add a case that type impls IndexMut, i.e. Vec.

Comment on lines +1449 to +1460
.type_implements_trait(
index_mut_trait,
[ty, index_ty],
self.infcx.param_env,
)
.must_apply_modulo_regions()
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 use type_implements_trait to judge if the type impl IndexMut.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 20, 2025
@rust-log-analyzer

This comment has been minimized.

@xizheyin xizheyin changed the title Suggest use .get_mut instead of &mut when occur HashMap and BtreeMap Suggest use .get_mut instead of &mut when overloaded index type not impl IndexMut Jul 20, 2025
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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

rust suggests using IndexMut for maps, even though they don't implement the trait
4 participants