Skip to content

Commit 5c1254f

Browse files
committed
use .get().unwrap() in [T]::get_unchecked
1 parent cc705b8 commit 5c1254f

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

library/core/src/slice/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ impl<T> [T] {
662662
// SAFETY: the caller must uphold most of the safety requirements for `get_unchecked`;
663663
// the slice is dereferenceable because `self` is a safe reference.
664664
// The returned pointer is safe because impls of `SliceIndex` have to guarantee that it is.
665-
unsafe { &*index.get_unchecked(self) }
665+
unsafe { &*index.get(self).unwrap_unchecked() }
666666
}
667667

668668
/// Returns a mutable reference to an element or subslice, without doing

tests/codegen/issues/issue-116878.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// no-system-llvm
2+
// compile-flags: -O
3+
// ignore-debug: the debug assertions get in the way
4+
#![crate_type = "lib"]
5+
6+
/// Make sure no bounds checks are emitted after a `get_unchecked`.
7+
// CHECK-LABEL: @unchecked_slice_no_bounds_check
8+
#[no_mangle]
9+
pub unsafe fn unchecked_slice_no_bounds_check(s: &[u8]) -> u8 {
10+
let a = *s.get_unchecked(1);
11+
// CHECK-NOT: panic_bounds_check
12+
a + s[0]
13+
}

0 commit comments

Comments
 (0)