about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-12-26 06:24:22 +0000
committerbors <bors@rust-lang.org>2023-12-26 06:24:22 +0000
commitf6456285ddca7db0ffc663d02d9ecaf22d1f0d82 (patch)
treeadb3ae0a1472a693e79543eb856d80f304fb530e
parent1ab783112ab4e4807304dbd249b39771246013ef (diff)
parentc082c1c4c37cb672e295a4cb1700fafb51d1e824 (diff)
downloadrust-f6456285ddca7db0ffc663d02d9ecaf22d1f0d82.tar.gz
rust-f6456285ddca7db0ffc663d02d9ecaf22d1f0d82.zip
Auto merge of #119285 - lch361:split-no-panic, r=the8472
Removed redundant bounds checking at Split's next and next_back methods

Since these methods are using `Iterator::rposition`, which always returns a valid index, then there is no point in regular indexing with bounds checking
-rw-r--r--library/core/src/slice/iter.rs16
1 files changed, 12 insertions, 4 deletions
diff --git a/library/core/src/slice/iter.rs b/library/core/src/slice/iter.rs
index fc54ea23770..3d58afd26ea 100644
--- a/library/core/src/slice/iter.rs
+++ b/library/core/src/slice/iter.rs
@@ -458,8 +458,12 @@ where
         match self.v.iter().position(|x| (self.pred)(x)) {
             None => self.finish(),
             Some(idx) => {
-                let ret = Some(&self.v[..idx]);
-                self.v = &self.v[idx + 1..];
+                let (left, right) =
+                    // SAFETY: if v.iter().position returns Some(idx), that
+                    // idx is definitely a valid index for v
+                    unsafe { (self.v.get_unchecked(..idx), self.v.get_unchecked(idx + 1..)) };
+                let ret = Some(left);
+                self.v = right;
                 ret
             }
         }
@@ -491,8 +495,12 @@ where
         match self.v.iter().rposition(|x| (self.pred)(x)) {
             None => self.finish(),
             Some(idx) => {
-                let ret = Some(&self.v[idx + 1..]);
-                self.v = &self.v[..idx];
+                let (left, right) =
+                    // SAFETY: if v.iter().rposition returns Some(idx), then
+                    // idx is definitely a valid index for v
+                    unsafe { (self.v.get_unchecked(..idx), self.v.get_unchecked(idx + 1..)) };
+                let ret = Some(right);
+                self.v = left;
                 ret
             }
         }