Skip to content

Commit 5170a3f

Browse files
committed
Auto merge of #63233 - RalfJung:get_unchecked, r=Centril
clarify that unchecked indexing is UB even if the reference is never used
2 parents 2c13edc + 9b5623f commit 5170a3f

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/libcore/slice/mod.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,13 @@ 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 *[undefined behavior]*
297+
/// even if the resulting reference is not used.
298+
/// For a safe alternative see [`get`].
297299
///
298300
/// [`get`]: #method.get
301+
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
299302
///
300303
/// # Examples
301304
///
@@ -317,10 +320,13 @@ impl<T> [T] {
317320
/// Returns a mutable reference to an element or subslice, without doing
318321
/// bounds checking.
319322
///
320-
/// This is generally not recommended, use with caution! For a safe
321-
/// alternative see [`get_mut`].
323+
/// This is generally not recommended, use with caution!
324+
/// Calling this method with an out-of-bounds index is *[undefined behavior]*
325+
/// even if the resulting reference is not used.
326+
/// For a safe alternative see [`get_mut`].
322327
///
323328
/// [`get_mut`]: #method.get_mut
329+
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
324330
///
325331
/// # Examples
326332
///
@@ -2629,11 +2635,17 @@ pub trait SliceIndex<T: ?Sized>: private_slice_index::Sealed {
26292635

26302636
/// Returns a shared reference to the output at this location, without
26312637
/// performing any bounds checking.
2638+
/// Calling this method with an out-of-bounds index is *[undefined behavior]*
2639+
/// even if the resulting reference is not used.
2640+
/// [undefined behavior]: ../../reference/behavior-considered-undefined.html
26322641
#[unstable(feature = "slice_index_methods", issue = "0")]
26332642
unsafe fn get_unchecked(self, slice: &T) -> &Self::Output;
26342643

26352644
/// Returns a mutable reference to the output at this location, without
26362645
/// performing any bounds checking.
2646+
/// Calling this method with an out-of-bounds index is *[undefined behavior]*
2647+
/// even if the resulting reference is not used.
2648+
/// [undefined behavior]: ../../reference/behavior-considered-undefined.html
26372649
#[unstable(feature = "slice_index_methods", issue = "0")]
26382650
unsafe fn get_unchecked_mut(self, slice: &mut T) -> &mut Self::Output;
26392651

0 commit comments

Comments
 (0)