Skip to content

Commit 9ff586f

Browse files
wutchzonejannau
authored andcommitted
rust: str: replace unwraps with question mark operators
Simplify the error handling by replacing unwraps with the question mark operator. Furthermore, unwraps can convey a wrong impression that unwrapping is fine in general, thus this patch removes this unwrapping. Suggested-by: Miguel Ojeda <[email protected]> Link: https://lore.kernel.org/rust-for-linux/CANiq72nsK1D4NuQ1U7NqMWoYjXkqQSj4QuUEL98OmFbq022Z9A@mail.gmail.com/ Reviewed-by: Alice Ryhl <[email protected]> Signed-off-by: Daniel Sedlak <[email protected]> Link: https://lore.kernel.org/r/[email protected] [ Slightly reworded commit. - Miguel ] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 6cc9a14 commit 9ff586f

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

rust/kernel/str.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ impl fmt::Display for BStr {
3939
/// ```
4040
/// # use kernel::{fmt, b_str, str::{BStr, CString}};
4141
/// let ascii = b_str!("Hello, BStr!");
42-
/// let s = CString::try_from_fmt(fmt!("{}", ascii)).unwrap();
42+
/// let s = CString::try_from_fmt(fmt!("{}", ascii))?;
4343
/// assert_eq!(s.as_bytes(), "Hello, BStr!".as_bytes());
4444
///
4545
/// let non_ascii = b_str!("🦀");
46-
/// let s = CString::try_from_fmt(fmt!("{}", non_ascii)).unwrap();
46+
/// let s = CString::try_from_fmt(fmt!("{}", non_ascii))?;
4747
/// assert_eq!(s.as_bytes(), "\\xf0\\x9f\\xa6\\x80".as_bytes());
48+
/// # Ok::<(), kernel::error::Error>(())
4849
/// ```
4950
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5051
for &b in &self.0 {
@@ -70,12 +71,13 @@ impl fmt::Debug for BStr {
7071
/// # use kernel::{fmt, b_str, str::{BStr, CString}};
7172
/// // Embedded double quotes are escaped.
7273
/// let ascii = b_str!("Hello, \"BStr\"!");
73-
/// let s = CString::try_from_fmt(fmt!("{:?}", ascii)).unwrap();
74+
/// let s = CString::try_from_fmt(fmt!("{:?}", ascii))?;
7475
/// assert_eq!(s.as_bytes(), "\"Hello, \\\"BStr\\\"!\"".as_bytes());
7576
///
7677
/// let non_ascii = b_str!("😺");
77-
/// let s = CString::try_from_fmt(fmt!("{:?}", non_ascii)).unwrap();
78+
/// let s = CString::try_from_fmt(fmt!("{:?}", non_ascii))?;
7879
/// assert_eq!(s.as_bytes(), "\"\\xf0\\x9f\\x98\\xba\"".as_bytes());
80+
/// # Ok::<(), kernel::error::Error>(())
7981
/// ```
8082
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
8183
f.write_char('"')?;
@@ -273,8 +275,9 @@ impl CStr {
273275
///
274276
/// ```
275277
/// # use kernel::str::CStr;
276-
/// let cstr = CStr::from_bytes_with_nul(b"foo\0").unwrap();
278+
/// let cstr = CStr::from_bytes_with_nul(b"foo\0")?;
277279
/// assert_eq!(cstr.to_str(), Ok("foo"));
280+
/// # Ok::<(), kernel::error::Error>(())
278281
/// ```
279282
#[inline]
280283
pub fn to_str(&self) -> Result<&str, core::str::Utf8Error> {
@@ -384,12 +387,13 @@ impl fmt::Display for CStr {
384387
/// # use kernel::str::CStr;
385388
/// # use kernel::str::CString;
386389
/// let penguin = c_str!("🐧");
387-
/// let s = CString::try_from_fmt(fmt!("{}", penguin)).unwrap();
390+
/// let s = CString::try_from_fmt(fmt!("{}", penguin))?;
388391
/// assert_eq!(s.as_bytes_with_nul(), "\\xf0\\x9f\\x90\\xa7\0".as_bytes());
389392
///
390393
/// let ascii = c_str!("so \"cool\"");
391-
/// let s = CString::try_from_fmt(fmt!("{}", ascii)).unwrap();
394+
/// let s = CString::try_from_fmt(fmt!("{}", ascii))?;
392395
/// assert_eq!(s.as_bytes_with_nul(), "so \"cool\"\0".as_bytes());
396+
/// # Ok::<(), kernel::error::Error>(())
393397
/// ```
394398
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
395399
for &c in self.as_bytes() {
@@ -413,13 +417,14 @@ impl fmt::Debug for CStr {
413417
/// # use kernel::str::CStr;
414418
/// # use kernel::str::CString;
415419
/// let penguin = c_str!("🐧");
416-
/// let s = CString::try_from_fmt(fmt!("{:?}", penguin)).unwrap();
420+
/// let s = CString::try_from_fmt(fmt!("{:?}", penguin))?;
417421
/// assert_eq!(s.as_bytes_with_nul(), "\"\\xf0\\x9f\\x90\\xa7\"\0".as_bytes());
418422
///
419423
/// // Embedded double quotes are escaped.
420424
/// let ascii = c_str!("so \"cool\"");
421-
/// let s = CString::try_from_fmt(fmt!("{:?}", ascii)).unwrap();
425+
/// let s = CString::try_from_fmt(fmt!("{:?}", ascii))?;
422426
/// assert_eq!(s.as_bytes_with_nul(), "\"so \\\"cool\\\"\"\0".as_bytes());
427+
/// # Ok::<(), kernel::error::Error>(())
423428
/// ```
424429
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
425430
f.write_str("\"")?;
@@ -801,16 +806,17 @@ impl fmt::Write for Formatter {
801806
/// ```
802807
/// use kernel::{str::CString, fmt};
803808
///
804-
/// let s = CString::try_from_fmt(fmt!("{}{}{}", "abc", 10, 20)).unwrap();
809+
/// let s = CString::try_from_fmt(fmt!("{}{}{}", "abc", 10, 20))?;
805810
/// assert_eq!(s.as_bytes_with_nul(), "abc1020\0".as_bytes());
806811
///
807812
/// let tmp = "testing";
808-
/// let s = CString::try_from_fmt(fmt!("{tmp}{}", 123)).unwrap();
813+
/// let s = CString::try_from_fmt(fmt!("{tmp}{}", 123))?;
809814
/// assert_eq!(s.as_bytes_with_nul(), "testing123\0".as_bytes());
810815
///
811816
/// // This fails because it has an embedded `NUL` byte.
812817
/// let s = CString::try_from_fmt(fmt!("a\0b{}", 123));
813818
/// assert_eq!(s.is_ok(), false);
819+
/// # Ok::<(), kernel::error::Error>(())
814820
/// ```
815821
pub struct CString {
816822
buf: KVec<u8>,

0 commit comments

Comments
 (0)