-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Suggest removing &mut
from a &mut borrow
#77110
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
Conversation
3f4fa7f
to
ff4c2e1
Compare
} | ||
}) | ||
.unwrap_or(false) | ||
if match self.body.local_decls.get(local) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we refactor this into another function?
rust/compiler/rustc_middle/src/mir/mod.rs
Line 937 in 81e0270
pub fn can_be_made_mutable(&self) -> bool { |
as
if self.body.local_decls
.get(local)
.map_or(false, |local_decl| local_decl.mut_borrow_of_mutable_ref(self.local_names[local])) => {
But I wonder if it will even failed to get local
? The next block just use local_decls[local]
but not sure if it will work here.
if self.body.local_decls[local]
.mut_borrow_of_mutable_ref(self.local_names[local]) => {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get it. Did you mean to make a method on LocalDecls?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think there are code elsewhere that need to reuse the logic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's true but I don't know if putting it as another function on itself would be good since it may easily be lost on context. Maybe refactor it as an implementation under MirBorrowckCtxt
which is the same thing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly I prefer free function as it since it don't need other states than passed arguments.
But I don't have strong opinion, let's let it for Esteban or David.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if it is worth extracting this into a method, but I would prefer something like this than a match
here:
self.body.local_decls
.get(local)
.map(|l| mut_borrow_of_mutable_ref(l, self.local_names[local])
.unwrap_or(false)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Just wondering, could there be multiple layers for this? Like multiple |
estebank's backlog is really long, so let's find another reviewer: r? @davidtwco |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, minor change requested, sorry for the delay in reviewing this.
} | ||
}) | ||
.unwrap_or(false) | ||
if match self.body.local_decls.get(local) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if it is worth extracting this into a method, but I would prefer something like this than a match
here:
self.body.local_decls
.get(local)
.map(|l| mut_borrow_of_mutable_ref(l, self.local_names[local])
.unwrap_or(false)
Sorry, I had to rebase because my local copy doesn't build with old worktree. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, r=me once CI passes; I'd like it if you could squash though.
Fix a typo: minding -> binding Add test for &mut &mut
@bors r=davidtwco |
📌 Commit ab226bd has been approved by |
.local_decls | ||
.get(local) | ||
.map(|l| mut_borrow_of_mutable_ref(l, self.local_names[local])) | ||
.unwrap_or(false) => | ||
{ | ||
err.span_label(span, format!("cannot {ACT}", ACT = act)); | ||
err.span_label(span, "try removing `&mut` here"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be nice to come back to this at some other time to gather a good span to make this a structured suggestion :)
☀️ Test successful - checks-actions, checks-azure |
Modify the code added in #54720.
Closes #75871