Skip to content

Commit 5886c38

Browse files
Replace unneeded unsafe calls to .get() with calls to .get_mut()
1 parent 8169989 commit 5886c38

File tree

5 files changed

+7
-16
lines changed

5 files changed

+7
-16
lines changed

library/core/src/cell.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -496,10 +496,7 @@ impl<T: ?Sized> Cell<T> {
496496
#[inline]
497497
#[stable(feature = "cell_get_mut", since = "1.11.0")]
498498
pub fn get_mut(&mut self) -> &mut T {
499-
// SAFETY: This can cause data races if called from a separate thread,
500-
// but `Cell` is `!Sync` so this won't happen, and `&mut` guarantees
501-
// unique access.
502-
unsafe { &mut *self.value.get() }
499+
self.value.get_mut()
503500
}
504501

505502
/// Returns a `&Cell<T>` from a `&mut T`
@@ -945,8 +942,7 @@ impl<T: ?Sized> RefCell<T> {
945942
#[inline]
946943
#[stable(feature = "cell_get_mut", since = "1.11.0")]
947944
pub fn get_mut(&mut self) -> &mut T {
948-
// SAFETY: `&mut` guarantees unique access.
949-
unsafe { &mut *self.value.get() }
945+
self.value.get_mut()
950946
}
951947

952948
/// Undo the effect of leaked guards on the borrow state of the `RefCell`.

library/core/src/sync/atomic.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -838,8 +838,7 @@ impl<T> AtomicPtr<T> {
838838
#[inline]
839839
#[stable(feature = "atomic_access", since = "1.15.0")]
840840
pub fn get_mut(&mut self) -> &mut *mut T {
841-
// SAFETY: the mutable reference guarantees unique ownership.
842-
unsafe { &mut *self.p.get() }
841+
self.p.get_mut()
843842
}
844843

845844
/// Get atomic access to a pointer.
@@ -1275,8 +1274,7 @@ assert_eq!(some_var.load(Ordering::SeqCst), 5);
12751274
#[inline]
12761275
#[$stable_access]
12771276
pub fn get_mut(&mut self) -> &mut $int_type {
1278-
// SAFETY: the mutable reference guarantees unique ownership.
1279-
unsafe { &mut *self.v.get() }
1277+
self.v.get_mut()
12801278
}
12811279
}
12821280

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@
315315
#![feature(try_reserve)]
316316
#![feature(unboxed_closures)]
317317
#![feature(unsafe_block_in_unsafe_fn)]
318+
#![feature(unsafe_cell_get_mut)]
318319
#![feature(unsafe_cell_raw_get)]
319320
#![feature(untagged_unions)]
320321
#![feature(unwind_attributes)]

library/std/src/sync/mutex.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,7 @@ impl<T: ?Sized> Mutex<T> {
406406
/// ```
407407
#[stable(feature = "mutex_get_mut", since = "1.6.0")]
408408
pub fn get_mut(&mut self) -> LockResult<&mut T> {
409-
// We know statically that there are no other references to `self`, so
410-
// there's no need to lock the inner mutex.
411-
let data = unsafe { &mut *self.data.get() };
409+
let data = self.data.get_mut();
412410
poison::map_result(self.poison.borrow(), |_| data)
413411
}
414412
}

library/std/src/sync/rwlock.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,7 @@ impl<T: ?Sized> RwLock<T> {
404404
/// ```
405405
#[stable(feature = "rwlock_get_mut", since = "1.6.0")]
406406
pub fn get_mut(&mut self) -> LockResult<&mut T> {
407-
// We know statically that there are no other references to `self`, so
408-
// there's no need to lock the inner lock.
409-
let data = unsafe { &mut *self.data.get() };
407+
let data = self.data.get_mut();
410408
poison::map_result(self.poison.borrow(), |_| data)
411409
}
412410
}

0 commit comments

Comments
 (0)