-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Initial implementation of mut_refcell_borrow
#9423
Conversation
r? @Jarcho (rust-highfive has picked a reviewer for you, use r? to override) |
779e3f1
to
f25f387
Compare
Pulled to draft, because this is very unsatisfactory as it is. For instance, we should lint if the |
There are essentially three cases for this lint, as far as I can see:
I can't see how one would prove if an |
|
You're point isn't true. |
You mean the first point? In that case, each |
My point was you can do something like: fn f(a: &mut RefCell<u32>) -> u32 {
let b = a.borrow();
let c = a.borrow();
let d = &* a;
let e = d.borrow();
*b + *c + *e
} You can't replace |
☔ The latest upstream changes (presumably #9541) made this pull request unmergeable. Please resolve the merge conflicts. |
I just realize we have a needless @Jarcho hm, good point. However one could write fn f(a: &mut RefCell<u32>) -> u32 {
let a = a.get_mut();
let b = &*a;
let c = &*a;
let e = &*a;
*b + *c + *e
} So, it is a slightly larger refactor, but still an improvement overall. Though there are probably odd corner cases where even this is not possible. But clippy's lint policy is anyway to cover the 95%, not the 100%, isn't it? |
Closing this for inactivity (296 days), @lukaslueg if you're still interested in working on this lint, feel free to re-open it! ❤️ |
Fixes #9044
This is an implementation of #9044: If we have an exclusive reference to a
RefCell
, the methods that perform an implicit (panic
) or explicit (Result
) check can be replaced byRefCell::get_mut(&mut self) -> &mut T
and performing the operation on the&mut T
directly. This situation may come up when theRefCell
is created and did not yet disappear behind the immutable reference that is the reason for its existence. This is akin tomut_mutex_lock
.At least initially, I came up light with suggesting
MachineApplicable
due to the myriad waysRefCell
can be used and the fact that its methods return a guard (instead of a&mut
), which may not typecheck when changed.cargo lintcheck
comes up empty with this new lint.Notice that this is a
style
-lint because this is wheremut_mutex_lock
lives;perf
might actually be better suited?!I noticed that the language around
get_mut
quite strongly discourages its use, which may be a misunderstanding with respect to whenget_mut
can be used, as&mut RefCell
does not come up often. This is a separate issue.changelog: new lint: [
mut_refcell_borrow
]#9423