-
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
Should the mut_from_ref
lint apply to unsafe functions?
#4281
Comments
IMO it should also trigger on |
I think whether or not the function is Is it possible to filter this lint based on whether the returned reference is derived from |
IMO it should still lint if the function is not use std::cell::UnsafeCell;
fn get_mut<T>(ptr: &UnsafeCell<T>) -> &mut T {
unsafe { &mut *ptr.get() }
}
fn main() {
let x: UnsafeCell<i32> = UnsafeCell::new(42);
let y1: &mut i32 = get_mut(&x);
let y2: &mut i32 = get_mut(&x);
*y1 = 3;
*y2 = 4;
} Here, I want the lint to trigger. If |
playground
Should
mut_from_ref
apply to unsafe functions as well?It is trivial to work around that error by wrapping the mutable reference in a new struct.
Here is the refcell implementation that does something very similar.
I personally don't have any strong opinions in either direction. I just needed to implement something similar to RefCell (runtime borrowing) and I ran into this lint.
The text was updated successfully, but these errors were encountered: