Skip to content

Commit fbe454e

Browse files
CopilotByron
andcommitted
Replace all unreachable! with proper error handling in gix-url
Co-authored-by: Byron <63622+Byron@users.noreply.github.com>
1 parent b2afc1c commit fbe454e

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

gix-url/src/lib.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,12 @@ impl Url {
353353
out.write_all(host.as_bytes())?;
354354
}
355355
(None, None) => {}
356-
(Some(_user), None) => unreachable!("BUG: should not be possible to have a user but no host"),
356+
(Some(_user), None) => {
357+
return Err(std::io::Error::new(
358+
std::io::ErrorKind::InvalidData,
359+
"Invalid URL structure: user specified without host"
360+
));
361+
}
357362
}
358363
if let Some(port) = &self.port {
359364
write!(out, ":{port}")?;
@@ -377,7 +382,12 @@ impl Url {
377382
out.write_all(host.as_bytes())?;
378383
}
379384
(None, None) => {}
380-
(Some(_user), None) => unreachable!("BUG: should not be possible to have a user but no host"),
385+
(Some(_user), None) => {
386+
return Err(std::io::Error::new(
387+
std::io::ErrorKind::InvalidData,
388+
"Invalid URL structure: user specified without host"
389+
));
390+
}
381391
}
382392
assert!(self.port.is_none(), "BUG: cannot serialize port in alternative form");
383393
if self.scheme == Scheme::Ssh {

gix-url/src/parse.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,15 @@ pub enum Error {
2828
MissingRepositoryPath { url: BString, kind: UrlKind },
2929
#[error("URL {url:?} is relative which is not allowed in this context")]
3030
RelativeUrl { url: String },
31+
#[error("URL has invalid structure: user specified without host")]
32+
InvalidUrlStructure,
3133
}
3234

3335
impl From<Infallible> for Error {
3436
fn from(_: Infallible) -> Self {
35-
unreachable!("Cannot actually happen, but it seems there can't be a blanket impl for this")
37+
// This should theoretically never happen since Infallible can never be constructed,
38+
// but with public URL fields, we need to handle it gracefully
39+
Error::InvalidUrlStructure
3640
}
3741
}
3842

gix-url/tests/url/expand_path.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ fn without_username() -> crate::Result {
3939
fn with_username() -> crate::Result {
4040
let (user, resolved_path) = expand_path::parse(b"/~byron/hello/git".as_bstr())?;
4141
let resolved_path = expand_path::with(user.as_ref(), resolved_path.as_ref(), |user: &ForUser| match user {
42-
ForUser::Current => unreachable!("we have a name"),
42+
ForUser::Current => {
43+
// This test expects a specific user name, not the current user
44+
None // Return None to trigger the MissingHome error
45+
},
4346
ForUser::Name(name) => Some(user_home(name.to_str_lossy().as_ref())),
4447
})?;
4548
assert_eq!(resolved_path, expected_path());

0 commit comments

Comments
 (0)