From 6689e2586580c42b0d925328545ff8fedd27ec0f Mon Sep 17 00:00:00 2001 From: Marc Hartmayer Date: Fri, 19 Jun 2026 11:21:15 +0200 Subject: [PATCH] rust: Fix all cargo clippy findings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Remove useless type conversion in uvdevice.rs - Replace useless comparison in hostname.rs - Replace unnecessary unwrap patterns in pvapconfig - Use sort_by_key instead of sort_by in pvimg example Command line used to get the findings: $ clippy --all-features -- --cap-lints=warn warning: useless conversion to the same type: `u64` --> pv_core/src/uvdevice.rs:56:28 | 56 | rc = ioctl(raw_fd, cmd.try_into().unwrap(), cb.as_ptr_mut()); | ^^^^^^^^^^^^^^ | = help: consider removing `.try_into()` = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion = note: `#[warn(clippy::useless_conversion)]` on by default warning: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false --> utils/src/hostname.rs:60:13 | 60 | assert!(isize::try_from(buf_len).unwrap() <= isize::MAX); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: because `isize::MAX` is the maximum value for this type, this comparison is always true = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#absurd_extreme_comparisons = note: `#[warn(clippy::absurd_extreme_comparisons)]` on by default warning: `utils` (lib) generated 1 warning Checking pvebc v0.12.0 (/home/mhartmay/git/s390-tools/rust/pvebc) warning: consider using `sort_unstable_by_key` --> pvapconfig/src/ap.rs:177:9 | 177 | self.0.sort_unstable_by(|a, b| b.gen.cmp(&a.gen)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_sort_by = note: `#[warn(clippy::unnecessary_sort_by)]` on by default help: try | 177 - self.0.sort_unstable_by(|a, b| b.gen.cmp(&a.gen)); 177 + self.0.sort_unstable_by_key(|b| std::cmp::Reverse(b.gen)); warning: called `unwrap_err` on `r` after checking its variant with `is_err` --> pvapconfig/src/main.rs:55:29 | 54 | if $r.is_err() { | -------------- help: try: `if let Err() = r` 55 | eprintln!("{}", $r.unwrap_err()); | ^^^^^^^^^^^^^^^ ... 87 | on_error_print_and_exit!(r); | --------------------------- in this macro invocation | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_unwrap = note: this warning originates in the macro `on_error_print_and_exit` (in Nightly builds, run with -Z macro-backtrace for more info) warning: this `repeat().take()` can be written more concisely --> pvimg/src/se_img_comps/bootloader/ipl.rs:95:21 | 95 | let comps = iter::repeat(ipl_pb0_pv_comp::default()) | _____________________^ 96 | | .take(num_comp) | |___________________________^ help: consider using `repeat_n()` instead: `std::iter::repeat_n(ipl_pb0_pv_comp::default(), num_comp)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_repeat_n = note: `#[warn(clippy::manual_repeat_n)]` on by default warning: this `repeat().take()` can be written more concisely --> pvimg/src/se_img_comps/bootloader/ipl.rs:113:21 | 113 | let comps = iter::repeat(comp).take(num_comp).collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `repeat_n()` instead: `std::iter::repeat_n(comp, num_comp)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_repeat_n Reviewed-by: Timo Keller Reviewed-by: Steffen Eiden Reviewed-by: Harald Freudenberger Signed-off-by: Marc Hartmayer Signed-off-by: Jan Höppner --- rust/pv_core/src/uvdevice.rs | 2 +- rust/pvapconfig/src/ap.rs | 2 +- rust/pvapconfig/src/main.rs | 4 ++-- rust/pvimg/examples/create-sehdr/main.rs | 2 +- rust/pvimg/src/se_img_comps/bootloader/ipl.rs | 6 ++---- rust/utils/src/hostname.rs | 2 +- 6 files changed, 8 insertions(+), 10 deletions(-) diff --git a/rust/pv_core/src/uvdevice.rs b/rust/pv_core/src/uvdevice.rs index 3e6529a8..023015e4 100644 --- a/rust/pv_core/src/uvdevice.rs +++ b/rust/pv_core/src/uvdevice.rs @@ -53,7 +53,7 @@ fn ioctl_raw(raw_fd: RawFd, cmd: u64, cb: &mut IoctlCb) -> Result<()> { // SAFETY: the passed pointer points to a valid memory region that // contains the expected C-struct. The struct outlives this function. unsafe { - rc = ioctl(raw_fd, cmd.try_into().unwrap(), cb.as_ptr_mut()); + rc = ioctl(raw_fd, cmd, cb.as_ptr_mut()); } // NOTE io::Error handles all errnos ioctl uses diff --git a/rust/pvapconfig/src/ap.rs b/rust/pvapconfig/src/ap.rs index 76798e26..23d2190b 100644 --- a/rust/pvapconfig/src/ap.rs +++ b/rust/pvapconfig/src/ap.rs @@ -174,7 +174,7 @@ impl ApqnList { /// Sort this Apqnlist by card generation: /// newest generation first, older generations last. pub fn sort_by_gen(&mut self) { - self.0.sort_unstable_by(|a, b| b.gen.cmp(&a.gen)); + self.0.sort_unstable_by_key(|b| std::cmp::Reverse(b.gen)); } /// Check MK restriction diff --git a/rust/pvapconfig/src/main.rs b/rust/pvapconfig/src/main.rs index f967e7b9..f0b2ca9f 100644 --- a/rust/pvapconfig/src/main.rs +++ b/rust/pvapconfig/src/main.rs @@ -51,8 +51,8 @@ macro_rules! println_and_exit_failure { /// returns with exit failure. macro_rules! on_error_print_and_exit { ($r:expr) => { - if $r.is_err() { - eprintln!("{}", $r.unwrap_err()); + if let Err(e) = $r { + eprintln!("{}", e); return ExitCode::FAILURE; } }; diff --git a/rust/pvimg/examples/create-sehdr/main.rs b/rust/pvimg/examples/create-sehdr/main.rs index 6bb170a1..abc13885 100644 --- a/rust/pvimg/examples/create-sehdr/main.rs +++ b/rust/pvimg/examples/create-sehdr/main.rs @@ -176,7 +176,7 @@ fn main() -> anyhow::Result<()> { let mut secure_comp_builer = SecuredComponentBuilder::new_v1(false)?; // Sort components by address in ascending order - args.components.sort_by(|a, b| a.addr.cmp(&b.addr)); + args.components.sort_by_key(|a| a.addr); for component_arg in args.components { info!("## Preparing {}", component_arg); let mut comp = Comp { diff --git a/rust/pvimg/src/se_img_comps/bootloader/ipl.rs b/rust/pvimg/src/se_img_comps/bootloader/ipl.rs index 66519681..584e83b2 100644 --- a/rust/pvimg/src/se_img_comps/bootloader/ipl.rs +++ b/rust/pvimg/src/se_img_comps/bootloader/ipl.rs @@ -92,9 +92,7 @@ use std::iter; impl ipl_parameter_block { pub fn size(num_comp: usize) -> Result { - let comps = iter::repeat(ipl_pb0_pv_comp::default()) - .take(num_comp) - .collect(); + let comps = std::iter::repeat_n(ipl_pb0_pv_comp::default(), num_comp).collect(); let ipib = Self { pv: ipl_pb0_pv { components: comps, @@ -110,7 +108,7 @@ impl ipl_parameter_block { impl ipl_pb0_pv { pub fn size(num_comp: usize) -> Result { let comp = ipl_pb0_pv_comp::default(); - let comps = iter::repeat(comp).take(num_comp).collect(); + let comps = std::iter::repeat_n(comp, num_comp).collect(); let ipl = Self { components: comps, ..Default::default() diff --git a/rust/utils/src/hostname.rs b/rust/utils/src/hostname.rs index 5bcb4658..1ff91dac 100644 --- a/rust/utils/src/hostname.rs +++ b/rust/utils/src/hostname.rs @@ -57,7 +57,7 @@ pub fn gethostname() -> io::Result { } } - assert!(isize::try_from(buf_len).unwrap() <= isize::MAX); + assert!(buf_len <= isize::MAX as usize); // SAFETY: We made sure that `buf` is: // 1. NUL-terminated