Skip to content

Commit

Permalink
Merge pull request #1072 from vcfxb/remove-error-unwraps
Browse files Browse the repository at this point in the history
(Breaking Changes) Remove error unwraps
  • Loading branch information
ehuss authored Jan 4, 2025
2 parents 8977c0c + 788306c commit 92d96ac
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 18 deletions.
4 changes: 1 addition & 3 deletions src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ pub fn c_try(ret: libc::c_int) -> Result<libc::c_int, Error> {
}

pub fn last_error(code: libc::c_int) -> Error {
// nowadays this unwrap is safe as `Error::last_error` always returns
// `Some`.
Error::last_error(code).unwrap()
Error::last_error(code)
}

mod impls {
Expand Down
9 changes: 2 additions & 7 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ impl Error {
///
/// The `code` argument typically comes from the return value of a function
/// call. This code will later be returned from the `code` function.
///
/// Historically this function returned `Some` or `None` based on the return
/// value of `git_error_last` but nowadays it always returns `Some` so it's
/// safe to unwrap the return value. This API will change in the next major
/// version.
pub fn last_error(code: c_int) -> Option<Error> {
pub fn last_error(code: c_int) -> Error {
crate::init();
unsafe {
// Note that whenever libgit2 returns an error any negative value
Expand All @@ -64,7 +59,7 @@ impl Error {
Error::from_raw(code, ptr)
};
raw::git_error_clear();
Some(err)
err
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,7 @@ impl io::Write for Indexer<'_> {

let res = raw::git_indexer_append(self.raw, ptr, len, &mut self.progress);
if res < 0 {
Err(io::Error::new(
io::ErrorKind::Other,
Error::last_error(res).unwrap(),
))
Err(io::Error::new(io::ErrorKind::Other, Error::last_error(res)))
} else {
Ok(buf.len())
}
Expand Down
2 changes: 1 addition & 1 deletion src/odb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ impl<'repo> OdbPackwriter<'repo> {
};

if res < 0 {
Err(Error::last_error(res).unwrap())
Err(Error::last_error(res))
} else {
Ok(res)
}
Expand Down
2 changes: 1 addition & 1 deletion src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ impl Repository {
match value {
0 => Ok(false),
1 => Ok(true),
_ => Err(Error::last_error(value).unwrap()),
_ => Err(Error::last_error(value)),
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ pub fn trace_set(level: TraceLevel, cb: TracingCb) -> Result<(), Error> {
let return_code: c_int = unsafe { raw::git_trace_set(level.raw(), Some(tracing_cb_c)) };

if return_code != 0 {
// Unwrap here is fine since `Error::last_error` always returns `Some`.
Err(Error::last_error(return_code).unwrap())
Err(Error::last_error(return_code))
} else {
Ok(())
}
Expand Down

0 comments on commit 92d96ac

Please sign in to comment.