Skip to contentSkip to conversation

rust: kunit: use C-string literals to clean warning

PATCH Applied Miguel Ojeda 5 participants 7 messages Sep 27, 2024 – Oct 3, 2024

Subsystems (2)
lore
Share & tools
lore

Miguel Ojeda

author maintainer
#0 lore raw

PATCH 1/1 rust: kunit: use C-string literals to clean warning

Starting with upstream Rust commit a5e3a3f9b6bd ("move manual_c_str_literals to complexity"), to be released in Rust 1.83.0 [1], Clippy now warns on manual_c_str_literals by default, e.g.:

    error: manually constructing a nul-terminated string
      --> rust/kernel/kunit.rs:21:13
       |
    21 |             b"\x013%pA\0".as_ptr() as _,
       |             ^^^^^^^^^^^^^ help: use a `c""` literal: `c"\x013%pA"`
       |
       = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_c_str_literals
       = note: `-D clippy::manual-c-str-literals` implied by `-D warnings`
       = help: to override `-D warnings` add `#[allow(clippy::manual_c_str_literals)]`

Apply the suggestion to clean up the warnings.

---
The cast could also be cleaned up, but that is already done in Gary's FFI series, so I didn't include another patch.

C-string literals are available since Rust 1.77.0, which is higher than our minimum version, but LTS kernels do not have Rust 1.77.0. But perhaps for these is not a big deal, and we could also enable feature(c_str_literals) instead.

 rust/kernel/kunit.rs | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
