Skip to content

Commit 6c37b68

Browse files
tamirdojeda
authored andcommitted
rust: kunit: replace kernel::c_str! with C-Strings
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> Reviewed-by: David Gow <davidgow@google.com> Link: https://patch.msgid.link/20251222-cstr-kunit-v1-1-39d999672f35@gmail.com Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 32d61c5 commit 6c37b68

File tree

3 files changed

+11
-14
lines changed

3 files changed

+11
-14
lines changed

rust/kernel/kunit.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
use crate::fmt;
1010
use crate::prelude::*;
1111

12-
#[cfg(CONFIG_PRINTK)]
13-
use crate::c_str;
14-
1512
/// Prints a KUnit error-level message.
1613
///
1714
/// Public but hidden since it should only be used from KUnit generated code.
@@ -22,7 +19,7 @@ pub fn err(args: fmt::Arguments<'_>) {
2219
#[cfg(CONFIG_PRINTK)]
2320
unsafe {
2421
bindings::_printk(
25-
c_str!("\x013%pA").as_char_ptr(),
22+
c"\x013%pA".as_char_ptr(),
2623
core::ptr::from_ref(&args).cast::<c_void>(),
2724
);
2825
}
@@ -38,7 +35,7 @@ pub fn info(args: fmt::Arguments<'_>) {
3835
#[cfg(CONFIG_PRINTK)]
3936
unsafe {
4037
bindings::_printk(
41-
c_str!("\x016%pA").as_char_ptr(),
38+
c"\x016%pA".as_char_ptr(),
4239
core::ptr::from_ref(&args).cast::<c_void>(),
4340
);
4441
}
@@ -60,7 +57,7 @@ macro_rules! kunit_assert {
6057
break 'out;
6158
}
6259

63-
static FILE: &'static $crate::str::CStr = $crate::c_str!($file);
60+
static FILE: &'static $crate::str::CStr = $file;
6461
static LINE: i32 = ::core::line!() as i32 - $diff;
6562
static CONDITION: &'static $crate::str::CStr = $crate::c_str!(stringify!($condition));
6663

@@ -253,7 +250,7 @@ pub const fn kunit_case_null() -> kernel::bindings::kunit_case {
253250
/// }
254251
///
255252
/// static mut KUNIT_TEST_CASES: [kernel::bindings::kunit_case; 2] = [
256-
/// kernel::kunit::kunit_case(kernel::c_str!("name"), test_fn),
253+
/// kernel::kunit::kunit_case(c"name", test_fn),
257254
/// kernel::kunit::kunit_case_null(),
258255
/// ];
259256
/// kernel::kunit_unsafe_test_suite!(suite_name, KUNIT_TEST_CASES);

rust/macros/kunit.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ pub(crate) fn kunit_tests(attr: TokenStream, ts: TokenStream) -> TokenStream {
102102
// unsafe extern "C" fn kunit_rust_wrapper_bar(_test: *mut ::kernel::bindings::kunit) { bar(); }
103103
//
104104
// static mut TEST_CASES: [::kernel::bindings::kunit_case; 3] = [
105-
// ::kernel::kunit::kunit_case(::kernel::c_str!("foo"), kunit_rust_wrapper_foo),
106-
// ::kernel::kunit::kunit_case(::kernel::c_str!("bar"), kunit_rust_wrapper_bar),
105+
// ::kernel::kunit::kunit_case(c"foo", kunit_rust_wrapper_foo),
106+
// ::kernel::kunit::kunit_case(c"bar", kunit_rust_wrapper_bar),
107107
// ::kernel::kunit::kunit_case_null(),
108108
// ];
109109
//
@@ -133,7 +133,7 @@ pub(crate) fn kunit_tests(attr: TokenStream, ts: TokenStream) -> TokenStream {
133133
writeln!(kunit_macros, "{kunit_wrapper}").unwrap();
134134
writeln!(
135135
test_cases,
136-
" ::kernel::kunit::kunit_case(::kernel::c_str!(\"{test}\"), {kunit_wrapper_fn_name}),"
136+
" ::kernel::kunit::kunit_case(c\"{test}\", {kunit_wrapper_fn_name}),"
137137
)
138138
.unwrap();
139139
writeln!(
@@ -143,15 +143,15 @@ pub(crate) fn kunit_tests(attr: TokenStream, ts: TokenStream) -> TokenStream {
143143
#[allow(unused)]
144144
macro_rules! assert {{
145145
($cond:expr $(,)?) => {{{{
146-
kernel::kunit_assert!("{test}", "{path}", 0, $cond);
146+
kernel::kunit_assert!("{test}", c"{path}", 0, $cond);
147147
}}}}
148148
}}
149149
150150
/// Overrides the usual [`assert_eq!`] macro with one that calls KUnit instead.
151151
#[allow(unused)]
152152
macro_rules! assert_eq {{
153153
($left:expr, $right:expr $(,)?) => {{{{
154-
kernel::kunit_assert_eq!("{test}", "{path}", 0, $left, $right);
154+
kernel::kunit_assert_eq!("{test}", c"{path}", 0, $left, $right);
155155
}}}}
156156
}}
157157
"#

scripts/rustdoc_test_gen.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ pub extern "C" fn {kunit_name}(__kunit_test: *mut ::kernel::bindings::kunit) {{
174174
macro_rules! assert {{
175175
($cond:expr $(,)?) => {{{{
176176
::kernel::kunit_assert!(
177-
"{kunit_name}", "{real_path}", __DOCTEST_ANCHOR - {line}, $cond
177+
"{kunit_name}", c"{real_path}", __DOCTEST_ANCHOR - {line}, $cond
178178
);
179179
}}}}
180180
}}
@@ -184,7 +184,7 @@ pub extern "C" fn {kunit_name}(__kunit_test: *mut ::kernel::bindings::kunit) {{
184184
macro_rules! assert_eq {{
185185
($left:expr, $right:expr $(,)?) => {{{{
186186
::kernel::kunit_assert_eq!(
187-
"{kunit_name}", "{real_path}", __DOCTEST_ANCHOR - {line}, $left, $right
187+
"{kunit_name}", c"{real_path}", __DOCTEST_ANCHOR - {line}, $left, $right
188188
);
189189
}}}}
190190
}}

0 commit comments

Comments
 (0)