diff options
| author | Matthias Krüger <476013+matthiaskrgr@users.noreply.github.com> | 2025-04-03 07:39:05 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-03 07:39:05 +0200 |
| commit | e332aa89a73f90259aafa69d57e74dc07ce4c466 (patch) | |
| tree | 1ec77d03747933716546035c7f8b1decd8c3acc8 /compiler/rustc_parse/src | |
| parent | dbd7f52c83edcf5710f08a063cbe1016b5f6d621 (diff) | |
| parent | 59ca7679c7db634465b5f021060f143567824ac4 (diff) | |
| download | rust-e332aa89a73f90259aafa69d57e74dc07ce4c466.tar.gz rust-e332aa89a73f90259aafa69d57e74dc07ce4c466.zip | |
Rollup merge of #139145 - okaneco:safe_splits, r=Amanieu
slice: Remove some uses of unsafe in first/last chunk methods
Remove unsafe `split_at_unchecked` and `split_at_mut_unchecked` in some slice `split_first_chunk`/`split_last_chunk` methods.
Replace those calls with the safe `split_at` and `split_at_checked` where applicable.
Add codegen tests to check for no panics when calculating the last chunk index using `checked_sub` and `split_at`.
Better viewed with whitespace disabled in diff view
---
The unchecked calls are mostly manual implementations of the safe methods, but with the safety condition negated from `mid <= len` to `len < mid`.
```rust
if self.len() < N {
None
} else {
// SAFETY: We manually verified the bounds of the split.
let (first, tail) = unsafe { self.split_at_unchecked(N) };
// Or for the last_chunk methods
let (init, last) = unsafe { self.split_at_unchecked(self.len() - N) };
```
Unsafe is still needed for the pointer array casts. Their safety comments are unmodified.
Diffstat (limited to 'compiler/rustc_parse/src')
0 files changed, 0 insertions, 0 deletions
