Skip to content

Commit f645628

Browse files
committedDec 26, 2023
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
2 parents 1ab7831 + c082c1c commit f645628

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed
 

Diff for: ‎library/core/src/slice/iter.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,12 @@ where
458458
match self.v.iter().position(|x| (self.pred)(x)) {
459459
None => self.finish(),
460460
Some(idx) => {
461-
let ret = Some(&self.v[..idx]);
462-
self.v = &self.v[idx + 1..];
461+
let (left, right) =
462+
// SAFETY: if v.iter().position returns Some(idx), that
463+
// idx is definitely a valid index for v
464+
unsafe { (self.v.get_unchecked(..idx), self.v.get_unchecked(idx + 1..)) };
465+
let ret = Some(left);
466+
self.v = right;
463467
ret
464468
}
465469
}
@@ -491,8 +495,12 @@ where
491495
match self.v.iter().rposition(|x| (self.pred)(x)) {
492496
None => self.finish(),
493497
Some(idx) => {
494-
let ret = Some(&self.v[idx + 1..]);
495-
self.v = &self.v[..idx];
498+
let (left, right) =
499+
// SAFETY: if v.iter().rposition returns Some(idx), then
500+
// idx is definitely a valid index for v
501+
unsafe { (self.v.get_unchecked(..idx), self.v.get_unchecked(idx + 1..)) };
502+
let ret = Some(right);
503+
self.v = left;
496504
ret
497505
}
498506
}

0 commit comments

Comments
 (0)
Please sign in to comment.