[PATCH] rust: kunit: replace `kernel::c_str!` with C-Strings

Tamir Duberstein posted 1 patch 1 month, 2 weeks ago
rust/kernel/kunit.rs        | 11 ++++-------
rust/macros/kunit.rs        | 10 +++++-----
scripts/rustdoc_test_gen.rs |  4 ++--
3 files changed, 11 insertions(+), 14 deletions(-)
[PATCH] rust: kunit: replace `kernel::c_str!` with C-Strings
Posted by Tamir Duberstein 1 month, 2 weeks ago
From: Tamir Duberstein <tamird@gmail.com>

C-String literals were added in Rust 1.77. Replace instances of
`kernel::c_str!` with C-String literals where possible.

Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Benno Lossin <lossin@kernel.org>
Signed-off-by: Tamir Duberstein <tamird@gmail.com>
---
 rust/kernel/kunit.rs        | 11 ++++-------
 rust/macros/kunit.rs        | 10 +++++-----
 scripts/rustdoc_test_gen.rs |  4 ++--
 3 files changed, 11 insertions(+), 14 deletions(-)

diff --git a/rust/kernel/kunit.rs b/rust/kernel/kunit.rs
index 79436509dd73..21aef6c97325 100644
--- a/rust/kernel/kunit.rs
+++ b/rust/kernel/kunit.rs
@@ -9,9 +9,6 @@
 use crate::fmt;
 use crate::prelude::*;
 
-#[cfg(CONFIG_PRINTK)]
-use crate::c_str;
-
 /// Prints a KUnit error-level message.
 ///
 /// Public but hidden since it should only be used from KUnit generated code.
