We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
saturating_sub
1 parent cd43430 commit 163594cCopy full SHA for 163594c
tests/codegen-llvm/issues/saturating-sub-index-139759.rs
@@ -0,0 +1,19 @@
1
+// Test that calculating an index with saturating subtraction from an in-bounds
2
+// index doesn't generate another bounds check.
3
+
4
+//@ compile-flags: -Copt-level=3
5
+//@ min-llvm-version: 21
6
7
+#![crate_type = "lib"]
8
9
+// CHECK-LABEL: @bounds_check_is_elided
10
+#[no_mangle]
11
+pub fn bounds_check_is_elided(s: &[i32], index: usize) -> i32 {
12
+ // CHECK-NOT: panic_bounds_check
13
+ if index < s.len() {
14
+ let lower_bound = index.saturating_sub(1);
15
+ s[lower_bound]
16
+ } else {
17
+ -1
18
+ }
19
+}
0 commit comments