Skip to content

Commit f411b7e

Browse files
tamirdojeda
authored andcommitted
rust: kernel: remove fmt!, fix clippy::uninlined-format-args
Rather than export a macro that delegates to `core::format_args`, simply re-export `core::format_args` as `fmt` from the prelude. This exposes clippy warnings which were previously obscured by this macro, such as: warning: variables can be used directly in the `format!` string --> ../drivers/cpufreq/rcpufreq_dt.rs:21:43 | 21 | let prop_name = CString::try_from_fmt(fmt!("{}-supply", name)).ok()?; | ^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#uninlined_format_args = note: `-W clippy::uninlined-format-args` implied by `-W clippy::all` = help: to override `-W clippy::all` add `#[allow(clippy::uninlined_format_args)]` help: change this to | 21 - let prop_name = CString::try_from_fmt(fmt!("{}-supply", name)).ok()?; 21 + let prop_name = CString::try_from_fmt(fmt!("{name}-supply")).ok()?; | Thus fix them in the same commit. This could possibly be fixed in two stages, but the diff is small enough (outside of kernel/str.rs) that I hope it can be taken in a single commit. Signed-off-by: Tamir Duberstein <tamird@gmail.com> Reviewed-by: Benno Lossin <lossin@kernel.org> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Acked-by: Danilo Krummrich <dakr@kernel.org> Link: https://lore.kernel.org/r/20250704-core-cstr-prepare-v1-1-a91524037783@gmail.com Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 2254991 commit f411b7e

5 files changed

Lines changed: 20 additions & 26 deletions

File tree

