Avoid panics for cannot-be-a-base URLs (#13406)

Following #13376, avoid `.unwrap()` on `Url::path_segments()`.

I also added some unwrap-safety comments.
This commit is contained in:
konsti 2025-05-13 04:29:26 +02:00 committed by GitHub
parent 6df588bb00
commit 3b125dbe71
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 7 additions and 2 deletions

View file

@ -65,6 +65,8 @@ impl CanonicalUrl {
.is_some_and(|ext| ext.eq_ignore_ascii_case("git"));
if needs_chopping {
let last = {
// Unwrap safety: We checked `url.cannot_be_a_base()`, and `url.path()` having
// an extension implies at least one segment.
let last = url.path_segments().unwrap().next_back().unwrap();
last[..last.len() - 4].to_owned()
};
@ -74,6 +76,7 @@ impl CanonicalUrl {
// Decode any percent-encoded characters in the path.
if memchr::memchr(b'%', url.path().as_bytes()).is_some() {
// Unwrap safety: We checked `url.cannot_be_a_base()`.
let decoded = url
.path_segments()
.unwrap()