From 1ff7036bae562bde06f10b64c2e9171cc83197db Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Sat, 9 Nov 2024 14:16:32 +0100 Subject: [PATCH] nihav_core: fix or update clippy warnings --- nihav-core/src/compr/deflate.rs | 15 ++++---- nihav-core/src/compr/mod.rs | 1 - nihav-core/src/formats.rs | 11 +++--- nihav-core/src/io/bitwriter.rs | 1 - nihav-core/src/io/byteio.rs | 42 +++++++---------------- nihav-core/src/io/codebook.rs | 1 - nihav-core/src/io/intcode.rs | 5 ++- nihav-core/src/lib.rs | 11 ------ nihav-core/src/scale/palette/elbg.rs | 20 +++++------ nihav-core/src/scale/palette/mediancut.rs | 4 +-- nihav-core/src/scale/palette/neuquant.rs | 1 + nihav-core/src/scale/palette/palettise.rs | 8 ++--- nihav-core/src/scale/repack.rs | 16 +++++---- nihav-core/src/scale/scale/mod.rs | 4 +-- 14 files changed, 53 insertions(+), 87 deletions(-) diff --git a/nihav-core/src/compr/deflate.rs b/nihav-core/src/compr/deflate.rs index c0a2d5f..052043d 100644 --- a/nihav-core/src/compr/deflate.rs +++ b/nihav-core/src/compr/deflate.rs @@ -368,9 +368,9 @@ impl Inflate { return Err(DecompressError::InvalidData); } let cstart = (self.bpos.wrapping_sub(offset)) & mask; - for i in 0..len { + for (i, out) in dst[..len].iter_mut().enumerate() { self.buf[(self.bpos + i) & mask] = self.buf[(cstart + i) & mask]; - dst[i] = self.buf[(cstart + i) & mask]; + *out = self.buf[(cstart + i) & mask]; } self.bpos = (self.bpos + len) & mask; self.full_pos += len; @@ -1041,10 +1041,9 @@ struct GzipCRC32 { } impl GzipCRC32 { - #[allow(clippy::unreadable_literal)] fn new() -> Self { let mut tab = [0u32; 256]; - for i in 0..256 { + for (i, el) in tab.iter_mut().enumerate() { let mut c = i as u32; for _ in 0..8 { if (c & 1) != 0 { @@ -1053,7 +1052,7 @@ impl GzipCRC32 { c >>= 1; } } - tab[i] = c; + *el = c; } Self { tab, crc: 0 } } @@ -1159,9 +1158,7 @@ pub fn gzip_decode(br: &mut ByteReader, skip_crc: bool) -> DecompressResult= 8 && llen < 255 { + if (8..255).contains(&llen) { let bits = (16 - llen.leading_zeros() - 3) as u8; self.write(llen & ((1 << bits) - 1), bits); } diff --git a/nihav-core/src/compr/mod.rs b/nihav-core/src/compr/mod.rs index a2062d4..6e2f782 100644 --- a/nihav-core/src/compr/mod.rs +++ b/nihav-core/src/compr/mod.rs @@ -1,6 +1,5 @@ //! Various compression formats support. #[cfg(feature="deflate")] -#[allow(clippy::manual_range_contains)] pub mod deflate; use crate::io::byteio::ByteIOError; diff --git a/nihav-core/src/formats.rs b/nihav-core/src/formats.rs index fdb7a1b..bdb0f40 100644 --- a/nihav-core/src/formats.rs +++ b/nihav-core/src/formats.rs @@ -713,16 +713,16 @@ impl NAPixelFormaton { start_shift = start_shift.min(comp.shift); if comp.comp_offs != 0 { use_shift = false; } } - for component in 0..(self.components as usize) { + for dname in name[..(self.components as usize)].iter_mut() { for (comp, cname) in self.comp_info.iter().zip(b"rgba".iter()) { if let Some(comp) = comp { if use_shift { if comp.shift == start_shift { - name[component] = *cname; + *dname = *cname; start_shift += comp.depth; } } else if comp.comp_offs == start_off { - name[component] = *cname; + *dname = *cname; if planar { start_off += 1; } else { @@ -909,13 +909,12 @@ fn parse_rgb_format(s: &str) -> Result { }, 5551 => { let mut offs = [0; 4]; - let depth = [ 5, 5, 5, 1 ]; let mut cur_off = 0; - for comp in 0..4 { + for (comp, &depth) in [ 5, 5, 5, 1 ].iter().enumerate() { for (off, ord) in offs.iter_mut().zip(order.iter()) { if *ord == comp { *off = cur_off; - cur_off += depth[comp]; + cur_off += depth; break; } } diff --git a/nihav-core/src/io/bitwriter.rs b/nihav-core/src/io/bitwriter.rs index 4f3c39c..e11a7cf 100644 --- a/nihav-core/src/io/bitwriter.rs +++ b/nihav-core/src/io/bitwriter.rs @@ -76,7 +76,6 @@ impl BitWriter { self.flush(); } /// Writes `bits` bits of `val` value to the output. - #[allow(clippy::collapsible_if)] #[allow(clippy::collapsible_else_if)] pub fn write(&mut self, val: u32, bits: u8) { if bits == 0 { diff --git a/nihav-core/src/io/byteio.rs b/nihav-core/src/io/byteio.rs index 61b8816..3642422 100644 --- a/nihav-core/src/io/byteio.rs +++ b/nihav-core/src/io/byteio.rs @@ -517,8 +517,7 @@ impl<'a> ByteIO for MemoryReader<'a> { Ok(read_size) } - #[allow(unused_variables)] - fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> { + fn write_buf(&mut self, _buf: &[u8]) -> ByteIOResult<()> { Err(ByteIOError::NotImplemented) } @@ -618,8 +617,7 @@ impl ByteIO for FileReader { Ok(size) } - #[allow(unused_variables)] - fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> { + fn write_buf(&mut self, _buf: &[u8]) -> ByteIOResult<()> { Err(ByteIOError::NotImplemented) } @@ -773,8 +771,7 @@ impl ByteIO for BoundedFileReader { Ok(size) } - #[allow(unused_variables)] - fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> { + fn write_buf(&mut self, _buf: &[u8]) -> ByteIOResult<()> { Err(ByteIOError::NotImplemented) } @@ -1006,28 +1003,23 @@ impl<'a> MemoryWriter<'a> { } impl<'a> ByteIO for MemoryWriter<'a> { - #[allow(unused_variables)] fn read_byte(&mut self) -> ByteIOResult { Err(ByteIOError::NotImplemented) } - #[allow(unused_variables)] fn peek_byte(&mut self) -> ByteIOResult { Err(ByteIOError::NotImplemented) } - #[allow(unused_variables)] - fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult { + fn read_buf(&mut self, _buf: &mut [u8]) -> ByteIOResult { Err(ByteIOError::NotImplemented) } - #[allow(unused_variables)] - fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult { + fn read_buf_some(&mut self, _buf: &mut [u8]) -> ByteIOResult { Err(ByteIOError::NotImplemented) } - #[allow(unused_variables)] - fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult { + fn peek_buf(&mut self, _buf: &mut [u8]) -> ByteIOResult { Err(ByteIOError::NotImplemented) } @@ -1084,28 +1076,23 @@ impl<'a> GrowableMemoryWriter<'a> { } impl<'a> ByteIO for GrowableMemoryWriter<'a> { - #[allow(unused_variables)] fn read_byte(&mut self) -> ByteIOResult { Err(ByteIOError::NotImplemented) } - #[allow(unused_variables)] fn peek_byte(&mut self) -> ByteIOResult { Err(ByteIOError::NotImplemented) } - #[allow(unused_variables)] - fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult { + fn read_buf(&mut self, _buf: &mut [u8]) -> ByteIOResult { Err(ByteIOError::NotImplemented) } - #[allow(unused_variables)] - fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult { + fn read_buf_some(&mut self, _buf: &mut [u8]) -> ByteIOResult { Err(ByteIOError::NotImplemented) } - #[allow(unused_variables)] - fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult { + fn peek_buf(&mut self, _buf: &mut [u8]) -> ByteIOResult { Err(ByteIOError::NotImplemented) } @@ -1155,28 +1142,23 @@ impl FileWriter { } impl ByteIO for FileWriter { - #[allow(unused_variables)] fn read_byte(&mut self) -> ByteIOResult { Err(ByteIOError::NotImplemented) } - #[allow(unused_variables)] fn peek_byte(&mut self) -> ByteIOResult { Err(ByteIOError::NotImplemented) } - #[allow(unused_variables)] - fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult { + fn read_buf(&mut self, _buf: &mut [u8]) -> ByteIOResult { Err(ByteIOError::NotImplemented) } - #[allow(unused_variables)] - fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult { + fn read_buf_some(&mut self, _buf: &mut [u8]) -> ByteIOResult { Err(ByteIOError::NotImplemented) } - #[allow(unused_variables)] - fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult { + fn peek_buf(&mut self, _buf: &mut [u8]) -> ByteIOResult { Err(ByteIOError::NotImplemented) } diff --git a/nihav-core/src/io/codebook.rs b/nihav-core/src/io/codebook.rs index f533cb2..bd7bb1e 100644 --- a/nihav-core/src/io/codebook.rs +++ b/nihav-core/src/io/codebook.rs @@ -360,7 +360,6 @@ impl Codebook { } impl<'a, S: Copy> CodebookReader for BitReader<'a> { - #[allow(unused_variables)] fn read_cb(&mut self, cb: &Codebook) -> CodebookResult { let mut esc = true; let mut idx = 0; diff --git a/nihav-core/src/io/intcode.rs b/nihav-core/src/io/intcode.rs index 6b45b77..b66afdb 100644 --- a/nihav-core/src/io/intcode.rs +++ b/nihav-core/src/io/intcode.rs @@ -168,7 +168,6 @@ impl<'a> IntCodeReader for BitReader<'a> { UintCodeType::GammaP => read_gammap(self), } } - #[allow(unused_variables)] fn read_code_signed(&mut self, t: IntCodeType) -> BitReaderResult { let uval = match t { @@ -178,8 +177,8 @@ impl<'a> IntCodeReader for BitReader<'a> { IntCodeType::GammaP => read_gammap(self)?, }; match t { - IntCodeType::Golomb(m) => Ok(uval_to_sval0mp(uval)), - IntCodeType::Rice(k) => Ok(uval_to_sval0mp(uval)), + IntCodeType::Golomb(_) => Ok(uval_to_sval0mp(uval)), + IntCodeType::Rice(_) => Ok(uval_to_sval0mp(uval)), IntCodeType::Gamma => Ok(uval_to_sval0pm(uval)), IntCodeType::GammaP => Ok(uval_to_sval0pm(uval)), } diff --git a/nihav-core/src/lib.rs b/nihav-core/src/lib.rs index b6e1690..bb803bc 100644 --- a/nihav-core/src/lib.rs +++ b/nihav-core/src/lib.rs @@ -1,15 +1,10 @@ //! Core functionality of NihAV intended to be used by both crates implementing format support and users. #[cfg(feature="decoders")] #[allow(clippy::upper_case_acronyms)] -#[allow(clippy::cast_lossless)] -#[allow(clippy::identity_op)] #[allow(clippy::too_many_arguments)] -#[allow(clippy::unreadable_literal)] pub mod codecs; #[cfg(feature="compr")] -#[allow(clippy::manual_memcpy)] -#[allow(clippy::needless_range_loop)] pub mod compr; #[cfg(feature="muxers")] @@ -18,7 +13,6 @@ pub mod muxers; #[cfg(feature="demuxers")] pub mod demuxers; -#[allow(clippy::needless_range_loop)] #[allow(clippy::too_many_arguments)] pub mod formats; pub mod frame; @@ -28,10 +22,5 @@ pub mod options; pub mod refs; pub mod reorder; #[allow(clippy::upper_case_acronyms)] -#[allow(clippy::collapsible_if)] -#[allow(clippy::many_single_char_names)] -#[allow(clippy::needless_range_loop)] -#[allow(clippy::trivially_copy_pass_by_ref)] pub mod scale; -#[allow(clippy::unreadable_literal)] pub mod soundcvt; diff --git a/nihav-core/src/scale/palette/elbg.rs b/nihav-core/src/scale/palette/elbg.rs index 9a7d967..1ec3edd 100644 --- a/nihav-core/src/scale/palette/elbg.rs +++ b/nihav-core/src/scale/palette/elbg.rs @@ -77,8 +77,8 @@ impl ELBG { #[allow(dead_code)] pub fn new(initial_pal: &[[u8; 3]; 256]) -> Self { let mut clusters = Vec::with_capacity(256); - for i in 0..256 { - let pix = Pixel { r: initial_pal[i][0], g: initial_pal[i][1], b: initial_pal[i][2] }; + for clr in initial_pal.iter() { + let pix = Pixel { r: clr[0], g: clr[1], b: clr[2] }; let cluster = Cluster::new(pix); clusters.push(cluster); } @@ -231,9 +231,9 @@ impl ELBG { let mut do_elbg_step = true; while (iterations < 10) && (dist < prev_dist - prev_dist / 100) { prev_dist = dist; - for i in 0..256 { - old_cb[i] = self.clusters[i].centroid; - self.clusters[i].reset(); + for (cb, cluster) in old_cb.iter_mut().zip(self.clusters.iter_mut()) { + *cb = cluster.centroid; + cluster.reset(); } // put pixels into the nearest clusters indices.clear(); @@ -290,11 +290,11 @@ impl ELBG { let mut closest_idx = *low_idx; let mut closest_dist = std::u32::MAX; let low_centr = self.clusters[*low_idx].centroid; - for i in 0..256 {//low_u.iter() { - if i == *low_idx || used[i] { + for (i, (cluster, &used)) in self.clusters.iter().zip(used.iter()).enumerate() {//low_u.iter() { + if i == *low_idx || used { continue; } - let dist = self.clusters[i].centroid.dist(low_centr); + let dist = cluster.centroid.dist(low_centr); if closest_dist > dist { closest_dist = dist; closest_idx = i; @@ -329,8 +329,8 @@ impl ELBG { iterations += 1; } if dist < prev_dist { - for i in 0..256 { - old_cb[i] = self.clusters[i].centroid; + for (cb, cluster) in old_cb.iter_mut().zip(self.clusters.iter()) { + *cb = cluster.centroid; } } for i in 0..256 { diff --git a/nihav-core/src/scale/palette/mediancut.rs b/nihav-core/src/scale/palette/mediancut.rs index b8fb7d6..0910328 100644 --- a/nihav-core/src/scale/palette/mediancut.rs +++ b/nihav-core/src/scale/palette/mediancut.rs @@ -32,8 +32,8 @@ impl<'a> VQBox<'a> { for pix in arr.iter() { counts[idx(pix)] += 1; } - for i in 0..256 { - buckets.push(Vec::with_capacity(counts[i])); + for &count in counts.iter() { + buckets.push(Vec::with_capacity(count)); } for pix in arr.iter() { buckets[idx(pix)].push(*pix); diff --git a/nihav-core/src/scale/palette/neuquant.rs b/nihav-core/src/scale/palette/neuquant.rs index 0974500..2168b68 100644 --- a/nihav-core/src/scale/palette/neuquant.rs +++ b/nihav-core/src/scale/palette/neuquant.rs @@ -9,6 +9,7 @@ pub struct NeuQuantQuantiser { const SPECIAL_NODES: usize = 2; impl NeuQuantQuantiser { + #[allow(clippy::needless_range_loop)] pub fn new(factor: usize) -> Self { let mut weights = [[0.0; 3]; 256]; if SPECIAL_NODES > 1 { diff --git a/nihav-core/src/scale/palette/palettise.rs b/nihav-core/src/scale/palette/palettise.rs index 54467d3..ab82d2f 100644 --- a/nihav-core/src/scale/palette/palettise.rs +++ b/nihav-core/src/scale/palette/palettise.rs @@ -122,10 +122,10 @@ impl KDTree { } let mut min = [255u8; 3]; let mut max = [0u8; 3]; - for i in start..end { - for comp in 0..3 { - min[comp] = min[comp].min(pal[i][comp]); - max[comp] = max[comp].max(pal[i][comp]); + for clr in pal[start..end].iter() { + for ((mi, ma), &c) in min.iter_mut().zip(max.iter_mut()).zip(clr.iter()) { + *mi = (*mi).min(c); + *ma = (*ma).max(c); } } let dr = max[0] - min[0]; diff --git a/nihav-core/src/scale/repack.rs b/nihav-core/src/scale/repack.rs index 8cdb8ea..506e396 100644 --- a/nihav-core/src/scale/repack.rs +++ b/nihav-core/src/scale/repack.rs @@ -166,8 +166,8 @@ impl Kernel for UnpackKernel { if let Some(ref buf) = pic_in.get_vbuf() { let step = buf.get_info().get_format().elem_size as usize; let mut soff: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS]; - for i in 0..self.ncomps { - soff[i] = buf.get_info().get_format().get_chromaton(i).unwrap().get_offset() as usize; + for (i, off) in soff[..self.ncomps].iter_mut().enumerate() { + *off = buf.get_info().get_format().get_chromaton(i).unwrap().get_offset() as usize; } let (w, h) = buf.get_dimensions(0); let ioff = buf.get_offset(0); @@ -186,16 +186,18 @@ impl Kernel for UnpackKernel { if ychr.next_elem == 0 || usize::from(ychr.next_elem) == step { for src in sdata.chunks(istride).take(h) { for x in 0..w { - for i in 0..self.ncomps { - dst[offs[i] + x] = src[x * step + soff[i]]; + for (&doff, &soff) in offs[..self.ncomps].iter().zip(soff.iter()) { + dst[doff + x] = src[x * step + soff]; } } - for i in 0..self.ncomps { offs[i] += ostride[i]; } + for (off, &stride) in offs[..self.ncomps].iter_mut().zip(ostride.iter()) { + *off += stride; + } } } else { let mut steps: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS]; - for i in 0..self.ncomps { - steps[i] = buf.get_info().get_format().get_chromaton(i).unwrap().next_elem as usize; + for (i, step) in steps[..self.ncomps].iter_mut().enumerate() { + *step = buf.get_info().get_format().get_chromaton(i).unwrap().next_elem as usize; } for src in sdata.chunks(istride).take(h) { diff --git a/nihav-core/src/scale/scale/mod.rs b/nihav-core/src/scale/scale/mod.rs index 5d0776d..59bfc64 100644 --- a/nihav-core/src/scale/scale/mod.rs +++ b/nihav-core/src/scale/scale/mod.rs @@ -219,10 +219,10 @@ impl LanczosResize { let norm = std::f32::consts::PI * std::f32::consts::PI; let frac = (num as f32) / (den as f32); let a = order as f32; - for i in 0..(order * 2) { + for (i, coef) in coeffs[..order * 2].iter_mut().enumerate() { let x = frac - ((i as f32) + 1.0 - a); let fp = std::f32::consts::PI * x; - coeffs[i] = a * fp.sin() * (fp / a).sin() / (norm * x * x); + *coef = a * fp.sin() * (fp / a).sin() / (norm * x * x); } } fn create_cache(order: usize, den: usize) -> Vec> { -- 2.39.5