Skip to content

Commit

Permalink
Clippy fixes (#2197)
Browse files Browse the repository at this point in the history
  • Loading branch information
fintelia committed Apr 7, 2024
1 parent 78167c3 commit ba78cba
Show file tree
Hide file tree
Showing 13 changed files with 21 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/animation.rs
Expand Up @@ -140,7 +140,7 @@ impl Delay {
// > `0 < b <= (u32::MAX + 1)/(millis + 1)`
// Corollary: millis <= u32::MAX

const MILLIS_BOUND: u128 = u32::max_value() as u128;
const MILLIS_BOUND: u128 = u32::MAX as u128;

let millis = duration.as_millis().min(MILLIS_BOUND);
let submillis = (duration.as_nanos() % 1_000_000) as u32;
Expand Down
2 changes: 1 addition & 1 deletion src/codecs/bmp/decoder.rs
Expand Up @@ -631,7 +631,7 @@ impl<R: BufRead + Seek> BmpDecoder<R> {
return Err(DecoderError::ImageTooLarge(self.width, self.height).into());
}

if self.height == i32::min_value() {
if self.height == i32::MIN {
return Err(DecoderError::InvalidHeight.into());
}

Expand Down
1 change: 0 additions & 1 deletion src/codecs/farbfeld.rs
Expand Up @@ -16,7 +16,6 @@
//! # Related Links
//! * <https://tools.suckless.org/farbfeld/> - the farbfeld specification

use std::i64;
use std::io::{self, Read, Seek, SeekFrom, Write};

use crate::color::ExtendedColorType;
Expand Down
2 changes: 1 addition & 1 deletion src/codecs/gif.rs
Expand Up @@ -551,7 +551,7 @@ impl<W: Write> GifEncoder<W> {
// would require a new special cased variant in ParameterErrorKind which most
// likely couldn't be reused for other cases. This isn't a bad trade-off given
// that the current algorithm is already lossy.
frame.delay = (frame_delay / 10).try_into().unwrap_or(std::u16::MAX);
frame.delay = (frame_delay / 10).try_into().unwrap_or(u16::MAX);

Ok(frame)
}
Expand Down
6 changes: 1 addition & 5 deletions src/codecs/jpeg/encoder.rs
Expand Up @@ -400,11 +400,7 @@ impl<W: Write> JpegEncoder<W> {
let mut tables = vec![STD_LUMA_QTABLE, STD_CHROMA_QTABLE];
tables.iter_mut().for_each(|t| {
t.iter_mut().for_each(|v| {
*v = clamp(
(u32::from(*v) * scale + 50) / 100,
1,
u32::from(u8::max_value()),
) as u8;
*v = clamp((u32::from(*v) * scale + 50) / 100, 1, u32::from(u8::MAX)) as u8;
})
});

Expand Down
1 change: 0 additions & 1 deletion src/dynimage.rs
@@ -1,6 +1,5 @@
use std::io::{self, Seek, Write};
use std::path::Path;
use std::u32;

#[cfg(feature = "gif")]
use crate::codecs::gif;
Expand Down
8 changes: 4 additions & 4 deletions src/flat.rs
Expand Up @@ -139,8 +139,8 @@ impl SampleLayout {
/// # Panics
///
/// On platforms where `usize` has the same size as `u32` this panics when the resulting stride
/// in the `height` direction would be larger than `usize::max_value()`. On other platforms
/// where it can surely accommodate `u8::max_value() * u32::max_value(), this can never happen.
/// in the `height` direction would be larger than `usize::MAX`. On other platforms
/// where it can surely accommodate `u8::MAX * u32::MAX, this can never happen.
pub fn row_major_packed(channels: u8, width: u32, height: u32) -> Self {
let height_stride = (channels as usize).checked_mul(width as usize).expect(
"Row major packed image can not be described because it does not fit into memory",
Expand Down Expand Up @@ -169,8 +169,8 @@ impl SampleLayout {
/// # Panics
///
/// On platforms where `usize` has the same size as `u32` this panics when the resulting stride
/// in the `width` direction would be larger than `usize::max_value()`. On other platforms
/// where it can surely accommodate `u8::max_value() * u32::max_value(), this can never happen.
/// in the `width` direction would be larger than `usize::MAX`. On other platforms
/// where it can surely accommodate `u8::MAX * u32::MAX, this can never happen.
pub fn column_major_packed(channels: u8, width: u32, height: u32) -> Self {
let width_stride = (channels as usize).checked_mul(height as usize).expect(
"Column major packed image can not be described because it does not fit into memory",
Expand Down
9 changes: 4 additions & 5 deletions src/image.rs
Expand Up @@ -3,7 +3,6 @@ use std::ffi::OsStr;
use std::io::{self, Write};
use std::ops::{Deref, DerefMut};
use std::path::Path;
use std::usize;

use crate::color::{ColorType, ExtendedColorType};
use crate::error::{
Expand Down Expand Up @@ -482,7 +481,7 @@ where
let row_bytes = bytes_per_pixel * u64::from(dimensions.0);
let total_bytes = width * height * bytes_per_pixel;

if buf.len() < usize::try_from(total_bytes).unwrap_or(usize::max_value()) {
if buf.len() < usize::try_from(total_bytes).unwrap_or(usize::MAX) {
panic!(
"output buffer too short\n expected `{}`, provided `{}`",
total_bytes,
Expand Down Expand Up @@ -560,7 +559,7 @@ where
ParameterErrorKind::DimensionMismatch,
)));
}
if scanline_bytes > usize::max_value() as u64 {
if scanline_bytes > usize::MAX as u64 {
return Err(ImageError::Limits(LimitError::from_kind(
LimitErrorKind::InsufficientMemory,
)));
Expand Down Expand Up @@ -592,7 +591,7 @@ where
T: crate::traits::Primitive + bytemuck::Pod,
{
let total_bytes = usize::try_from(decoder.total_bytes());
if total_bytes.is_err() || total_bytes.unwrap() > isize::max_value() as usize {
if total_bytes.is_err() || total_bytes.unwrap() > isize::MAX as usize {
return Err(ImageError::Limits(LimitError::from_kind(
LimitErrorKind::InsufficientMemory,
)));
Expand Down Expand Up @@ -1786,7 +1785,7 @@ mod tests {
(*self).read_image(buf)
}
}
assert_eq!(D.total_bytes(), u64::max_value());
assert_eq!(D.total_bytes(), u64::MAX);

let v: ImageResult<Vec<u8>> = super::decoder_to_vec(D);
assert!(v.is_err());
Expand Down
4 changes: 2 additions & 2 deletions src/imageops/mod.rs
Expand Up @@ -438,8 +438,8 @@ mod tests {
overlay(
&mut target,
&source,
i64::from(u32::max_value() - 31),
i64::from(u32::max_value() - 31),
i64::from(u32::MAX - 31),
i64::from(u32::MAX - 31),
);
assert!(*target.get_pixel(0, 0) == Rgb([0, 0, 0]));
assert!(*target.get_pixel(1, 1) == Rgb([0, 0, 0]));
Expand Down
1 change: 0 additions & 1 deletion src/io/free_functions.rs
@@ -1,7 +1,6 @@
use std::fs::File;
use std::io::{BufRead, BufWriter, Seek};
use std::path::Path;
use std::u32;

use crate::{codecs::*, ExtendedColorType};

Expand Down
12 changes: 6 additions & 6 deletions src/math/utils.rs
Expand Up @@ -48,7 +48,7 @@ mod test {
// We could check that case separately but it does not conform to the same expectation.
if new_w as u64 * 400u64 >= old_w as u64 * u64::from(u32::MAX) { return true; }

let result = super::resize_dimensions(old_w, 400, new_w, std::u32::MAX, false);
let result = super::resize_dimensions(old_w, 400, new_w, u32::MAX, false);
let exact = (400_f64 * new_w as f64 / old_w as f64).round() as u32;
result.0 == new_w && result.1 == exact.max(1)
}
Expand All @@ -61,7 +61,7 @@ mod test {
// We could check that case separately but it does not conform to the same expectation.
if 400u64 * new_h as u64 >= old_h as u64 * u64::from(u32::MAX) { return true; }

let result = super::resize_dimensions(400, old_h, std::u32::MAX, new_h, false);
let result = super::resize_dimensions(400, old_h, u32::MAX, new_h, false);
let exact = (400_f64 * new_h as f64 / old_h as f64).round() as u32;
result.1 == new_h && result.0 == exact.max(1)
}
Expand All @@ -87,12 +87,12 @@ mod test {

#[test]
fn resize_handles_overflow() {
let result = super::resize_dimensions(100, std::u32::MAX, 200, std::u32::MAX, true);
let result = super::resize_dimensions(100, u32::MAX, 200, u32::MAX, true);
assert!(result.0 == 100);
assert!(result.1 == std::u32::MAX);
assert!(result.1 == u32::MAX);

let result = super::resize_dimensions(std::u32::MAX, 100, std::u32::MAX, 200, true);
assert!(result.0 == std::u32::MAX);
let result = super::resize_dimensions(u32::MAX, 100, u32::MAX, 200, true);
assert!(result.0 == u32::MAX);
assert!(result.1 == 100);
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/mod.rs
Expand Up @@ -66,7 +66,7 @@ pub(crate) fn expand_bits(bit_depth: u8, row_size: u32, buf: &[u8]) -> Vec<u8> {
#[allow(dead_code)]
// When no image formats that use it are enabled
pub(crate) fn check_dimension_overflow(width: u32, height: u32, bytes_per_pixel: u8) -> bool {
width as u64 * height as u64 > std::u64::MAX / bytes_per_pixel as u64
width as u64 * height as u64 > u64::MAX / bytes_per_pixel as u64
}

#[allow(dead_code)]
Expand Down
1 change: 0 additions & 1 deletion tests/reference_images.rs
Expand Up @@ -2,7 +2,6 @@
use std::fs;
use std::io;
use std::path::PathBuf;
use std::u32;

use crc32fast::Hasher as Crc32;
use image::DynamicImage;
Expand Down

0 comments on commit ba78cba

Please sign in to comment.