refactor: clippy::cast_lossless (#974)

This commit is contained in:
EdJoPaTo 2024-03-02 11:58:43 +01:00 committed by Josh McKinney
parent 0de5238ed3
commit b8ea190bf2
No known key found for this signature in database
GPG key ID: 722287396A903BC5
5 changed files with 10 additions and 9 deletions

View file

@ -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"

View file

@ -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),

View file

@ -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
}
}

View file

@ -183,7 +183,7 @@ impl LegendPosition {
x_title_width: u16,
y_title_width: u16,
) -> Option<Rect> {
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;
}

View file

@ -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;