From b766d4a53c2e52a155c38c143b724adbc6578719 Mon Sep 17 00:00:00 2001 From: Marc Hartmayer Date: Tue, 28 Oct 2025 15:29:16 +0100 Subject: [PATCH] rust: Apply clippy fixes to format strings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Makes the code easier to read. Reviewed-by: Steffen Eiden Signed-off-by: Marc Hartmayer Signed-off-by: Jan Höppner --- rust/cpacfinfo/src/msa.rs | 2 +- rust/pv_core/src/apdevice.rs | 15 +++++---------- rust/pvapconfig/src/ap.rs | 8 +++----- rust/pvapconfig/src/config.rs | 12 +++++------- rust/pvapconfig/src/main.rs | 7 +++---- rust/pvapconfig/src/uv.rs | 2 +- rust/pvattest/src/additional.rs | 6 +++--- rust/pvattest/src/cmd/check/host_key.rs | 3 +-- rust/pvimg/examples/tamper_pvimg/main.rs | 5 +++-- rust/pvimg/src/pv_utils/layout.rs | 2 +- rust/pvimg/src/pv_utils/se_hdr/flags.rs | 6 +++--- rust/pvimg/src/pv_utils/serializing.rs | 2 +- rust/utils/src/hexslice.rs | 2 +- 13 files changed, 31 insertions(+), 41 deletions(-) diff --git a/rust/cpacfinfo/src/msa.rs b/rust/cpacfinfo/src/msa.rs index 4e9bad1e..8b9a7147 100644 --- a/rust/cpacfinfo/src/msa.rs +++ b/rust/cpacfinfo/src/msa.rs @@ -162,7 +162,7 @@ impl Display for MsaLevel { fn fmt(&self, f: &mut Formatter) -> Result { write!(f, "{} ", self.msa_level)?; match self.stfle_bit { - Some(bit) => write!(f, "STFLE bit [ {:>3} ] : ", bit)?, + Some(bit) => write!(f, "STFLE bit [ {bit:>3} ] : ")?, None => write!(f, " : ")?, } match self.enabled { diff --git a/rust/pv_core/src/apdevice.rs b/rust/pv_core/src/apdevice.rs index 0c957db4..ec9f7834 100644 --- a/rust/pv_core/src/apdevice.rs +++ b/rust/pv_core/src/apdevice.rs @@ -304,8 +304,7 @@ pub enum BindState { /// an error string. Does NOT print any error messages. pub fn get_apqn_bind_state(card: u32, dom: u32) -> Result { let path = format!( - "{}/card{:02x}/{:02x}.{:04x}/se_bind", - PATH_SYS_DEVICES_AP, card, card, dom + "{PATH_SYS_DEVICES_AP}/card{card:02x}/{card:02x}.{dom:04x}/se_bind" ); let state_str = read_file_string(path, "se_bind attribute")?; let state = state_str.trim(); @@ -332,8 +331,7 @@ pub fn get_apqn_bind_state(card: u32, dom: u32) -> Result { pub fn set_apqn_bind_state(card: u32, dom: u32, state: BindState) -> Result<()> { let ctx = "bind APQN"; let path = format!( - "{}/card{:02x}/{:02x}.{:04x}/se_bind", - PATH_SYS_DEVICES_AP, card, card, dom + "{PATH_SYS_DEVICES_AP}/card{card:02x}/{card:02x}.{dom:04x}/se_bind" ); match state { BindState::Bound => write_file(path, 1.to_string(), ctx), @@ -375,8 +373,7 @@ pub enum AssocState { /// an error string. Does NOT print any error messages. pub fn get_apqn_associate_state(card: u32, dom: u32) -> Result { let path = format!( - "{}/card{:02x}/{:02x}.{:04x}/se_associate", - PATH_SYS_DEVICES_AP, card, card, dom + "{PATH_SYS_DEVICES_AP}/card{card:02x}/{card:02x}.{dom:04x}/se_associate" ); let state_str = read_file_string(path, "se_associate attribute")?; let state = state_str.trim(); @@ -393,8 +390,7 @@ pub fn get_apqn_associate_state(card: u32, dom: u32) -> Result { fn set_apqn_associate_state_associate(card: u32, dom: u32, idx: u16) -> Result<()> { let path = format!( - "{}/card{:02x}/{:02x}.{:04x}/se_associate", - PATH_SYS_DEVICES_AP, card, card, dom + "{PATH_SYS_DEVICES_AP}/card{card:02x}/{card:02x}.{dom:04x}/se_associate" ); write_file(path, idx.to_string(), "associate APQN")?; let mut ms: u64 = 0; @@ -423,8 +419,7 @@ fn set_apqn_associate_state_associate(card: u32, dom: u32, idx: u16) -> Result<( fn set_apqn_associate_state_unbind(card: u32, dom: u32) -> Result<()> { let bindpath = format!( - "{}/card{:02x}/{:02x}.{:04x}/se_bind", - PATH_SYS_DEVICES_AP, card, card, dom + "{PATH_SYS_DEVICES_AP}/card{card:02x}/{card:02x}.{dom:04x}/se_bind" ); write_file(bindpath, 0.to_string(), "unbind APQN")?; let mut ms: u64 = 0; diff --git a/rust/pvapconfig/src/ap.rs b/rust/pvapconfig/src/ap.rs index 7880db32..93b23ca8 100644 --- a/rust/pvapconfig/src/ap.rs +++ b/rust/pvapconfig/src/ap.rs @@ -127,8 +127,7 @@ impl ApqnList { Ok(r) => r, Err(err) => { eprintln!( - "Failure reading AP devices {} ({:?}).", - PATH_SYS_DEVICES_AP, err + "Failure reading AP devices {PATH_SYS_DEVICES_AP} ({err:?})." ); return None; } @@ -139,8 +138,7 @@ impl ApqnList { Ok(r) => r, Err(err) => { eprintln!( - "Failure reading AP queue directories in {} ({:?}).", - path, err + "Failure reading AP queue directories in {path} ({err:?})." ); return None; } @@ -209,7 +207,7 @@ impl ApqnList { continue; } if i1.mkvp == i2.mkvp { - eprintln!("APQN {} and APQN {} have same MPVK", a1, a2); + eprintln!("APQN {a1} and APQN {a2} have same MPVK"); return false; } } diff --git a/rust/pvapconfig/src/config.rs b/rust/pvapconfig/src/config.rs index 289c1e8c..d92f82ba 100644 --- a/rust/pvapconfig/src/config.rs +++ b/rust/pvapconfig/src/config.rs @@ -143,7 +143,7 @@ impl ApConfigEntry { self.mode = mode; self.validate_accel_entry()?; } - _ => return Err(format!("Unknown or invalid mode '{}'.", mode)), + _ => return Err(format!("Unknown or invalid mode '{mode}'.")), } Ok(()) } @@ -176,16 +176,14 @@ impl ApConfigList { Ok(f) => f, Err(err) => { return Err(format!( - "Failure to open AP config file {}: {:?}", - fname, err + "Failure to open AP config file {fname}: {err:?}" )) } }; match serde_yaml::from_reader(file) { Ok(cfg) => Ok(cfg), Err(err) => Err(format!( - "Failure parsing AP config file {}: {:?}", - fname, err + "Failure parsing AP config file {fname}: {err:?}" )), } } @@ -195,10 +193,10 @@ impl ApConfigList { let ename = if !entry.name.trim().is_empty() { format!("AP config entry {} '{}'", i, entry.name.trim()) } else { - format!("AP config entry {}", i) + format!("AP config entry {i}") }; if let Err(err) = &entry.validate() { - return Err(format!("{}: {}", ename, err)); + return Err(format!("{ename}: {err}")); } } Ok(()) diff --git a/rust/pvapconfig/src/main.rs b/rust/pvapconfig/src/main.rs index 1e4a3cce..5ad67db1 100644 --- a/rust/pvapconfig/src/main.rs +++ b/rust/pvapconfig/src/main.rs @@ -103,8 +103,7 @@ fn main() -> ExitCode { }; if apconfig.is_empty() { println!( - "No AP configuration entries in config file '{}': Nothing to do.", - configfile + "No AP configuration entries in config file '{configfile}': Nothing to do." ); return ExitCode::SUCCESS; } @@ -382,7 +381,7 @@ fn do_ap_config( if let Err(err) = apqn.set_bind_state(pvap::bind_state::Bound) { // bind failed, unbind/reset this apqn, return with failure let _ = apqn.set_bind_state(pvap::bind_state::Unbound); - return Err(format!("Failure binding APQN {}: {}", apqn, err)); + return Err(format!("Failure binding APQN {apqn}: {err}")); } } // try to associate @@ -662,6 +661,6 @@ mod tests { let r = do_ap_config(&mut apqnlist, &secretlist, &apcfglist, true); assert!(r.is_ok()); let n = r.unwrap(); - assert!(n == 3, "n = {} != 3", n); + assert!(n == 3, "n = {n} != 3"); } } diff --git a/rust/pvapconfig/src/uv.rs b/rust/pvapconfig/src/uv.rs index 60bc171c..2c6a29dc 100644 --- a/rust/pvapconfig/src/uv.rs +++ b/rust/pvapconfig/src/uv.rs @@ -52,7 +52,7 @@ pub fn has_list_secrets_facility() -> Result<(), String> { /// The list may be empty if the UV doesn't have any secrets stored. pub fn gather_secrets() -> Result { let uv = match UvDevice::open() { - Err(e) => return Err(format!("Failed to open UV device: {:?}.", e)), + Err(e) => return Err(format!("Failed to open UV device: {e:?}.")), Ok(u) => u, }; let mut cmd = ListCmd::default(); diff --git a/rust/pvattest/src/additional.rs b/rust/pvattest/src/additional.rs index a4c65f54..7af6ace0 100644 --- a/rust/pvattest/src/additional.rs +++ b/rust/pvattest/src/additional.rs @@ -44,15 +44,15 @@ impl Display for AttestationResult<'_> { writeln!(f, "{:#}", self.cuid)?; if let Some(data) = &self.add { writeln!(f, "Additional-data:")?; - writeln!(f, "{:#}", data)?; + writeln!(f, "{data:#}")?; } if let Some(data) = &self.add_fields { writeln!(f, "Additional-data content:")?; - writeln!(f, "{:#}", data)?; + writeln!(f, "{data:#}")?; } if let Some(data) = &self.user_data { writeln!(f, "user-data:")?; - writeln!(f, "{:#}", data)?; + writeln!(f, "{data:#}")?; } Ok(()) } diff --git a/rust/pvattest/src/cmd/check/host_key.rs b/rust/pvattest/src/cmd/check/host_key.rs index 27b30141..c4c32b5d 100644 --- a/rust/pvattest/src/cmd/check/host_key.rs +++ b/rust/pvattest/src/cmd/check/host_key.rs @@ -123,8 +123,7 @@ pub fn host_key_check<'a, 'b>( }) { Some(phkh) => contains_phkh(&hkd_hashes, phkh, kind, check_enforced), None if check_enforced => CheckState::Err(format!( - "The Attestation result does not contain an {}, but checking was enabled.", - kind + "The Attestation result does not contain an {kind}, but checking was enabled." )), None => CheckState::Data(HostKeyCheck::default()), }; diff --git a/rust/pvimg/examples/tamper_pvimg/main.rs b/rust/pvimg/examples/tamper_pvimg/main.rs index 34756c7e..40f1bf61 100644 --- a/rust/pvimg/examples/tamper_pvimg/main.rs +++ b/rust/pvimg/examples/tamper_pvimg/main.rs @@ -1,5 +1,7 @@ #![allow(missing_docs)] +use std::{fs::File, io::Write, path::PathBuf}; + use anyhow::Context; use clap::{Parser, ValueEnum, ValueHint}; use log::info; @@ -14,7 +16,6 @@ use pvimg::{ UvDataPlainTrait, UvDataTrait, }, }; -use std::{fs::File, io::Write, path::PathBuf}; use utils::{PvLogger, VerbosityOptions}; #[derive(Parser, Debug)] @@ -139,7 +140,7 @@ fn main() -> anyhow::Result<()> { Ok(_) => (), Err(err) => { std::fs::remove_file(&opt.outfile)?; - panic!("Could not seek SE header: {}", err); + panic!("Could not seek SE header: {err}"); } }; diff --git a/rust/pvimg/src/pv_utils/layout.rs b/rust/pvimg/src/pv_utils/layout.rs index fddeaa91..e741c051 100644 --- a/rust/pvimg/src/pv_utils/layout.rs +++ b/rust/pvimg/src/pv_utils/layout.rs @@ -150,7 +150,7 @@ impl Layout { } if let Some(overlapped) = self.overlaps(&interval) { - let msg = format!("{} ... {}", overlapped, interval); + let msg = format!("{overlapped} ... {interval}"); return Err(Error::IntervalOverlap(msg)); } diff --git a/rust/pvimg/src/pv_utils/se_hdr/flags.rs b/rust/pvimg/src/pv_utils/se_hdr/flags.rs index 277c3bb1..a1af2509 100644 --- a/rust/pvimg/src/pv_utils/se_hdr/flags.rs +++ b/rust/pvimg/src/pv_utils/se_hdr/flags.rs @@ -145,7 +145,7 @@ impl ControlFlagsTrait for ControlFlags { impl Display for ControlFlags { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let value: u64 = self.flags.into(); - write!(f, "{:#018x}", value) + write!(f, "{value:#018x}") } } @@ -266,7 +266,7 @@ mod test { #[test] fn test_display() { let flags = PlaintextControlFlagsV1::from_flags([PcfV1::NoComponentEncryption.enabled()]); - assert_eq!("0x0000000010000000", format!("{}", flags)); + assert_eq!("0x0000000010000000", format!("{flags}")); let flags = PlaintextControlFlagsV1::from_flags([ PcfV1::AllowDumping.enabled(), @@ -277,7 +277,7 @@ mod test { PcfV1::PckmoEcc.enabled(), PcfV1::PckmoHmac.enabled(), ]); - assert_eq!("0x00000000300000f2", format!("{}", flags)); + assert_eq!("0x00000000300000f2", format!("{flags}")); } #[test] diff --git a/rust/pvimg/src/pv_utils/serializing.rs b/rust/pvimg/src/pv_utils/serializing.rs index a06bbf0e..63c61b40 100644 --- a/rust/pvimg/src/pv_utils/serializing.rs +++ b/rust/pvimg/src/pv_utils/serializing.rs @@ -25,7 +25,7 @@ pub fn ser_lower_hex( data: &B, ser: S, ) -> std::result::Result { - format!("{:#018x}", data).serialize(ser) + format!("{data:#018x}").serialize(ser) } pub fn ser_hex_confidential( diff --git a/rust/utils/src/hexslice.rs b/rust/utils/src/hexslice.rs index 768db3ae..eb9b7b2e 100644 --- a/rust/utils/src/hexslice.rs +++ b/rust/utils/src/hexslice.rs @@ -48,7 +48,7 @@ impl Display for HexSlice<'_> { if f.sign_minus() && f.alternate() { write!(f, "0x")?; } - write!(f, "{:0>2x}", byte)?; + write!(f, "{byte:0>2x}")?; if f.sign_minus() { write!(f, " ")?; }