From 14867c7abc1ce053affe2d796168fb1ade22a788 Mon Sep 17 00:00:00 2001 From: Kornel Date: Wed, 19 May 2021 15:04:31 +0100 Subject: [PATCH] Clippy lints (#401) --- src/colors.rs | 4 ++++ src/error.rs | 10 +++------ src/headers.rs | 13 ++++++++--- src/lib.rs | 9 ++++---- src/png/mod.rs | 8 +++---- src/png/scan_lines.rs | 2 +- src/rayon.rs | 6 +----- src/reduction/bit_depth.rs | 3 +-- src/reduction/color.rs | 2 +- src/reduction/mod.rs | 44 ++++++++++++++++---------------------- 10 files changed, 49 insertions(+), 52 deletions(-) diff --git a/src/colors.rs b/src/colors.rs index f5fe57ec..ae698dca 100644 --- a/src/colors.rs +++ b/src/colors.rs @@ -101,6 +101,10 @@ impl BitDepth { } } /// Parse a number of bits per channel per pixel into a `BitDepth` + /// + /// # Panics + /// + /// If depth is unsupported #[inline] pub fn from_u8(depth: u8) -> BitDepth { match depth { diff --git a/src/error.rs b/src/error.rs index 0198379f..0f099d5d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -14,15 +14,11 @@ pub enum PngError { Other(Box), } -impl Error for PngError { - // deprecated - fn description(&self) -> &str { - "" - } -} +impl Error for PngError {} impl fmt::Display for PngError { #[inline] + #[cold] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { PngError::DeflatedDataTooLong(_) => f.write_str("deflated data too long"), @@ -40,7 +36,7 @@ impl fmt::Display for PngError { } impl PngError { - #[inline] + #[cold] pub fn new(description: &str) -> PngError { PngError::Other(description.into()) } diff --git a/src/headers.rs b/src/headers.rs index 049ec00c..be0381d9 100644 --- a/src/headers.rs +++ b/src/headers.rs @@ -27,11 +27,14 @@ pub struct IhdrData { impl IhdrData { /// Bits per pixel + #[must_use] + #[inline] pub fn bpp(&self) -> u8 { self.bit_depth.as_u8() * self.color_type.channels_per_pixel() } /// Byte length of IDAT that is correct for this IHDR + #[must_use] pub fn raw_data_size(&self) -> usize { let w = self.width as usize; let h = self.height as usize; @@ -134,7 +137,7 @@ pub fn parse_next_header<'a>( ))); } - let mut name = [0u8; 4]; + let mut name = [0_u8; 4]; name.copy_from_slice(chunk_name); Ok(Some(RawHeader { name, data })) } @@ -160,8 +163,12 @@ pub fn parse_ihdr_header(byte_data: &[u8]) -> PngResult { 16 => BitDepth::Sixteen, _ => return Err(PngError::new("Unexpected bit depth in header")), }, - width: rdr.read_u32::().unwrap(), - height: rdr.read_u32::().unwrap(), + width: rdr + .read_u32::() + .map_err(|_| PngError::TruncatedData)?, + height: rdr + .read_u32::() + .map_err(|_| PngError::TruncatedData)?, compression: byte_data[10], filter: byte_data[11], interlaced, diff --git a/src/lib.rs b/src/lib.rs index c43ff623..0b6fc479 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,7 @@ #![warn(clippy::path_buf_push_overwrite)] #![warn(clippy::range_plus_one)] #![allow(clippy::cognitive_complexity)] +#![allow(clippy::upper_case_acronyms)] #![cfg_attr( not(any(feature = "libdeflater", feature = "zopfli")), allow(irrefutable_let_patterns), @@ -97,7 +98,7 @@ impl InFile { pub fn path(&self) -> Option<&Path> { match *self { InFile::Path(ref p) => Some(p.as_path()), - _ => None, + InFile::StdIn => None, } } } @@ -589,7 +590,7 @@ fn optimize_png( ); return None; } - _ => return None, + Err(_) => return None, }; // update best size across all threads @@ -655,14 +656,14 @@ fn optimize_png( " file size = {} bytes ({} bytes = {:.2}% decrease)", output.len(), file_original_size - output.len(), - (file_original_size - output.len()) as f64 / file_original_size as f64 * 100f64 + (file_original_size - output.len()) as f64 / file_original_size as f64 * 100_f64 ); } else { info!( " file size = {} bytes ({} bytes = {:.2}% increase)", output.len(), output.len() - file_original_size, - (output.len() - file_original_size) as f64 / file_original_size as f64 * 100f64 + (output.len() - file_original_size) as f64 / file_original_size as f64 * 100_f64 ); } diff --git a/src/png/mod.rs b/src/png/mod.rs index eddc44bf..e8b6dddc 100644 --- a/src/png/mod.rs +++ b/src/png/mod.rs @@ -208,10 +208,10 @@ impl PngData { .fold( 0, |prev, (index, px)| { - if px.a != 255 { - index + 1 - } else { + if px.a == 255 { prev + } else { + index + 1 } }, ); @@ -346,7 +346,7 @@ impl PngImage { // Avoid vertical filtering on first line of each interlacing pass for filter in if last_pass == line.pass { 0..5 } else { 0..2 } { filter_line(filter, bpp, &line.data, last_line, &mut f_buf); - let size = f_buf.iter().fold(0u64, |acc, &x| { + let size = f_buf.iter().fold(0_u64, |acc, &x| { let signed = x as i8; acc + i16::from(signed).abs() as u64 }); diff --git a/src/png/scan_lines.rs b/src/png/scan_lines.rs index 90b92589..2a13941f 100644 --- a/src/png/scan_lines.rs +++ b/src/png/scan_lines.rs @@ -1,7 +1,7 @@ use crate::png::PngImage; -#[derive(Debug, Clone)] /// An iterator over the scan lines of a PNG image +#[derive(Debug, Clone)] pub struct ScanLines<'a> { iter: ScanLineRanges, /// A reference to the PNG image being iterated upon diff --git a/src/rayon.rs b/src/rayon.rs index c0ffd8c5..768db04c 100644 --- a/src/rayon.rs +++ b/src/rayon.rs @@ -10,11 +10,7 @@ pub trait ParallelIterator: Iterator + Sized { where OP: Fn(Self::Item, Self::Item) -> Self::Item + Sync, { - if let Some(a) = self.next() { - Some(self.fold(a, op)) - } else { - None - } + self.next().map(|a| self.fold(a, op)) } } diff --git a/src/reduction/bit_depth.rs b/src/reduction/bit_depth.rs index 49fee066..75ad95df 100644 --- a/src/reduction/bit_depth.rs +++ b/src/reduction/bit_depth.rs @@ -123,9 +123,8 @@ pub fn reduce_bit_depth_8_or_less(png: &PngImage, mut minimum_bits: usize) -> Op }; if permutations.iter().any(|perm| *perm == byte) { break; - } else { - minimum_bits <<= 1; } + minimum_bits <<= 1; } } } diff --git a/src/reduction/color.rs b/src/reduction/color.rs index 42d47c9b..11686fc6 100644 --- a/src/reduction/color.rs +++ b/src/reduction/color.rs @@ -150,7 +150,7 @@ pub fn reduced_color_to_palette(png: &PngImage) -> Option { } }) .max(); - let trns_size = num_transparent.map(|n| n + 8).unwrap_or(0); + let trns_size = num_transparent.map_or(0, |n| n + 8); let headers_size = palette.len() * 3 + 8 + trns_size; if raw_data.len() + headers_size > png.data.len() { diff --git a/src/reduction/mod.rs b/src/reduction/mod.rs index 1b9f7fc7..e6f4bf7d 100644 --- a/src/reduction/mod.rs +++ b/src/reduction/mod.rs @@ -6,9 +6,9 @@ use rgb::RGBA8; use std::borrow::Cow; pub mod alpha; -use crate::alpha::*; +use crate::alpha::reduced_alpha_channel; pub mod bit_depth; -use crate::bit_depth::*; +use crate::bit_depth::reduce_bit_depth_8_or_less; pub mod color; use crate::color::*; @@ -17,7 +17,6 @@ pub(crate) use crate::bit_depth::reduce_bit_depth; /// Attempt to reduce the number of colors in the palette /// Returns `None` if palette hasn't changed -#[must_use] pub fn reduced_palette(png: &PngImage) -> Option { if png.ihdr.color_type != ColorType::Indexed { // Can't reduce if there is no palette @@ -70,14 +69,14 @@ pub fn reduced_palette(png: &PngImage) -> Option { .unwrap_or_else(|| RGBA8::new(0, 0, 0, 255)); ((color.a as i32) << 18) // These are coefficients for standard sRGB to luma conversion - - (color.r as i32) * 299 - - (color.g as i32) * 587 - - (color.b as i32) * 114 + - i32::from(color.r) * 299 + - i32::from(color.g) * 587 + - i32::from(color.b) * 114 }; color_val(a.0).cmp(&color_val(b.0)) }); - let mut next_index = 0u16; + let mut next_index = 0_u16; let mut seen = IndexMap::with_capacity(palette.len()); for (i, used) in used_enumerated.iter().cloned() { if !used { @@ -138,33 +137,33 @@ fn do_palette_reduction(png: &PngImage, palette_map: &[Option; 256]) -> Opti } fn palette_map_to_byte_map(png: &PngImage, palette_map: &[Option; 256]) -> Option<[u8; 256]> { - let len = png.palette.as_ref().map(|p| p.len()).unwrap_or(0); + let len = png.palette.as_ref().map_or(0, |p| p.len()); if (0..len).all(|i| palette_map[i].map_or(true, |to| to == i as u8)) { // No reduction necessary return None; } - let mut byte_map = [0u8; 256]; + let mut byte_map = [0_u8; 256]; // low bit-depths can be pre-computed for every byte value match png.ihdr.bit_depth { BitDepth::Eight => { - for byte in 0..=255 { - byte_map[byte as usize] = palette_map[byte as usize].unwrap_or(0) + for byte in 0..=255usize { + byte_map[byte] = palette_map[byte].unwrap_or(0) } } BitDepth::Four => { - for byte in 0..=255 { - byte_map[byte as usize] = palette_map[(byte & 0x0F) as usize].unwrap_or(0) - | (palette_map[(byte >> 4) as usize].unwrap_or(0) << 4); + for byte in 0..=255usize { + byte_map[byte] = palette_map[(byte & 0x0F)].unwrap_or(0) + | (palette_map[(byte >> 4)].unwrap_or(0) << 4); } } BitDepth::Two => { - for byte in 0..=255 { - byte_map[byte as usize] = palette_map[(byte & 0x03) as usize].unwrap_or(0) - | (palette_map[((byte >> 2) & 0x03) as usize].unwrap_or(0) << 2) - | (palette_map[((byte >> 4) & 0x03) as usize].unwrap_or(0) << 4) - | (palette_map[(byte >> 6) as usize].unwrap_or(0) << 6); + for byte in 0..=255usize { + byte_map[byte] = palette_map[(byte & 0x03)].unwrap_or(0) + | (palette_map[((byte >> 2) & 0x03)].unwrap_or(0) << 2) + | (palette_map[((byte >> 4) & 0x03)].unwrap_or(0) << 4) + | (palette_map[(byte >> 6)].unwrap_or(0) << 6); } } _ => {} @@ -174,12 +173,7 @@ fn palette_map_to_byte_map(png: &PngImage, palette_map: &[Option; 256]) -> O } fn reordered_palette(palette: &[RGBA8], palette_map: &[Option; 256]) -> Vec { - let max_index = palette_map - .iter() - .cloned() - .filter_map(|x| x) - .max() - .unwrap_or(0) as usize; + let max_index = palette_map.iter().cloned().flatten().max().unwrap_or(0) as usize; let mut new_palette = vec![RGBA8::new(0, 0, 0, 255); max_index + 1]; for (&color, &map_to) in palette.iter().zip(palette_map.iter()) { if let Some(map_to) = map_to {