Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rust: str: take advantage of the `-> Result` support in KUnit `#[test]`'s

Since now we have support for returning `-> Result`s, we can convert some
of these tests to use the feature, and serve as a first user for it too.

Thus convert them, which allows us to remove some `unwrap()`s.

We keep the actual assertions we want to make as explicit ones with
`assert*!`s.

Reviewed-by: David Gow <davidgow@google.com>
Acked-by: Danilo Krummrich <dakr@kernel.org>
Link: https://lore.kernel.org/r/20250502215133.1923676-6-ojeda@kernel.org
[ Split the `CString` simplification into a new commit. - Miguel ]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

+30 -22
+30 -22
rust/kernel/str.rs
··· 578 578 579 579 macro_rules! format { 580 580 ($($f:tt)*) => ({ 581 - CString::try_from_fmt(::kernel::fmt!($($f)*)).unwrap().to_str().unwrap() 581 + CString::try_from_fmt(::kernel::fmt!($($f)*))?.to_str()? 582 582 }) 583 583 } 584 584 ··· 597 597 \\xf0\\xf1\\xf2\\xf3\\xf4\\xf5\\xf6\\xf7\\xf8\\xf9\\xfa\\xfb\\xfc\\xfd\\xfe\\xff"; 598 598 599 599 #[test] 600 - fn test_cstr_to_str() { 600 + fn test_cstr_to_str() -> Result { 601 601 let good_bytes = b"\xf0\x9f\xa6\x80\0"; 602 - let checked_cstr = CStr::from_bytes_with_nul(good_bytes).unwrap(); 603 - let checked_str = checked_cstr.to_str().unwrap(); 602 + let checked_cstr = CStr::from_bytes_with_nul(good_bytes)?; 603 + let checked_str = checked_cstr.to_str()?; 604 604 assert_eq!(checked_str, "🦀"); 605 + Ok(()) 605 606 } 606 607 607 608 #[test] 608 - fn test_cstr_to_str_invalid_utf8() { 609 + fn test_cstr_to_str_invalid_utf8() -> Result { 609 610 let bad_bytes = b"\xc3\x28\0"; 610 - let checked_cstr = CStr::from_bytes_with_nul(bad_bytes).unwrap(); 611 + let checked_cstr = CStr::from_bytes_with_nul(bad_bytes)?; 611 612 assert!(checked_cstr.to_str().is_err()); 613 + Ok(()) 612 614 } 613 615 614 616 #[test] 615 - fn test_cstr_as_str_unchecked() { 617 + fn test_cstr_as_str_unchecked() -> Result { 616 618 let good_bytes = b"\xf0\x9f\x90\xA7\0"; 617 - let checked_cstr = CStr::from_bytes_with_nul(good_bytes).unwrap(); 619 + let checked_cstr = CStr::from_bytes_with_nul(good_bytes)?; 618 620 // SAFETY: The contents come from a string literal which contains valid UTF-8. 619 621 let unchecked_str = unsafe { checked_cstr.as_str_unchecked() }; 620 622 assert_eq!(unchecked_str, "🐧"); 623 + Ok(()) 621 624 } 622 625 623 626 #[test] 624 - fn test_cstr_display() { 625 - let hello_world = CStr::from_bytes_with_nul(b"hello, world!\0").unwrap(); 627 + fn test_cstr_display() -> Result { 628 + let hello_world = CStr::from_bytes_with_nul(b"hello, world!\0")?; 626 629 assert_eq!(format!("{hello_world}"), "hello, world!"); 627 - let non_printables = CStr::from_bytes_with_nul(b"\x01\x09\x0a\0").unwrap(); 630 + let non_printables = CStr::from_bytes_with_nul(b"\x01\x09\x0a\0")?; 628 631 assert_eq!(format!("{non_printables}"), "\\x01\\x09\\x0a"); 629 - let non_ascii = CStr::from_bytes_with_nul(b"d\xe9j\xe0 vu\0").unwrap(); 632 + let non_ascii = CStr::from_bytes_with_nul(b"d\xe9j\xe0 vu\0")?; 630 633 assert_eq!(format!("{non_ascii}"), "d\\xe9j\\xe0 vu"); 631 - let good_bytes = CStr::from_bytes_with_nul(b"\xf0\x9f\xa6\x80\0").unwrap(); 634 + let good_bytes = CStr::from_bytes_with_nul(b"\xf0\x9f\xa6\x80\0")?; 632 635 assert_eq!(format!("{good_bytes}"), "\\xf0\\x9f\\xa6\\x80"); 636 + Ok(()) 633 637 } 634 638 635 639 #[test] 636 - fn test_cstr_display_all_bytes() { 640 + fn test_cstr_display_all_bytes() -> Result { 637 641 let mut bytes: [u8; 256] = [0; 256]; 638 642 // fill `bytes` with [1..=255] + [0] 639 643 for i in u8::MIN..=u8::MAX { 640 644 bytes[i as usize] = i.wrapping_add(1); 641 645 } 642 - let cstr = CStr::from_bytes_with_nul(&bytes).unwrap(); 646 + let cstr = CStr::from_bytes_with_nul(&bytes)?; 643 647 assert_eq!(format!("{cstr}"), ALL_ASCII_CHARS); 648 + Ok(()) 644 649 } 645 650 646 651 #[test] 647 - fn test_cstr_debug() { 648 - let hello_world = CStr::from_bytes_with_nul(b"hello, world!\0").unwrap(); 652 + fn test_cstr_debug() -> Result { 653 + let hello_world = CStr::from_bytes_with_nul(b"hello, world!\0")?; 649 654 assert_eq!(format!("{hello_world:?}"), "\"hello, world!\""); 650 - let non_printables = CStr::from_bytes_with_nul(b"\x01\x09\x0a\0").unwrap(); 655 + let non_printables = CStr::from_bytes_with_nul(b"\x01\x09\x0a\0")?; 651 656 assert_eq!(format!("{non_printables:?}"), "\"\\x01\\x09\\x0a\""); 652 - let non_ascii = CStr::from_bytes_with_nul(b"d\xe9j\xe0 vu\0").unwrap(); 657 + let non_ascii = CStr::from_bytes_with_nul(b"d\xe9j\xe0 vu\0")?; 653 658 assert_eq!(format!("{non_ascii:?}"), "\"d\\xe9j\\xe0 vu\""); 654 - let good_bytes = CStr::from_bytes_with_nul(b"\xf0\x9f\xa6\x80\0").unwrap(); 659 + let good_bytes = CStr::from_bytes_with_nul(b"\xf0\x9f\xa6\x80\0")?; 655 660 assert_eq!(format!("{good_bytes:?}"), "\"\\xf0\\x9f\\xa6\\x80\""); 661 + Ok(()) 656 662 } 657 663 658 664 #[test] 659 - fn test_bstr_display() { 665 + fn test_bstr_display() -> Result { 660 666 let hello_world = BStr::from_bytes(b"hello, world!"); 661 667 assert_eq!(format!("{hello_world}"), "hello, world!"); 662 668 let escapes = BStr::from_bytes(b"_\t_\n_\r_\\_\'_\"_"); ··· 673 667 assert_eq!(format!("{non_ascii}"), "d\\xe9j\\xe0 vu"); 674 668 let good_bytes = BStr::from_bytes(b"\xf0\x9f\xa6\x80"); 675 669 assert_eq!(format!("{good_bytes}"), "\\xf0\\x9f\\xa6\\x80"); 670 + Ok(()) 676 671 } 677 672 678 673 #[test] 679 - fn test_bstr_debug() { 674 + fn test_bstr_debug() -> Result { 680 675 let hello_world = BStr::from_bytes(b"hello, world!"); 681 676 assert_eq!(format!("{hello_world:?}"), "\"hello, world!\""); 682 677 let escapes = BStr::from_bytes(b"_\t_\n_\r_\\_\'_\"_"); ··· 688 681 assert_eq!(format!("{non_ascii:?}"), "\"d\\xe9j\\xe0 vu\""); 689 682 let good_bytes = BStr::from_bytes(b"\xf0\x9f\xa6\x80"); 690 683 assert_eq!(format!("{good_bytes:?}"), "\"\\xf0\\x9f\\xa6\\x80\""); 684 + Ok(()) 691 685 } 692 686 } 693 687