@@ -22,7 +19,7 @@ pub fn err(args: fmt::Arguments<'_>) {
     #[cfg(CONFIG_PRINTK)]
     unsafe {
         bindings::_printk(
-            c_str!("\x013%pA").as_char_ptr(),
+            c"\x013%pA".as_char_ptr(),
             core::ptr::from_ref(&args).cast::<c_void>(),
         );
     }
@@ -38,7 +35,7 @@ pub fn info(args: fmt::Arguments<'_>) {
     #[cfg(CONFIG_PRINTK)]
     unsafe {
         bindings::_printk(
-            c_str!("\x016%pA").as_char_ptr(),
+            c"\x016%pA".as_char_ptr(),
             core::ptr::from_ref(&args).cast::<c_void>(),
         );
     }
@@ -60,7 +57,7 @@ macro_rules! kunit_assert {
                 break 'out;
             }
 
-            static FILE: &'static $crate::str::CStr = $crate::c_str!($file);
+            static FILE: &'static $crate::str::CStr = $file;
             static LINE: i32 = ::core::line!() as i32 - $diff;
             static CONDITION: &'static $crate::str::CStr = $crate::c_str!(stringify!($condition));
 
@@ -253,7 +250,7 @@ pub const fn kunit_case_null() -> kernel::bindings::kunit_case {
 /// }
 ///
 /// static mut KUNIT_TEST_CASES: [kernel::bindings::kunit_case; 2] = [
-///     kernel::kunit::kunit_case(kernel::c_str!("name"), test_fn),
+///     kernel::kunit::kunit_case(c"name", test_fn),
 ///     kernel::kunit::kunit_case_null(),
 /// ];
 /// kernel::kunit_unsafe_test_suite!(suite_name, KUNIT_TEST_CASES);
diff --git a/rust/macros/kunit.rs b/rust/macros/kunit.rs
index b395bb053695..3d7724b35c0f 100644
--- a/rust/macros/kunit.rs
+++ b/rust/macros/kunit.rs
@@ -102,8 +102,8 @@ pub(crate) fn kunit_tests(attr: TokenStream, ts: TokenStream) -> TokenStream {
     // unsafe extern "C" fn kunit_rust_wrapper_bar(_test: *mut ::kernel::bindings::kunit) { bar(); }
     //
     // static mut TEST_CASES: [::kernel::bindings::kunit_case; 3] = [
-    //     ::kernel::kunit::kunit_case(::kernel::c_str!("foo"), kunit_rust_wrapper_foo),
-    //     ::kernel::kunit::kunit_case(::kernel::c_str!("bar"), kunit_rust_wrapper_bar),
+    //     ::kernel::kunit::kunit_case(c"foo", kunit_rust_wrapper_foo),
+    //     ::kernel::kunit::kunit_case(c"bar", kunit_rust_wrapper_bar),
     //     ::kernel::kunit::kunit_case_null(),
     // ];
     //
@@ -133,7 +133,7 @@ pub(crate) fn kunit_tests(attr: TokenStream, ts: TokenStream) -> TokenStream {
         writeln!(kunit_macros, "{kunit_wrapper}").unwrap();
         writeln!(
             test_cases,
-            "    ::kernel::kunit::kunit_case(::kernel::c_str!(\"{test}\"), {kunit_wrapper_fn_name}),"
+            "    ::kernel::kunit::kunit_case(c\"{test}\", {kunit_wrapper_fn_name}),"
         )
         .unwrap();
         writeln!(
@@ -143,7 +143,7 @@ pub(crate) fn kunit_tests(attr: TokenStream, ts: TokenStream) -> TokenStream {
 #[allow(unused)]
 macro_rules! assert {{
     ($cond:expr $(,)?) => {{{{
-        kernel::kunit_assert!("{test}", "{path}", 0, $cond);
+        kernel::kunit_assert!("{test}", c"{path}", 0, $cond);
     }}}}
 }}
 
@@ -151,7 +151,7 @@ macro_rules! assert {{
 #[allow(unused)]
 macro_rules! assert_eq {{
     ($left:expr, $right:expr $(,)?) => {{{{
-        kernel::kunit_assert_eq!("{test}", "{path}", 0, $left, $right);
+        kernel::kunit_assert_eq!("{test}", c"{path}", 0, $left, $right);
     }}}}
 }}
         "#
diff --git a/scripts/rustdoc_test_gen.rs b/scripts/rustdoc_test_gen.rs
index be0561049660..967064ebd391 100644
--- a/scripts/rustdoc_test_gen.rs
+++ b/scripts/rustdoc_test_gen.rs
@@ -174,7 +174,7 @@ pub extern "C" fn {kunit_name}(__kunit_test: *mut ::kernel::bindings::kunit) {{
     macro_rules! assert {{
         ($cond:expr $(,)?) => {{{{
             ::kernel::kunit_assert!(
-                "{kunit_name}", "{real_path}", __DOCTEST_ANCHOR - {line}, $cond
+                "{kunit_name}", c"{real_path}", __DOCTEST_ANCHOR - {line}, $cond
             );
         }}}}
     }}
@@ -184,7 +184,7 @@ macro_rules! assert {{
     macro_rules! assert_eq {{
         ($left:expr, $right:expr $(,)?) => {{{{
             ::kernel::kunit_assert_eq!(
-                "{kunit_name}", "{real_path}", __DOCTEST_ANCHOR - {line}, $left, $right
+                "{kunit_name}", c"{real_path}", __DOCTEST_ANCHOR - {line}, $left, $right
             );
         }}}}
     }}

---
base-commit: 8f0b4cce4481fb22653697cced8d0d04027cb1e8
change-id: 20251222-cstr-kunit-be68dd8cfbd5

Best regards,
--  
Tamir Duberstein <tamird@gmail.com>
Re: [PATCH] rust: kunit: replace `kernel::c_str!` with C-Strings
Posted by Miguel Ojeda 2 weeks, 5 days ago
On Mon, Dec 22, 2025 at 1:28 PM Tamir Duberstein <tamird@kernel.org> wrote:
>
> From: Tamir Duberstein <tamird@gmail.com>
>
> C-String literals were added in Rust 1.77. Replace instances of
> `kernel::c_str!` with C-String literals where possible.
>
> Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> Reviewed-by: Benno Lossin <lossin@kernel.org>
> Signed-off-by: Tamir Duberstein <tamird@gmail.com>

Applied to `rust-next` -- thanks everyone!

Cheers,
Miguel
Re: [PATCH] rust: kunit: replace `kernel::c_str!` with C-Strings
Posted by David Gow 1 month ago
On Mon, 22 Dec 2025 at 20:28, Tamir Duberstein <tamird@kernel.org> wrote:
>
> From: Tamir Duberstein <tamird@gmail.com>
>
> C-String literals were added in Rust 1.77. Replace instances of
> `kernel::c_str!` with C-String literals where possible.
>
> Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> Reviewed-by: Benno Lossin <lossin@kernel.org>
> Signed-off-by: Tamir Duberstein <tamird@gmail.com>
> ---

Sorry for the delay: this looks good to me.

Reviewed-by: David Gow <davidgow@google.com>

Cheers,
-- David


>  rust/kernel/kunit.rs        | 11 ++++-------
>  rust/macros/kunit.rs        | 10 +++++-----
>  scripts/rustdoc_test_gen.rs |  4 ++--
>  3 files changed, 11 insertions(+), 14 deletions(-)
>
> diff --git a/rust/kernel/kunit.rs b/rust/kernel/kunit.rs
> index 79436509dd73..21aef6c97325 100644
> --- a/rust/kernel/kunit.rs
> +++ b/rust/kernel/kunit.rs
> @@ -9,9 +9,6 @@
>  use crate::fmt;
>  use crate::prelude::*;
>
> -#[cfg(CONFIG_PRINTK)]
> -use crate::c_str;
> -
>  /// Prints a KUnit error-level message.
>  ///
>  /// Public but hidden since it should only be used from KUnit generated code.
> @@ -22,7 +19,7 @@ pub fn err(args: fmt::Arguments<'_>) {
>      #[cfg(CONFIG_PRINTK)]
>      unsafe {
>          bindings::_printk(
> -            c_str!("\x013%pA").as_char_ptr(),
> +            c"\x013%pA".as_char_ptr(),
>              core::ptr::from_ref(&args).cast::<c_void>(),
>          );
>      }
> @@ -38,7 +35,7 @@ pub fn info(args: fmt::Arguments<'_>) {
>      #[cfg(CONFIG_PRINTK)]
>      unsafe {
>          bindings::_printk(
> -            c_str!("\x016%pA").as_char_ptr(),
> +            c"\x016%pA".as_char_ptr(),
>              core::ptr::from_ref(&args).cast::<c_void>(),
>          );
>      }
> @@ -60,7 +57,7 @@ macro_rules! kunit_assert {
>                  break 'out;
>              }
>
> -            static FILE: &'static $crate::str::CStr = $crate::c_str!($file);
> +            static FILE: &'static $crate::str::CStr = $file;
>              static LINE: i32 = ::core::line!() as i32 - $diff;
>              static CONDITION: &'static $crate::str::CStr = $crate::c_str!(stringify!($condition));
>
> @@ -253,7 +250,7 @@ pub const fn kunit_case_null() -> kernel::bindings::kunit_case {
>  /// }
>  ///
>  /// static mut KUNIT_TEST_CASES: [kernel::bindings::kunit_case; 2] = [
> -///     kernel::kunit::kunit_case(kernel::c_str!("name"), test_fn),
> +///     kernel::kunit::kunit_case(c"name", test_fn),
>  ///     kernel::kunit::kunit_case_null(),
>  /// ];
>  /// kernel::kunit_unsafe_test_suite!(suite_name, KUNIT_TEST_CASES);
> diff --git a/rust/macros/kunit.rs b/rust/macros/kunit.rs
> index b395bb053695..3d7724b35c0f 100644
> --- a/rust/macros/kunit.rs
> +++ b/rust/macros/kunit.rs
> @@ -102,8 +102,8 @@ pub(crate) fn kunit_tests(attr: TokenStream, ts: TokenStream) -> TokenStream {
>      // unsafe extern "C" fn kunit_rust_wrapper_bar(_test: *mut ::kernel::bindings::kunit) { bar(); }
>      //
>      // static mut TEST_CASES: [::kernel::bindings::kunit_case; 3] = [
> -    //     ::kernel::kunit::kunit_case(::kernel::c_str!("foo"), kunit_rust_wrapper_foo),
> -    //     ::kernel::kunit::kunit_case(::kernel::c_str!("bar"), kunit_rust_wrapper_bar),
> +    //     ::kernel::kunit::kunit_case(c"foo", kunit_rust_wrapper_foo),
> +    //     ::kernel::kunit::kunit_case(c"bar", kunit_rust_wrapper_bar),
>      //     ::kernel::kunit::kunit_case_null(),
>      // ];
>      //
> @@ -133,7 +133,7 @@ pub(crate) fn kunit_tests(attr: TokenStream, ts: TokenStream) -> TokenStream {
>          writeln!(kunit_macros, "{kunit_wrapper}").unwrap();
>          writeln!(
>              test_cases,
> -            "    ::kernel::kunit::kunit_case(::kernel::c_str!(\"{test}\"), {kunit_wrapper_fn_name}),"
> +            "    ::kernel::kunit::kunit_case(c\"{test}\", {kunit_wrapper_fn_name}),"
>          )
>          .unwrap();
>          writeln!(
> @@ -143,7 +143,7 @@ pub(crate) fn kunit_tests(attr: TokenStream, ts: TokenStream) -> TokenStream {
>  #[allow(unused)]
>  macro_rules! assert {{
>      ($cond:expr $(,)?) => {{{{
> -        kernel::kunit_assert!("{test}", "{path}", 0, $cond);
> +        kernel::kunit_assert!("{test}", c"{path}", 0, $cond);
>      }}}}
>  }}
>
> @@ -151,7 +151,7 @@ macro_rules! assert {{
>  #[allow(unused)]
>  macro_rules! assert_eq {{
>      ($left:expr, $right:expr $(,)?) => {{{{
> -        kernel::kunit_assert_eq!("{test}", "{path}", 0, $left, $right);
> +        kernel::kunit_assert_eq!("{test}", c"{path}", 0, $left, $right);
>      }}}}
>  }}
>          "#
> diff --git a/scripts/rustdoc_test_gen.rs b/scripts/rustdoc_test_gen.rs
> index be0561049660..967064ebd391 100644
> --- a/scripts/rustdoc_test_gen.rs
> +++ b/scripts/rustdoc_test_gen.rs
> @@ -174,7 +174,7 @@ pub extern "C" fn {kunit_name}(__kunit_test: *mut ::kernel::bindings::kunit) {{
>      macro_rules! assert {{
>          ($cond:expr $(,)?) => {{{{
>              ::kernel::kunit_assert!(
> -                "{kunit_name}", "{real_path}", __DOCTEST_ANCHOR - {line}, $cond
> +                "{kunit_name}", c"{real_path}", __DOCTEST_ANCHOR - {line}, $cond
>              );
>          }}}}
>      }}
> @@ -184,7 +184,7 @@ macro_rules! assert {{
>      macro_rules! assert_eq {{
>          ($left:expr, $right:expr $(,)?) => {{{{
>              ::kernel::kunit_assert_eq!(
> -                "{kunit_name}", "{real_path}", __DOCTEST_ANCHOR - {line}, $left, $right
> +                "{kunit_name}", c"{real_path}", __DOCTEST_ANCHOR - {line}, $left, $right
>              );
>          }}}}
>      }}
>
> ---
> base-commit: 8f0b4cce4481fb22653697cced8d0d04027cb1e8
> change-id: 20251222-cstr-kunit-be68dd8cfbd5
>
> Best regards,
> --
> Tamir Duberstein <tamird@gmail.com>
>
Re: [PATCH] rust: kunit: replace `kernel::c_str!` with C-Strings
Posted by Miguel Ojeda 1 month, 2 weeks ago
On Mon, Dec 22, 2025 at 1:28 PM Tamir Duberstein <tamird@kernel.org> wrote:
>
> From: Tamir Duberstein <tamird@gmail.com>
>
> C-String literals were added in Rust 1.77. Replace instances of
> `kernel::c_str!` with C-String literals where possible.
>
> Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> Reviewed-by: Benno Lossin <lossin@kernel.org>
> Signed-off-by: Tamir Duberstein <tamird@gmail.com>

Thanks for these (and all the others you just sent) cleanups.

David et al.: I can pick this up if you want with an Acked-by -- thanks!

Cheers,
Miguel
Re: [PATCH] rust: kunit: replace `kernel::c_str!` with C-Strings
Posted by Tamir Duberstein 1 month ago
On Mon, Dec 22, 2025 at 7:59 AM Miguel Ojeda
<miguel.ojeda.sandonis@gmail.com> wrote:
>
> On Mon, Dec 22, 2025 at 1:28 PM Tamir Duberstein <tamird@kernel.org> wrote:
> >
> > From: Tamir Duberstein <tamird@gmail.com>
> >
> > C-String literals were added in Rust 1.77. Replace instances of
> > `kernel::c_str!` with C-String literals where possible.
> >
> > Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> > Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> > Reviewed-by: Benno Lossin <lossin@kernel.org>
> > Signed-off-by: Tamir Duberstein <tamird@gmail.com>
>
> Thanks for these (and all the others you just sent) cleanups.
>
> David et al.: I can pick this up if you want with an Acked-by -- thanks!
>
> Cheers,
> Miguel

@David: could you please have a look? Thanks!