drivers/cpufreq/rcpufreq_dt.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use kernel::{
99
cpumask::CpumaskVar,
1010
device::{Core, Device},
1111
error::code::*,
12-
fmt,
1312
macros::vtable,
1413
module_platform_driver, of, opp, platform,
1514
prelude::*,
@@ -19,7 +18,7 @@ use kernel::{
1918

2019
/// Finds exact supply name from the OF node.
2120
fn find_supply_name_exact(dev: &Device, name: &str) -> Option<CString> {
22-
let prop_name = CString::try_from_fmt(fmt!("{}-supply", name)).ok()?;
21+
let prop_name = CString::try_from_fmt(fmt!("{name}-supply")).ok()?;
2322
dev.property_present(&prop_name)
2423
.then(|| CString::try_from_fmt(fmt!("{name}")).ok())
2524
.flatten()

drivers/gpu/nova-core/firmware.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ pub(crate) struct Firmware {
2424

2525
impl Firmware {
2626
pub(crate) fn new(dev: &device::Device, chipset: Chipset, ver: &str) -> Result<Firmware> {
27-
let mut chip_name = CString::try_from_fmt(fmt!("{}", chipset))?;
27+
let mut chip_name = CString::try_from_fmt(fmt!("{chipset}"))?;
2828
chip_name.make_ascii_lowercase();
29+
let chip_name = &*chip_name;
2930

3031
let request = |name_| {
31-
CString::try_from_fmt(fmt!("nvidia/{}/gsp/{}-{}.bin", &*chip_name, name_, ver))
32+
CString::try_from_fmt(fmt!("nvidia/{chip_name}/gsp/{name_}-{ver}.bin"))
3233
.and_then(|path| firmware::Firmware::request(&path, dev))
3334
};
3435

rust/kernel/opp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ impl Drop for ConfigToken {
345345
/// impl ConfigOps for Driver {}
346346
///
347347
/// fn configure(dev: &ARef<Device>) -> Result<ConfigToken> {
348-
/// let name = CString::try_from_fmt(fmt!("{}", "slow"))?;
348+
/// let name = CString::try_from_fmt(fmt!("slow"))?;
349349
///
350350
/// // The OPP configuration is cleared once the [`ConfigToken`] goes out of scope.
351351
/// Config::<Driver>::new()

rust/kernel/prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ pub use super::{build_assert, build_error};
3131
// `super::std_vendor` is hidden, which makes the macro inline for some reason.
3232
#[doc(no_inline)]
3333
pub use super::dbg;
34-
pub use super::fmt;
3534
pub use super::{dev_alert, dev_crit, dev_dbg, dev_emerg, dev_err, dev_info, dev_notice, dev_warn};
3635
pub use super::{pr_alert, pr_crit, pr_debug, pr_emerg, pr_err, pr_info, pr_notice, pr_warn};
36+
pub use core::format_args as fmt;
3737

3838
pub use super::{try_init, try_pin_init};
3939

rust/kernel/str.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ impl fmt::Display for BStr {
5454
/// Formats printable ASCII characters, escaping the rest.
5555
///
5656
/// ```
57-
/// # use kernel::{fmt, b_str, str::{BStr, CString}};
57+
/// # use kernel::{prelude::fmt, b_str, str::{BStr, CString}};
5858
/// let ascii = b_str!("Hello, BStr!");
59-
/// let s = CString::try_from_fmt(fmt!("{}", ascii))?;
59+
/// let s = CString::try_from_fmt(fmt!("{ascii}"))?;
6060
/// assert_eq!(s.as_bytes(), "Hello, BStr!".as_bytes());
6161
///
6262
/// let non_ascii = b_str!("🦀");
63-
/// let s = CString::try_from_fmt(fmt!("{}", non_ascii))?;
63+
/// let s = CString::try_from_fmt(fmt!("{non_ascii}"))?;
6464
/// assert_eq!(s.as_bytes(), "\\xf0\\x9f\\xa6\\x80".as_bytes());
6565
/// # Ok::<(), kernel::error::Error>(())
6666
/// ```
@@ -85,14 +85,14 @@ impl fmt::Debug for BStr {
8585
/// escaping the rest.
8686
///
8787
/// ```
88-
/// # use kernel::{fmt, b_str, str::{BStr, CString}};
88+
/// # use kernel::{prelude::fmt, b_str, str::{BStr, CString}};
8989
/// // Embedded double quotes are escaped.
9090
/// let ascii = b_str!("Hello, \"BStr\"!");
91-
/// let s = CString::try_from_fmt(fmt!("{:?}", ascii))?;
91+
/// let s = CString::try_from_fmt(fmt!("{ascii:?}"))?;
9292
/// assert_eq!(s.as_bytes(), "\"Hello, \\\"BStr\\\"!\"".as_bytes());
9393
///
9494
/// let non_ascii = b_str!("😺");
95-
/// let s = CString::try_from_fmt(fmt!("{:?}", non_ascii))?;
95+
/// let s = CString::try_from_fmt(fmt!("{non_ascii:?}"))?;
9696
/// assert_eq!(s.as_bytes(), "\"\\xf0\\x9f\\x98\\xba\"".as_bytes());
9797
/// # Ok::<(), kernel::error::Error>(())
9898
/// ```
@@ -429,15 +429,15 @@ impl fmt::Display for CStr {
429429
///
430430
/// ```
431431
/// # use kernel::c_str;
432-
/// # use kernel::fmt;
432+
/// # use kernel::prelude::fmt;
433433
/// # use kernel::str::CStr;
434434
/// # use kernel::str::CString;
435435
/// let penguin = c_str!("🐧");
436-
/// let s = CString::try_from_fmt(fmt!("{}", penguin))?;
436+
/// let s = CString::try_from_fmt(fmt!("{penguin}"))?;
437437
/// assert_eq!(s.as_bytes_with_nul(), "\\xf0\\x9f\\x90\\xa7\0".as_bytes());
438438
///
439439
/// let ascii = c_str!("so \"cool\"");
440-
/// let s = CString::try_from_fmt(fmt!("{}", ascii))?;
440+
/// let s = CString::try_from_fmt(fmt!("{ascii}"))?;
441441
/// assert_eq!(s.as_bytes_with_nul(), "so \"cool\"\0".as_bytes());
442442
/// # Ok::<(), kernel::error::Error>(())
443443
/// ```
@@ -459,16 +459,16 @@ impl fmt::Debug for CStr {
459459
///
460460
/// ```
461461
/// # use kernel::c_str;
462-
/// # use kernel::fmt;
462+
/// # use kernel::prelude::fmt;
463463
/// # use kernel::str::CStr;
464464
/// # use kernel::str::CString;
465465
/// let penguin = c_str!("🐧");
466-
/// let s = CString::try_from_fmt(fmt!("{:?}", penguin))?;
466+
/// let s = CString::try_from_fmt(fmt!("{penguin:?}"))?;
467467
/// assert_eq!(s.as_bytes_with_nul(), "\"\\xf0\\x9f\\x90\\xa7\"\0".as_bytes());
468468
///
469469
/// // Embedded double quotes are escaped.
470470
/// let ascii = c_str!("so \"cool\"");
471-
/// let s = CString::try_from_fmt(fmt!("{:?}", ascii))?;
471+
/// let s = CString::try_from_fmt(fmt!("{ascii:?}"))?;
472472
/// assert_eq!(s.as_bytes_with_nul(), "\"so \\\"cool\\\"\"\0".as_bytes());
473473
/// # Ok::<(), kernel::error::Error>(())
474474
/// ```
@@ -578,7 +578,7 @@ mod tests {
578578

579579
macro_rules! format {
580580
($($f:tt)*) => ({
581-
CString::try_from_fmt(::kernel::fmt!($($f)*))?.to_str()?
581+
CString::try_from_fmt(fmt!($($f)*))?.to_str()?
582582
})
583583
}
584584

@@ -840,7 +840,7 @@ impl fmt::Write for Formatter {
840840
/// # Examples
841841
///
842842
/// ```
843-
/// use kernel::{str::CString, fmt};
843+
/// use kernel::{str::CString, prelude::fmt};
844844
///
845845
/// let s = CString::try_from_fmt(fmt!("{}{}{}", "abc", 10, 20))?;
846846
/// assert_eq!(s.as_bytes_with_nul(), "abc1020\0".as_bytes());
@@ -930,9 +930,3 @@ impl fmt::Debug for CString {
930930
fmt::Debug::fmt(&**self, f)
931931
}
932932
}
933-
934-
/// A convenience alias for [`core::format_args`].
935-
#[macro_export]
936-
macro_rules! fmt {
937-
($($f:tt)*) => ( ::core::format_args!($($f)*) )
938-
}

0 commit comments

Comments
 (0)