From b8ea190bf2cde8c18e2ac8276d2eb57d219db263 Mon Sep 17 00:00:00 2001 From: EdJoPaTo Date: Sat, 2 Mar 2024 11:58:43 +0100 Subject: [PATCH] refactor: clippy::cast_lossless (#974) --- Cargo.toml | 1 + src/layout/constraint.rs | 6 +++--- src/layout/rect.rs | 4 ++-- src/widgets/chart.rs | 2 +- src/widgets/reflow.rs | 6 +++--- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e9733587..1673af7e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -64,6 +64,7 @@ serde_json = "1.0.109" [lints.rust] unsafe_code = "forbid" [lints.clippy] +cast_lossless = "warn" cloned_instead_of_copied = "warn" default_trait_access = "warn" deref_by_slicing = "warn" diff --git a/src/layout/constraint.rs b/src/layout/constraint.rs index b223538d..b856f7d0 100644 --- a/src/layout/constraint.rs +++ b/src/layout/constraint.rs @@ -198,15 +198,15 @@ impl Constraint { pub fn apply(&self, length: u16) -> u16 { match *self { Self::Percentage(p) => { - let p = p as f32 / 100.0; - let length = length as f32; + let p = f32::from(p) / 100.0; + let length = f32::from(length); (p * length).min(length) as u16 } Self::Ratio(numerator, denominator) => { // avoid division by zero by using 1 when denominator is 0 // this results in 0/0 -> 0 and x/0 -> x for x != 0 let percentage = numerator as f32 / denominator.max(1) as f32; - let length = length as f32; + let length = f32::from(length); (percentage * length).min(length) as u16 } Self::Length(l) => length.min(l), diff --git a/src/layout/rect.rs b/src/layout/rect.rs index f7ecfe58..f4dd73a0 100644 --- a/src/layout/rect.rs +++ b/src/layout/rect.rs @@ -141,10 +141,10 @@ impl Rect { Self { x: i32::from(self.x) .saturating_add(offset.x) - .clamp(0, (u16::MAX - self.width) as i32) as u16, + .clamp(0, i32::from(u16::MAX - self.width)) as u16, y: i32::from(self.y) .saturating_add(offset.y) - .clamp(0, (u16::MAX - self.height) as i32) as u16, + .clamp(0, i32::from(u16::MAX - self.height)) as u16, ..self } } diff --git a/src/widgets/chart.rs b/src/widgets/chart.rs index 751d8c52..b37f1ddc 100644 --- a/src/widgets/chart.rs +++ b/src/widgets/chart.rs @@ -183,7 +183,7 @@ impl LegendPosition { x_title_width: u16, y_title_width: u16, ) -> Option { - let mut height_margin = (area.height - legend_height) as i32; + let mut height_margin = i32::from(area.height - legend_height); if x_title_width != 0 { height_margin -= 1; } diff --git a/src/widgets/reflow.rs b/src/widgets/reflow.rs index abcf7f98..27773f85 100644 --- a/src/widgets/reflow.rs +++ b/src/widgets/reflow.rs @@ -144,9 +144,9 @@ where // or if it would be too long with the current partially processed word added || current_line_width + whitespace_width + word_width >= self.max_line_width && symbol_width > 0 { - let mut remaining_width = - (self.max_line_width as i32 - current_line_width as i32).max(0) - as u16; + let mut remaining_width = (i32::from(self.max_line_width) + - i32::from(current_line_width)) + .max(0) as u16; wrapped_lines.push(std::mem::take(&mut current_line)); current_line_width = 0;