rust/kernel/kunit.rs +2 -2
index 0ba77276ae7e..824da0e9738a 100644
@@ -18,7 +18,7 @@ pub fn err(args: fmt::Arguments<'_>) {
     #[cfg(CONFIG_PRINTK)]
     unsafe {
         bindings::_printk(
-            b"\x013%pA\0".as_ptr() as _,
+            c"\x013%pA".as_ptr() as _,
             &args as *const _ as *const c_void,
         );
     }
@@ -34,7 +34,7 @@ pub fn info(args: fmt::Arguments<'_>) {
     #[cfg(CONFIG_PRINTK)]
     unsafe {
         bindings::_printk(
-            b"\x016%pA\0".as_ptr() as _,
+            c"\x016%pA".as_ptr() as _,
             &args as *const _ as *const c_void,
         );
     }

base-commit: 570172569238c66a482ec3eb5d766cc9cf255f69 --
2.46.2

Reviewed-by on v1 1/1
#1 lore raw
Quoted 14 lines · Miguel Ojeda

Starting with upstream Rust commit a5e3a3f9b6bd ("move manual_c_str_literals to complexity"), to be released in Rust 1.83.0 [1], Clippy now warns on manual_c_str_literals by default, e.g.:

    error: manually constructing a nul-terminated string
      --> rust/kernel/kunit.rs:21:13
       |
    21 |             b"\x013%pA\0".as_ptr() as _,
       |             ^^^^^^^^^^^^^ help: use a `c""` literal: `c"\x013%pA"`
       |
       = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_c_str_literals
       = note: `-D clippy::manual-c-str-literals` implied by `-D warnings`
       = help: to override `-D warnings` add `#[allow(clippy::manual_c_str_literals)]`
Quoted 4 lines

Apply the suggestion to clean up the warnings.

Quoted 5 lines

---
The cast could also be cleaned up, but that is already done in Gary's FFI series, so I didn't include another patch.

C-string literals are available since Rust 1.77.0, which is higher than

I thought our minimal version is 1.78.0, or am I misunderstanding the wording here (I interpret higher as "1.77 > min-version").

---
Cheers,
Benno

Quoted 33 lines

our minimum version, but LTS kernels do not have Rust 1.77.0. But perhaps for these is not a big deal, and we could also enable feature(c_str_literals) instead.

 rust/kernel/kunit.rs | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
rust/kernel/kunit.rs +2 -2
index 0ba77276ae7e..824da0e9738a 100644
@@ -18,7 +18,7 @@ pub fn err(args: fmt::Arguments<'_>) {
     #[cfg(CONFIG_PRINTK)]
     unsafe {
         bindings::_printk(
-            b"\x013%pA\0".as_ptr() as _,
+            c"\x013%pA".as_ptr() as _,
             &args as *const _ as *const c_void,
         );
     }
@@ -34,7 +34,7 @@ pub fn info(args: fmt::Arguments<'_>) {
     #[cfg(CONFIG_PRINTK)]
     unsafe {
         bindings::_printk(
-            b"\x016%pA\0".as_ptr() as _,
+            c"\x016%pA".as_ptr() as _,
             &args as *const _ as *const c_void,
         );
     }

base-commit: 570172569238c66a482ec3eb5d766cc9cf255f69 --
2.46.2

#2 lore raw
Quoted 3 lines · Benno Lossin

I thought our minimal version is 1.78.0, or am I misunderstanding the wording here (I interpret higher as "1.77 > min-version").

Yeah, sorry, I meant that our minimum is higher and thus we can use the feature in mainline, but not in LTSs -- rewording artifact :)

Cheers,
Miguel

reviewer
#3 lore raw
In reply to Miguel Ojeda
Quoted 14 lines · On Fri, 27 Sep 2024 18:44:14 +0200 Miguel Ojeda <[email protected]> wrote:

Starting with upstream Rust commit a5e3a3f9b6bd ("move manual_c_str_literals to complexity"), to be released in Rust 1.83.0 [1], Clippy now warns on manual_c_str_literals by default, e.g.:

    error: manually constructing a nul-terminated string
      --> rust/kernel/kunit.rs:21:13  
       |
    21 |             b"\x013%pA\0".as_ptr() as _,
       |             ^^^^^^^^^^^^^ help: use a `c""` literal: `c"\x013%pA"`
       |
       = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_c_str_literals
       = note: `-D clippy::manual-c-str-literals` implied by `-D warnings`
       = help: to override `-D warnings` add `#[allow(clippy::manual_c_str_literals)]`
Quoted 7 lines

Apply the suggestion to clean up the warnings.

---
The cast could also be cleaned up, but that is already done in Gary's FFI series, so I didn't include another patch.

Note that the cast actually need to be re-introduced instead of cleaned-up -- c"".as_ptr() returns core::ffi::c_char which might be signed, while after my series *const u8 is expected by printf.

An alternative is to use c_str!(...).as_char_ptr() which will guarantee the type is what we expected.

Best,
Gary

Quoted 35 lines

C-string literals are available since Rust 1.77.0, which is higher than our minimum version, but LTS kernels do not have Rust 1.77.0. But perhaps for these is not a big deal, and we could also enable feature(c_str_literals) instead.

 rust/kernel/kunit.rs | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
rust/kernel/kunit.rs +2 -2
index 0ba77276ae7e..824da0e9738a 100644
@@ -18,7 +18,7 @@ pub fn err(args: fmt::Arguments<'_>) {
     #[cfg(CONFIG_PRINTK)]
     unsafe {
         bindings::_printk(
-            b"\x013%pA\0".as_ptr() as _,
+            c"\x013%pA".as_ptr() as _,
             &args as *const _ as *const c_void,
         );
     }
@@ -34,7 +34,7 @@ pub fn info(args: fmt::Arguments<'_>) {
     #[cfg(CONFIG_PRINTK)]
     unsafe {
         bindings::_printk(
-            b"\x016%pA\0".as_ptr() as _,
+            c"\x016%pA".as_ptr() as _,
             &args as *const _ as *const c_void,
         );
     }

base-commit: 570172569238c66a482ec3eb5d766cc9cf255f69 --
2.46.2

reviewer Reviewed-by on v1 1/1
#5 lore raw
In reply to Miguel Ojeda
Quoted 15 lines · Miguel Ojeda

Starting with upstream Rust commit a5e3a3f9b6bd ("move manual_c_str_literals to complexity"), to be released in Rust 1.83.0 [1], Clippy now warns on manual_c_str_literals by default, e.g.:

    error: manually constructing a nul-terminated string
      --> rust/kernel/kunit.rs:21:13
       |
    21 |             b"\x013%pA\0".as_ptr() as _,
       |             ^^^^^^^^^^^^^ help: use a `c""` literal: `c"\x013%pA"`
       |
       = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_c_str_literals
       = note: `-D clippy::manual-c-str-literals` implied by `-D warnings`
       = help: to override `-D warnings` add `#[allow(clippy::manual_c_str_literals)]`
Quoted 4 lines

Apply the suggestion to clean up the warnings.

Applied
#6 lore raw
In reply to Miguel Ojeda
Quoted 15 lines · Miguel Ojeda

Starting with upstream Rust commit a5e3a3f9b6bd ("move manual_c_str_literals to complexity"), to be released in Rust 1.83.0 [1], Clippy now warns on manual_c_str_literals by default, e.g.:

    error: manually constructing a nul-terminated string
      --> rust/kernel/kunit.rs:21:13
       |
    21 |             b"\x013%pA\0".as_ptr() as _,
       |             ^^^^^^^^^^^^^ help: use a `c""` literal: `c"\x013%pA"`
       |
       = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_c_str_literals
       = note: `-D clippy::manual-c-str-literals` implied by `-D warnings`
       = help: to override `-D warnings` add `#[allow(clippy::manual_c_str_literals)]`
Quoted 4 lines

Apply the suggestion to clean up the warnings.

Applied to rust-fixes -- thanks everyone!

Cheers,
Miguel

#4 lore raw
In reply to Gary Guo
Quoted 4 lines · Gary Guo

Note that the cast actually need to be re-introduced instead of cleaned-up -- c"".as_ptr() returns core::ffi::c_char which might be signed, while after my series *const u8 is expected by printf.

Ah, right, in your series bindgen will point to the new one while the method will return the core::ffi one which might be still i8 in some targets.

It would be nice to get a way to modify the target for u8, i.e. -Cunsigned-char or a "target modifier" or similar.

Added an entry with some links in our rustc list at
https://github.com/Rust-for-Linux/linux/issues/355.

Cheers,
Miguel