From 134f0b2d808cc68aa5b515dc32adc51da1c6a920 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Tue, 27 Jun 2023 00:09:22 +0200 Subject: [PATCH] Run clippy --- src/cmd.rs | 12 ++++-------- src/draw.rs | 8 ++++---- src/event.rs | 5 +---- src/execution.rs | 12 ++++-------- src/gl/mod.rs | 15 ++++++--------- src/image.rs | 2 +- src/session.rs | 29 +++++++++-------------------- src/view.rs | 4 ++-- src/view/resource.rs | 4 ++-- 9 files changed, 33 insertions(+), 58 deletions(-) diff --git a/src/cmd.rs b/src/cmd.rs index 5a74f5e3..2498052a 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -175,13 +175,9 @@ impl fmt::Display for Command { Self::Noop => write!(f, "No-op"), Self::PaletteAdd(c) => write!(f, "Add {color} to palette", color = c), Self::PaletteClear => write!(f, "Clear palette"), - Self::PaletteGradient(cs, ce, n) => write!( - f, - "Create {} colors gradient from {} to {}", - number = n, - colorstart = cs, - colorend = ce - ), + Self::PaletteGradient(cs, ce, n) => { + write!(f, "Create {n} colors gradient from {cs} to {ce}") + } Self::PaletteSample => write!(f, "Sample palette from view"), Self::PaletteSort => write!(f, "Sort palette colors"), Self::Pan(x, 0) if *x > 0 => write!(f, "Pan workspace right"), @@ -435,7 +431,7 @@ impl From for f32 { impl From for f64 { fn from(other: Value) -> f64 { if let Value::F64(x) = other { - return x as f64; + return x; } panic!("expected {:?} to be a `f64`", other); } diff --git a/src/draw.rs b/src/draw.rs index 79ceece6..4dcb4ed4 100644 --- a/src/draw.rs +++ b/src/draw.rs @@ -749,14 +749,14 @@ pub fn draw_view_composites(session: &Session, v: &View) -> sprite2d::Batc pub fn draw_help(session: &Session, text: &mut TextBatch, shape: &mut shape2d::Batch) { shape.add(Shape::Rectangle( - Rect::origin(session.width as f32, session.height as f32), + Rect::origin(session.width, session.height), ZDepth(0.0), Rotation::ZERO, Stroke::new(1., color::RED.into()), Fill::Empty, )); shape.add(Shape::Rectangle( - Rect::origin(session.width as f32, session.height as f32), + Rect::origin(session.width, session.height), self::HELP_LAYER, Rotation::ZERO, Stroke::NONE, @@ -773,7 +773,7 @@ pub fn draw_help(session: &Session, text: &mut TextBatch, shape: &mut shape2d::B platform::Key::Escape, ), left_margin, - session.height as f32 - self::MARGIN - self::LINE_HEIGHT, + session.height - self::MARGIN - self::LINE_HEIGHT, self::HELP_LAYER, color::LIGHT_GREY, TextAlign::Left, @@ -859,7 +859,7 @@ pub fn draw_help(session: &Session, text: &mut TextBatch, shape: &mut shape2d::B } for (i, l) in session.help().iter().enumerate() { - let y = session.height as f32 - (i + 4) as f32 * self::LINE_HEIGHT; + let y = session.height - (i + 4) as f32 * self::LINE_HEIGHT; text.add( l, diff --git a/src/event.rs b/src/event.rs index 5b31cd89..2993462d 100644 --- a/src/event.rs +++ b/src/event.rs @@ -125,10 +125,7 @@ impl FromStr for Event { .followed_by(end()) .parse(p) .map_err(|(e, _)| e)?; - Ok(( - Event::CursorMoved(platform::LogicalPosition::new(x as f64, y as f64)), - p, - )) + Ok((Event::CursorMoved(platform::LogicalPosition::new(x, y)), p)) } "keyboard/input" => { let ((k, s), p) = parser::param::() diff --git a/src/execution.rs b/src/execution.rs index 0d438af9..6558b54a 100644 --- a/src/execution.rs +++ b/src/execution.rs @@ -48,7 +48,7 @@ impl DigestState { let mut frames = Vec::new(); let path = path.as_ref(); - match File::open(&path) { + match File::open(path) { Ok(f) => { let r = io::BufReader::new(f); for line in r.lines() { @@ -97,8 +97,10 @@ pub enum ExecutionMode { /// Execution mode. Controls whether the session is playing or recording /// commands. // TODO: Make this a `struct` and have `ExecutionMode`. +#[derive(Default)] pub enum Execution { /// Normal execution. User inputs are processed normally. + #[default] Normal, /// Recording user inputs to log. Recording { @@ -198,7 +200,7 @@ impl Execution { } => { let mut frames = Vec::new(); - match File::open(&path) { + match File::open(path) { Ok(f) => { let r = io::BufReader::new(f); for line in r.lines() { @@ -372,12 +374,6 @@ impl Execution { } } -impl Default for Execution { - fn default() -> Self { - Execution::Normal - } -} - pub struct GifRecorder { width: u16, height: u16, diff --git a/src/gl/mod.rs b/src/gl/mod.rs index 4337c6fa..c1cc93c4 100644 --- a/src/gl/mod.rs +++ b/src/gl/mod.rs @@ -1015,14 +1015,13 @@ impl Renderer { .map_err(Error::Texture)?; } ViewOp::Yank(src) => { - let (_, pixels) = v.layer.get_snapshot_rect(&src.map(|n| n as i32)).unwrap(); + let (_, pixels) = v.layer.get_snapshot_rect(&src.map(|n| n)).unwrap(); let (w, h) = (src.width() as u32, src.height() as u32); let [paste_w, paste_h] = self.paste.size(); if paste_w != w || paste_h != h { - self.paste = - Texture::new(&mut self.ctx, [w as u32, h as u32], 0, self::SAMPLER) - .map_err(Error::Texture)?; + self.paste = Texture::new(&mut self.ctx, [w, h], 0, self::SAMPLER) + .map_err(Error::Texture)?; } let body = util::align_u8(&pixels); @@ -1031,15 +1030,13 @@ impl Renderer { .map_err(Error::Texture)?; } ViewOp::Flip(src, dir) => { - let (_, mut pixels) = - v.layer.get_snapshot_rect(&src.map(|n| n as i32)).unwrap(); + let (_, mut pixels) = v.layer.get_snapshot_rect(&src.map(|n| n)).unwrap(); let (w, h) = (src.width() as u32, src.height() as u32); let [paste_w, paste_h] = self.paste.size(); if paste_w != w || paste_h != h { - self.paste = - Texture::new(&mut self.ctx, [w as u32, h as u32], 0, self::SAMPLER) - .map_err(Error::Texture)?; + self.paste = Texture::new(&mut self.ctx, [w, h], 0, self::SAMPLER) + .map_err(Error::Texture)?; } match dir { diff --git a/src/image.rs b/src/image.rs index e6ee3128..977d905c 100644 --- a/src/image.rs +++ b/src/image.rs @@ -112,7 +112,7 @@ pub fn read(reader: R) -> io::Result<(Vec, u32, u32)> { )); } - let (width, height) = (info.width as u32, info.height as u32); + let (width, height) = (info.width, info.height); let mut buffer: Vec = vec![0; info.buffer_size()]; reader diff --git a/src/session.rs b/src/session.rs index b59c4799..a9da6135 100644 --- a/src/session.rs +++ b/src/session.rs @@ -1,5 +1,5 @@ #![allow(clippy::needless_collect)] -///! Session +//! Session use crate::autocomplete::FileCompleter; use crate::brush::*; use crate::cmd::{self, Command, CommandLine, KeyMapping, Op, Value}; @@ -68,9 +68,10 @@ pub type SessionCoords = Point; /// An editing mode the `Session` can be in. /// Some of these modes are inspired by vi. -#[derive(Eq, PartialEq, Copy, Clone, Debug)] +#[derive(Eq, PartialEq, Copy, Clone, Debug, Default)] pub enum Mode { /// Allows the user to paint pixels. + #[default] Normal, /// Allows pixels to be selected, copied and manipulated visually. Visual(VisualState), @@ -83,12 +84,6 @@ pub enum Mode { Help, } -impl Default for Mode { - fn default() -> Self { - Mode::Normal - } -} - impl fmt::Display for Mode { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { @@ -227,9 +222,10 @@ pub enum State { } /// An editing tool. -#[derive(PartialEq, Eq, Debug, Clone)] +#[derive(PartialEq, Eq, Debug, Clone, Default)] pub enum Tool { /// The standard drawing tool. + #[default] Brush, /// Used for filling enclosed regions with color. FloodFill, @@ -239,12 +235,6 @@ pub enum Tool { Pan(PanState), } -impl Default for Tool { - fn default() -> Self { - Tool::Brush - } -} - #[derive(PartialEq, Eq, Debug, Clone, Copy)] pub enum PanState { Panning, @@ -1545,10 +1535,10 @@ impl Session { let view = self.view(id); let delay = time::Duration::from_millis(self.settings["animation/delay"].to_u64()); - view.save_gif(&path, delay, &palette, scale)? + view.save_gif(path, delay, &palette, scale)? } - "svg" => self.view(id).save_svg(&path, scale)?, - "png" => self.view(id).save_png(&path, scale)?, + "svg" => self.view(id).save_svg(path, scale)?, + "png" => self.view(id).save_png(path, scale)?, _ => { return Err(io::Error::new( io::ErrorKind::InvalidInput, @@ -2190,7 +2180,7 @@ impl Session { let path = path.as_ref(); debug!("source: {}", path.display()); - File::open(&path) + File::open(path) .or_else(|_| File::open(self.proj_dirs.config_dir().join(path))) .and_then(|f| self.source_reader(io::BufReader::new(f), path)) .map_err(|e| { @@ -2678,7 +2668,6 @@ impl Session { } } } - #[allow(mutable_borrow_reservation_conflict)] Command::Toggle(ref k) => match self.settings.get(k) { Some(Value::Bool(b)) => self.command(Command::Set(k.clone(), Value::Bool(!b))), Some(_) => { diff --git a/src/view.rs b/src/view.rs index 45ac10af..9bb93277 100644 --- a/src/view.rs +++ b/src/view.rs @@ -73,7 +73,7 @@ impl ViewExtent { /// Compute the frame index, given a point. /// Warning: can underflow. pub fn to_frame(self, p: ViewCoords) -> usize { - (p.x / (self.fw as u32)) as usize + (p.x / self.fw) as usize } } @@ -550,7 +550,7 @@ impl View { format!("\"{}\" already exists", path.display()), )); } - let (e_id, _) = self.save(rect, &path)?; + let (e_id, _) = self.save(rect, path)?; Ok(e_id) } diff --git a/src/view/resource.rs b/src/view/resource.rs index bd0711fd..0d2d322d 100644 --- a/src/view/resource.rs +++ b/src/view/resource.rs @@ -216,8 +216,8 @@ impl ViewResource { { // Convert animation strip into discrete frames for gif encoder. - let nrows = fh as usize * nframes; - let row_nbytes = fw as usize; + let nrows = fh * nframes; + let row_nbytes = fw; for i in 0..nrows { let offset = i * row_nbytes;