Skip to content

Commit f44abba

Browse files
committed
clarify that unchecked indexing is UB even if the reference is never used
1 parent d727071 commit f44abba

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/libcore/slice/mod.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,10 @@ impl<T> [T] {
292292
/// Returns a reference to an element or subslice, without doing bounds
293293
/// checking.
294294
///
295-
/// This is generally not recommended, use with caution! For a safe
296-
/// alternative see [`get`].
295+
/// This is generally not recommended, use with caution!
296+
/// Calling this method with an out-of-bounds index is UB even if the resulting
297+
/// reference is not used.
298+
/// For a safe alternative see [`get`].
297299
///
298300
/// [`get`]: #method.get
299301
///
@@ -317,8 +319,10 @@ impl<T> [T] {
317319
/// Returns a mutable reference to an element or subslice, without doing
318320
/// bounds checking.
319321
///
320-
/// This is generally not recommended, use with caution! For a safe
321-
/// alternative see [`get_mut`].
322+
/// This is generally not recommended, use with caution!
323+
/// Calling this method with an out-of-bounds index is UB even if the resulting
324+
/// reference is not used.
325+
/// For a safe alternative see [`get_mut`].
322326
///
323327
/// [`get_mut`]: #method.get_mut
324328
///
@@ -2629,11 +2633,15 @@ pub trait SliceIndex<T: ?Sized>: private_slice_index::Sealed {
26292633

26302634
/// Returns a shared reference to the output at this location, without
26312635
/// performing any bounds checking.
2636+
/// Calling this method with an out-of-bounds index is UB even if the resulting
2637+
/// reference is not used.
26322638
#[unstable(feature = "slice_index_methods", issue = "0")]
26332639
unsafe fn get_unchecked(self, slice: &T) -> &Self::Output;
26342640

26352641
/// Returns a mutable reference to the output at this location, without
26362642
/// performing any bounds checking.
2643+
/// Calling this method with an out-of-bounds index is UB even if the resulting
2644+
/// reference is not used.
26372645
#[unstable(feature = "slice_index_methods", issue = "0")]
26382646
unsafe fn get_unchecked_mut(self, slice: &mut T) -> &mut Self::Output;
26392647

0 commit comments

Comments
 (0)