From: Kostya Shishkov Date: Tue, 16 Jun 2020 10:08:29 +0000 (+0200) Subject: core: fix or silence clippy warnings X-Git-Url: https://git.nihav.org/?a=commitdiff_plain;h=b36f412c24813b14cb2b1f8fd151863e2a49c1e2;p=nihav.git core: fix or silence clippy warnings --- diff --git a/nihav-core/src/compr/deflate.rs b/nihav-core/src/compr/deflate.rs index 1b3dc68..89e8b68 100644 --- a/nihav-core/src/compr/deflate.rs +++ b/nihav-core/src/compr/deflate.rs @@ -72,6 +72,7 @@ impl CodebookDescReader for FixedLenCodeReader { else if idx < 280 { 7 } else { 8 } } + #[allow(clippy::identity_op)] fn code(&mut self, idx: usize) -> u32 { let base = idx as u32; let bits = self.bits(idx); @@ -355,7 +356,7 @@ impl Inflate { ///! [`DecompressError::ShortData`]: ../enum.DecompressError.html#variant.ShortData ///! [`DecompressError::OutputFull`]: ../enum.DecompressError.html#variant.OutputFull pub fn decompress_data(&mut self, src: &[u8], dst: &mut [u8], continue_block: bool) -> DecompressResult { - if src.len() == 0 || dst.len() == 0 { + if src.is_empty() || dst.is_empty() { return Err(DecompressError::InvalidArgument); } let mut csrc = if !continue_block { @@ -597,16 +598,15 @@ impl Inflate { self.state = InflateState::End; return Err(DecompressError::InvalidHeader); } - let rpt; - if mode == 0 { - if self.cur_len_idx == 0 { - self.state = InflateState::End; - return Err(DecompressError::InvalidHeader); - } - rpt = self.all_lengths[self.cur_len_idx - 1]; - } else { - rpt = 0; - } + let rpt = if mode == 0 { + if self.cur_len_idx == 0 { + self.state = InflateState::End; + return Err(DecompressError::InvalidHeader); + } + self.all_lengths[self.cur_len_idx - 1] + } else { + 0 + }; for _ in 0..len { self.all_lengths[self.cur_len_idx] = rpt; self.cur_len_idx += 1; @@ -720,6 +720,12 @@ impl Inflate { } } +impl Default for Inflate { + fn default() -> Self { + Self::new() + } +} + fn lengths_to_codes(lens: &[u8], codes: &mut [ShortCodebookDesc]) -> DecompressResult<()> { let mut bits = [0u32; 32]; let mut pfx = [0u32; 33]; @@ -757,6 +763,7 @@ struct GzipCRC32 { } impl GzipCRC32 { + #[allow(clippy::unreadable_literal)] fn new() -> Self { let mut tab = [0u32; 256]; for i in 0..256 { diff --git a/nihav-core/src/frame.rs b/nihav-core/src/frame.rs index 6386d6f..8944f45 100644 --- a/nihav-core/src/frame.rs +++ b/nihav-core/src/frame.rs @@ -927,8 +927,8 @@ impl NATimeInfo { /// Converts time in given scale into timestamp in given base. pub fn time_to_ts(time: u64, base: u64, tb_num: u32, tb_den: u32) -> u64 { - let tb_num = tb_num as u64; - let tb_den = tb_den as u64; + let tb_num = u64::from(tb_num); + let tb_den = u64::from(tb_den); let tmp = time.checked_mul(tb_num); if let Some(tmp) = tmp { tmp / base / tb_den @@ -949,8 +949,8 @@ impl NATimeInfo { } /// Converts timestamp in given base into time in given scale. pub fn ts_to_time(ts: u64, base: u64, tb_num: u32, tb_den: u32) -> u64 { - let tb_num = tb_num as u64; - let tb_den = tb_den as u64; + let tb_num = u64::from(tb_num); + let tb_den = u64::from(tb_den); let tmp = ts.checked_mul(base); if let Some(tmp) = tmp { let tmp2 = tmp.checked_mul(tb_num); diff --git a/nihav-core/src/io/byteio.rs b/nihav-core/src/io/byteio.rs index ff899fc..7da5e22 100644 --- a/nihav-core/src/io/byteio.rs +++ b/nihav-core/src/io/byteio.rs @@ -200,6 +200,7 @@ write_int_func!(write_u64le, u64, 8, to_le); /// # Ok(()) /// # } /// ```` +#[allow(clippy::identity_op)] pub fn write_u24be(dst: &mut [u8], val: u32) -> ByteIOResult<()> { if dst.len() < 3 { return Err(ByteIOError::WriteError); } dst[0] = (val >> 16) as u8; @@ -208,6 +209,7 @@ pub fn write_u24be(dst: &mut [u8], val: u32) -> ByteIOResult<()> { Ok(()) } /// Writes 24-bit little-endian integer to the provided buffer. +#[allow(clippy::identity_op)] pub fn write_u24le(dst: &mut [u8], val: u32) -> ByteIOResult<()> { if dst.len() < 3 { return Err(ByteIOError::WriteError); } dst[0] = (val >> 0) as u8; @@ -807,9 +809,7 @@ impl<'a> ByteIO for MemoryWriter<'a> { fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> { if self.pos + buf.len() > self.buf.len() { return Err(ByteIOError::WriteError); } - for i in 0..buf.len() { - self.buf[self.pos + i] = buf[i]; - } + self.buf[self.pos..][..buf.len()].copy_from_slice(buf); self.pos += buf.len(); Ok(()) } @@ -887,9 +887,7 @@ impl<'a> ByteIO for GrowableMemoryWriter<'a> { if self.pos + buf.len() > self.buf.len() { self.buf.resize(self.pos + buf.len(), 0); } - for i in 0..buf.len() { - self.buf[self.pos + i] = buf[i]; - } + self.buf[self.pos..][..buf.len()].copy_from_slice(buf); self.pos += buf.len(); Ok(()) } diff --git a/nihav-core/src/io/codebook.rs b/nihav-core/src/io/codebook.rs index d835ce3..384a743 100644 --- a/nihav-core/src/io/codebook.rs +++ b/nihav-core/src/io/codebook.rs @@ -113,6 +113,7 @@ pub struct ShortCodebookDesc { /// /// [`ShortCodebookDescReader`]: ./struct.ShortCodebookDescReader.html /// [`TableCodebookDescReader`]: ./struct.TableCodebookDescReader.html +#[allow(clippy::len_without_is_empty)] pub trait CodebookDescReader { /// Returns the codeword length for the provided index. fn bits(&mut self, idx: usize) -> u8; diff --git a/nihav-core/src/lib.rs b/nihav-core/src/lib.rs index 303fb24..d3104a4 100644 --- a/nihav-core/src/lib.rs +++ b/nihav-core/src/lib.rs @@ -7,6 +7,8 @@ pub mod codecs; #[cfg(feature="compr")] +#[allow(clippy::manual_memcpy)] +#[allow(clippy::needless_range_loop)] pub mod compr; #[cfg(feature="muxers")] @@ -23,5 +25,10 @@ pub mod io; pub mod options; pub mod refs; pub mod reorder; +#[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/options.rs b/nihav-core/src/options.rs index ea5b329..96e093e 100644 --- a/nihav-core/src/options.rs +++ b/nihav-core/src/options.rs @@ -9,20 +9,20 @@ use std::sync::Arc; use std::fmt; /// Common name for keyframe interval option. -pub const KEYFRAME_OPTION: &'static str = "key_int"; +pub const KEYFRAME_OPTION: &str = "key_int"; /// Common description for keyframe interval option. -pub const KEYFRAME_OPTION_DESC: &'static str = "Keyframe interval (0 - automatic)"; +pub const KEYFRAME_OPTION_DESC: &str = "Keyframe interval (0 - automatic)"; /// Common name for frame skipping mode. -pub const FRAME_SKIP_OPTION: &'static str = "frame_skip"; +pub const FRAME_SKIP_OPTION: &str = "frame_skip"; /// Common description for frame skipping mode. -pub const FRAME_SKIP_OPTION_DESC: &'static str = "Frame skipping mode"; +pub const FRAME_SKIP_OPTION_DESC: &str = "Frame skipping mode"; /// Frame skipping option value for no skipped frames. -pub const FRAME_SKIP_OPTION_VAL_NONE: &'static str = "none"; +pub const FRAME_SKIP_OPTION_VAL_NONE: &str = "none"; /// Frame skipping option value for decoding only keyframes. -pub const FRAME_SKIP_OPTION_VAL_KEYFRAME: &'static str = "keyframes"; +pub const FRAME_SKIP_OPTION_VAL_KEYFRAME: &str = "keyframes"; /// Frame skipping option value for decoding only intra frames. -pub const FRAME_SKIP_OPTION_VAL_INTRA: &'static str = "intra"; +pub const FRAME_SKIP_OPTION_VAL_INTRA: &str = "intra"; /// A list specifying option parsing and validating errors. #[derive(Clone,Copy,Debug,PartialEq)] @@ -70,17 +70,17 @@ pub struct NAOptionDefinition { impl NAOptionDefinition { /// Tries to parse input string(s) as an option and returns new option and number of arguments used (1 or 2) on success. - pub fn parse(&self, name: &String, value: Option<&String>) -> OptionResult<(NAOption, usize)> { + pub fn parse(&self, name: &str, value: Option<&String>) -> OptionResult<(NAOption, usize)> { let no_name = "no".to_owned() + self.name; let opt_no_name = "--no".to_owned() + self.name; - if name == &no_name || name == &opt_no_name { + if name == no_name || name == opt_no_name { match self.opt_type { NAOptionDefinitionType::Bool => return Ok((NAOption { name: self.name, value: NAValue::Bool(false) }, 1)), _ => return Err(OptionError::InvalidFormat), }; } let opt_name = "--".to_owned() + self.name; - if self.name != name && &opt_name != name { + if self.name != name && opt_name != name { return Err(OptionError::WrongName); } match self.opt_type { @@ -293,17 +293,17 @@ mod test { #[test] fn test_option_parsing() { let mut def = NAOptionDefinition { name: "option", description: "", opt_type: NAOptionDefinitionType::Float(None, None) }; - assert_eq!(def.parse(&"--option".to_string(), None), Err(OptionError::ParseError)); - assert_eq!(def.parse(&"--nooption".to_string(), None), Err(OptionError::InvalidFormat)); - assert_eq!(def.parse(&"--option".to_string(), Some(&"42".to_string())), + assert_eq!(def.parse("--option", None), Err(OptionError::ParseError)); + assert_eq!(def.parse("--nooption", None), Err(OptionError::InvalidFormat)); + assert_eq!(def.parse("--option", Some(&"42".to_string())), Ok((NAOption{name:"option",value: NAValue::Float(42.0)}, 2))); def.opt_type = NAOptionDefinitionType::Float(None, Some(40.0)); - assert_eq!(def.parse(&"--option".to_string(), Some(&"42".to_string())), + assert_eq!(def.parse("--option", Some(&"42".to_string())), Err(OptionError::InvalidValue)); let def = NAOptionDefinition { name: "option", description: "", opt_type: NAOptionDefinitionType::Bool }; - assert_eq!(def.parse(&"option".to_string(), None), + assert_eq!(def.parse("option", None), Ok((NAOption{name: "option", value: NAValue::Bool(true) }, 1))); - assert_eq!(def.parse(&"nooption".to_string(), None), + assert_eq!(def.parse("nooption", None), Ok((NAOption{name: "option", value: NAValue::Bool(false) }, 1))); } } diff --git a/nihav-core/src/reorder.rs b/nihav-core/src/reorder.rs index 83896df..625e759 100644 --- a/nihav-core/src/reorder.rs +++ b/nihav-core/src/reorder.rs @@ -32,6 +32,12 @@ impl NoReorderer { } } +impl Default for NoReorderer { + fn default() -> Self { + Self::new() + } +} + impl FrameReorderer for NoReorderer { fn add_frame(&mut self, fref: NAFrameRef) -> bool { if self.fref.is_none() { diff --git a/nihav-core/src/scale/colorcvt.rs b/nihav-core/src/scale/colorcvt.rs index 056c050..0343bdd 100644 --- a/nihav-core/src/scale/colorcvt.rs +++ b/nihav-core/src/scale/colorcvt.rs @@ -369,7 +369,7 @@ println!(" [intermediate format {}]", df); let mut voff = sbuf.get_offset(2); let src = sbuf.get_data(); let dst = dbuf.get_data_mut().unwrap(); - if self.yscale.len() > 0 { + if !self.yscale.is_empty() { for y in 0..h { for x in 0..w { let y = self.yscale[src[yoff + x] as usize]; @@ -432,7 +432,7 @@ impl YuvToRgb { let mut yoff = sbuf.get_offset(0); let src = sbuf.get_data(); let dst = dbuf.get_data_mut().unwrap(); - if self.yscale.len() > 0 { + if !self.yscale.is_empty() { for _y in 0..h { for x in 0..w { let y = self.yscale[src[yoff + x] as usize]; diff --git a/nihav-core/src/scale/mod.rs b/nihav-core/src/scale/mod.rs index e66d393..2192b18 100644 --- a/nihav-core/src/scale/mod.rs +++ b/nihav-core/src/scale/mod.rs @@ -21,6 +21,7 @@ mod kernel; mod colorcvt; mod repack; +#[allow(clippy::module_inception)] mod scale; mod palette; diff --git a/nihav-core/src/scale/palette/elbg.rs b/nihav-core/src/scale/palette/elbg.rs index 0926901..300b3f1 100644 --- a/nihav-core/src/scale/palette/elbg.rs +++ b/nihav-core/src/scale/palette/elbg.rs @@ -8,7 +8,7 @@ impl RNG { fn new() -> Self { Self { seed: 0x1234 } } fn next(&mut self) -> u8 { if (self.seed & 0x8000) != 0 { - self.seed = (self.seed & 0x7FFF) * 2 ^ 0x1B2B; + self.seed = ((self.seed & 0x7FFF) * 2) ^ 0x1B2B; } else { self.seed <<= 1; } @@ -198,6 +198,7 @@ impl ELBG { clu1.calc_dist(); clu0.dist + clu1.dist } + #[allow(clippy::cyclomatic_complexity)] pub fn quantise(&mut self, src: &[Pixel], dst: &mut [[u8; 3]; 256]) { if src.len() < 3 { return; @@ -284,7 +285,7 @@ impl ELBG { if do_elbg_step { do_elbg_step = false; for low_idx in low_u.iter() { - if high_u.len() == 0 { + if high_u.is_empty() { break; } let high_idx_idx = (rng.next() as usize) % high_u.len(); diff --git a/nihav-core/src/scale/palette/neuquant.rs b/nihav-core/src/scale/palette/neuquant.rs index 7b19eb5..73d2a3a 100644 --- a/nihav-core/src/scale/palette/neuquant.rs +++ b/nihav-core/src/scale/palette/neuquant.rs @@ -39,7 +39,7 @@ impl NeuQuantQuantiser { let mut range = 0; let sqradius = (radius * radius) as f64; while (idx0 < high) || (idx1 > low) { - let sqrng = (range * range) as f64; + let sqrng = f64::from(range * range); let a = alpha * (sqradius - sqrng) / sqradius; range += 1; if idx0 < high { @@ -85,7 +85,7 @@ impl NeuQuantQuantiser { pub fn learn(&mut self, src: &[Pixel]) { let mut bias_radius = (256 / 8) << 6; let alphadec = (30 + (self.factor - 1) / 3) as f64; - let initial_alpha = (1 << 10) as f64; + let initial_alpha = f64::from(1 << 10); let npixels = src.len(); @@ -105,7 +105,7 @@ impl NeuQuantQuantiser { } for i in 0..samples { - let clr = [src[pos].r as f64, src[pos].g as f64, src[pos].b as f64]; + let clr = [f64::from(src[pos].r), f64::from(src[pos].g), f64::from(src[pos].b)]; let idx = self.find_node(&clr); if idx >= SPECIAL_NODES { let new_alpha = alphadec / initial_alpha; diff --git a/nihav-core/src/scale/repack.rs b/nihav-core/src/scale/repack.rs index faa5fe7..f0e5956 100644 --- a/nihav-core/src/scale/repack.rs +++ b/nihav-core/src/scale/repack.rs @@ -143,9 +143,7 @@ impl Kernel for UnpackKernel { chr.push(Some(dchr)); } let mut df = in_fmt.fmt; - for i in 0..self.ncomps { - df.comp_info[i] = chr[i]; - } + df.comp_info[..self.ncomps].clone_from_slice(&chr[..self.ncomps]); df.palette = false; println!(" [intermediate format {}]", df); let res = alloc_video_buffer(NAVideoInfo::new(in_fmt.width, in_fmt.height, false, df), 3); @@ -253,9 +251,7 @@ impl Kernel for DepalKernel { } let mut df = in_fmt.fmt; df.palette = false; - for i in 0..self.ncomps { - df.comp_info[i] = chr[i]; - } + df.comp_info[..self.ncomps].clone_from_slice(&chr[..self.ncomps]); println!(" [intermediate format {}]", df); let res = alloc_video_buffer(NAVideoInfo::new(in_fmt.width, in_fmt.height, false, df), 3); if res.is_err() { return Err(ScaleError::AllocError); } diff --git a/nihav-core/src/soundcvt/mod.rs b/nihav-core/src/soundcvt/mod.rs index 6864f7e..b218e4a 100644 --- a/nihav-core/src/soundcvt/mod.rs +++ b/nihav-core/src/soundcvt/mod.rs @@ -37,10 +37,10 @@ impl ChannelOp { } } -fn apply_channel_op(ch_op: &ChannelOp, src: &Vec, dst: &mut Vec) { +fn apply_channel_op(ch_op: &ChannelOp, src: &[T], dst: &mut Vec) { match *ch_op { ChannelOp::Passthrough => { - dst.copy_from_slice(src.as_slice()); + dst.copy_from_slice(src); }, ChannelOp::Reorder(ref reorder) => { for (out, idx) in dst.iter_mut().zip(reorder.iter()) { @@ -51,7 +51,7 @@ fn apply_channel_op(ch_op: &ChannelOp, src: &Vec, dst: &mut Vec) { }; } -fn remix_i32(ch_op: &ChannelOp, src: &Vec, dst: &mut Vec) { +fn remix_i32(ch_op: &ChannelOp, src: &[i32], dst: &mut Vec) { if let ChannelOp::Remix(ref remix_mat) = ch_op { let sch = src.len(); for (out, coeffs) in dst.iter_mut().zip(remix_mat.chunks(sch)) { @@ -70,7 +70,7 @@ fn remix_i32(ch_op: &ChannelOp, src: &Vec, dst: &mut Vec) { } } -fn remix_f32(ch_op: &ChannelOp, src: &Vec, dst: &mut Vec) { +fn remix_f32(ch_op: &ChannelOp, src: &[f32], dst: &mut Vec) { if let ChannelOp::Remix(ref remix_mat) = ch_op { let sch = src.len(); for (out, coeffs) in dst.iter_mut().zip(remix_mat.chunks(sch)) { @@ -97,13 +97,13 @@ impl FromFmt for u8 { fn cvt_from(val: u8) -> u8 { val } } impl FromFmt for i16 { - fn cvt_from(val: u8) -> i16 { ((val as i16) - 128).wrapping_mul(0x101) } + fn cvt_from(val: u8) -> i16 { (i16::from(val) - 128).wrapping_mul(0x101) } } impl FromFmt for i32 { - fn cvt_from(val: u8) -> i32 { ((val as i32) - 128).wrapping_mul(0x01010101) } + fn cvt_from(val: u8) -> i32 { (i32::from(val) - 128).wrapping_mul(0x01010101) } } impl FromFmt for f32 { - fn cvt_from(val: u8) -> f32 { ((val as f32) - 128.0) / 128.0 } + fn cvt_from(val: u8) -> f32 { (f32::from(val) - 128.0) / 128.0 } } impl FromFmt for u8 { @@ -113,10 +113,10 @@ impl FromFmt for i16 { fn cvt_from(val: i16) -> i16 { val } } impl FromFmt for i32 { - fn cvt_from(val: i16) -> i32 { (val as i32).wrapping_mul(0x10001) } + fn cvt_from(val: i16) -> i32 { i32::from(val).wrapping_mul(0x10001) } } impl FromFmt for f32 { - fn cvt_from(val: i16) -> f32 { (val as f32) / 32768.0 } + fn cvt_from(val: i16) -> f32 { f32::from(val) / 32768.0 } } impl FromFmt for u8 { @@ -233,8 +233,8 @@ impl SampleReader for PackedSampleReader<'_> { } trait SampleWriter { - fn store_samples_i32(&mut self, pos: usize, src: &Vec); - fn store_samples_f32(&mut self, pos: usize, src: &Vec); + fn store_samples_i32(&mut self, pos: usize, src: &[i32]); + fn store_samples_f32(&mut self, pos: usize, src: &[f32]); } struct GenericSampleWriter<'a, T:Copy> { @@ -243,14 +243,14 @@ struct GenericSampleWriter<'a, T:Copy> { } impl<'a, T:Copy+FromFmt+FromFmt> SampleWriter for GenericSampleWriter<'a, T> { - fn store_samples_i32(&mut self, pos: usize, src: &Vec) { + fn store_samples_i32(&mut self, pos: usize, src: &[i32]) { let mut off = pos; for el in src.iter() { self.data[off] = (*el).cvt_into(); off += self.stride; } } - fn store_samples_f32(&mut self, pos: usize, src: &Vec) { + fn store_samples_f32(&mut self, pos: usize, src: &[f32]) { let mut off = pos; for el in src.iter() { self.data[off] = (*el).cvt_into(); @@ -272,7 +272,7 @@ impl<'a> PackedSampleWriter<'a> { Self { data, fmt, bpp } } - fn store_samples(&mut self, pos: usize, src: &Vec) where u8: FromFmt, i16: FromFmt, i32: FromFmt, f32: FromFmt { + fn store_samples(&mut self, pos: usize, src: &[T]) where u8: FromFmt, i16: FromFmt, i32: FromFmt, f32: FromFmt { let mut offset = pos * self.bpp * src.len(); for el in src.iter() { let dst = &mut self.data[offset..]; @@ -293,8 +293,8 @@ impl<'a> PackedSampleWriter<'a> { match (self.bpp, self.fmt.be) { (4, true) => write_f32be(dst, f32::cvt_from(*el)).unwrap(), (4, false) => write_f32le(dst, f32::cvt_from(*el)).unwrap(), - (8, true) => write_f64be(dst, f32::cvt_from(*el) as f64).unwrap(), - (8, false) => write_f64le(dst, f32::cvt_from(*el) as f64).unwrap(), + (8, true) => write_f64be(dst, f64::from(f32::cvt_from(*el))).unwrap(), + (8, false) => write_f64le(dst, f64::from(f32::cvt_from(*el))).unwrap(), (_, _) => unreachable!(), }; } @@ -304,10 +304,10 @@ impl<'a> PackedSampleWriter<'a> { } impl SampleWriter for PackedSampleWriter<'_> { - fn store_samples_i32(&mut self, pos: usize, src: &Vec) { + fn store_samples_i32(&mut self, pos: usize, src: &[i32]) { self.store_samples(pos, src); } - fn store_samples_f32(&mut self, pos: usize, src: &Vec) { + fn store_samples_f32(&mut self, pos: usize, src: &[f32]) { self.store_samples(pos, src); } } @@ -362,7 +362,7 @@ Result { return Ok(src.clone()); } - let ret = alloc_audio_buffer(dst_info.clone(), nsamples, dst_chmap.clone()); + let ret = alloc_audio_buffer(*dst_info, nsamples, dst_chmap.clone()); if ret.is_err() { return Err(SoundConvertError::AllocError); }