From 8193e471f01f176590bd66ce1269d5a974a40f67 Mon Sep 17 00:00:00 2001 From: emma Date: Sun, 3 Mar 2024 17:26:40 -0700 Subject: [PATCH] changes redundant .unwrap_or_else(panic!()) to .unwrap() --- src/strerror.rs | 4 ++-- src/test.rs | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 src/test.rs diff --git a/src/strerror.rs b/src/strerror.rs index 117ed0b..01d98e2 100644 --- a/src/strerror.rs +++ b/src/strerror.rs @@ -15,12 +15,12 @@ extern "C" { fn strerror(errnum: c_int) -> *mut c_char; } /* wrapper function for use in Rust */ pub fn raw_message(err: std::io::Error) -> String { /* Get the raw OS error. If it’s None, what the hell is going on‽ */ - let error = err.raw_os_error().unwrap_or_else(|| { panic!() }) as c_int; + let errno = err.raw_os_error().unwrap() as c_int; /* Get a CStr from the error message so that it’s referenced and then * convert it to an owned value. If the string is not valid UTF-8, return * that error instead. */ - match unsafe { CStr::from_ptr(strerror(error)) }.to_str() { + match unsafe { CStr::from_ptr(strerror(errno)) }.to_str() { Ok(s) => s.to_owned(), // yay!! :D Err(e) => e.to_string(), // awww :( } diff --git a/src/test.rs b/src/test.rs new file mode 100644 index 0000000..4a602b1 --- /dev/null +++ b/src/test.rs @@ -0,0 +1,10 @@ +extern crate strerror; + +use strerror::raw_message; + +fn main() { + stdout.write_all(b"meow\n").unwrap_or_else(|e| { + eprintln!("{}", raw_message(e)); + std::process::exit(1); + }); +}