From 1c6a53602659e5e0659d6dc092ffc8ee9a35b584 Mon Sep 17 00:00:00 2001 From: Bendik Aagaard Lynghaug Date: Sat, 3 Oct 2026 09:20:25 +0000 Subject: [PATCH] clippy: clean up all warnings Mostly `cargo clippy --fix`: needless borrows, redundant field names, match -> if let, io::Error::other, Option::filter/map, let chains, c"" literals. By hand: drop unused impl lifetimes, a type alias for the font preload map, Iterator::find in next_buffer, contains_key, and allow new_ret_no_self where constructors intentionally return the boxed/generic wrapper type. No functional change; tests and release build unchanged. Co-Authored-By: Claude Fable 5.1 --- src/buffer.rs | 17 +++------ src/config.rs | 20 +++++------ src/draw.rs | 42 +++++++++++----------- src/event.rs | 2 +- src/fonts.rs | 5 ++- src/keyboard/mod.rs | 2 +- src/keyboard/state.rs | 20 +++++------ src/main.rs | 29 +++++++--------- src/state.rs | 73 ++++++++++++++++----------------------- src/utils/desktop.rs | 4 +-- src/utils/inish.rs | 6 ++-- src/widgets/backlight.rs | 11 +++--- src/widgets/bar_widget.rs | 15 ++++---- src/widgets/battery.rs | 2 ++ src/widgets/calendar.rs | 26 ++++++-------- src/widgets/clock.rs | 11 +++--- src/widgets/date.rs | 9 ++--- src/widgets/launcher.rs | 21 +++++------ src/widgets/layout.rs | 15 ++++---- src/widgets/pulseaudio.rs | 13 +++---- 20 files changed, 153 insertions(+), 190 deletions(-) diff --git a/src/buffer.rs b/src/buffer.rs index 6ff7164..02febc0 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -1,5 +1,4 @@ use std::{ - ffi::CStr, fs::File, io, os::unix::io::{FromRawFd, RawFd}, @@ -30,10 +29,7 @@ fn create_shm_fd() -> io::Result { // Only try memfd on linux #[cfg(target_os = "linux")] loop { - match memfd::memfd_create( - CStr::from_bytes_with_nul(b"wldash\0").unwrap(), - memfd::MemFdCreateFlag::MFD_CLOEXEC, - ) { + match memfd::memfd_create(c"wldash", memfd::MemFdCreateFlag::MFD_CLOEXEC) { Ok(fd) => return Ok(fd), Err(Errno::EINTR) => continue, Err(Errno::ENOSYS) => break, @@ -103,9 +99,9 @@ impl ShmBuffer { let mem_fd = create_shm_fd()?; let file = unsafe { File::from_raw_fd(mem_fd) }; file.set_len((height * stride) as u64)?; - let pool = shm.create_pool(mem_fd, height * stride, &qh, ()); + let pool = shm.create_pool(mem_fd, height * stride, qh, ()); let mmap = unsafe { MmapMut::map_mut(&file)? }; - let buffer = pool.create_buffer(0, width, height, stride, format, &qh, ()); + let buffer = pool.create_buffer(0, width, height, stride, format, qh, ()); Ok(ShmBuffer { refcnt: 0, @@ -148,12 +144,7 @@ impl BufferManager { } pub fn next_buffer(&mut self) -> Option<&mut ShmBuffer> { - for buf in self.buffers.iter_mut() { - if buf.refcnt == 0 { - return Some(buf); - } - } - None + self.buffers.iter_mut().find(|buf| buf.refcnt == 0) } pub fn add_buffer( diff --git a/src/config.rs b/src/config.rs index c09b990..f985773 100644 --- a/src/config.rs +++ b/src/config.rs @@ -161,7 +161,7 @@ impl Config { Config { font_paths: Some(map), - widget: widget, + widget, mode: OperationMode::LayerSurface((1024, 768)), background: Some(0xFAFAFAFA), server: None, @@ -251,7 +251,7 @@ impl Config { Config { font_paths: Some(map), - widget: widget, + widget, mode: if clay { OperationMode::XdgToplevel } else { @@ -281,7 +281,7 @@ impl Widget { pub fn construct_widgets( self, v: &mut Vec>, - mut fm: &mut FontMap, + fm: &mut FontMap, events: &Arc>, ) { match self { @@ -301,12 +301,12 @@ impl Widget { Widget::HorizontalLine(width) => v.push(Box::new(Line::new(width, false))), Widget::VerticalLine(width) => v.push(Box::new(Line::new(width, true))), Widget::Clock { font, font_size } => v.push(Box::new(Clock::new( - &mut fm, + fm, leak_or_default(font, "sans"), font_size, ))), Widget::Date { font, font_size } => v.push(Box::new(Date::new( - &mut fm, + fm, leak_or_default(font, "sans"), font_size, ))), @@ -317,7 +317,7 @@ impl Widget { sections_x, sections_y, } => v.push(Box::new(Calendar::new( - &mut fm, + fm, leak_or_default(font_primary, "monospace"), leak_or_default(font_secondary, "sans"), font_size, @@ -330,14 +330,14 @@ impl Widget { launch_cmd, } => v.push(Box::new(Interface::new( events.clone(), - &mut fm, + fm, leak_or_default(font, "sans"), font_size, launch_cmd, ))), Widget::Battery { font, font_size } => v.push(Box::new(Battery::new( events.clone(), - &mut fm, + fm, leak_or_default(font, "sans"), font_size, ))), @@ -347,13 +347,13 @@ impl Widget { font_size, } => v.push(Box::new(Backlight::new( leak_or_none(device), - &mut fm, + fm, leak_or_default(font, "sans"), font_size, ))), Widget::PulseAudio { font, font_size } => v.push(Box::new(PulseAudio::new( events.clone(), - &mut fm, + fm, Box::leak(font.unwrap_or_else(|| "sans".to_string()).into_boxed_str()), font_size, ))), diff --git a/src/draw.rs b/src/draw.rs index d21f132..2b9159b 100644 --- a/src/draw.rs +++ b/src/draw.rs @@ -98,7 +98,7 @@ impl<'a> Font<'a> { pub fn add_str_to_cache(&mut self, s: &str) { for ch in s.chars() { - if self.glyphs.get(&ch).is_none() { + if !self.glyphs.contains_key(&ch) { let glyph = CachedGlyph::new(self.font, self.size, ch); self.glyphs.insert(ch, glyph); } @@ -118,10 +118,10 @@ impl<'a> Font<'a> { let glyph = match self.glyphs.get(&ch) { Some(glyph) => glyph, None => { - return Err(::std::io::Error::new( - ::std::io::ErrorKind::Other, - format!("glyph for {:} not in cache", ch), - )); + return Err(::std::io::Error::other(format!( + "glyph for {:} not in cache", + ch + ))); } }; glyphs.push(glyph); @@ -151,10 +151,10 @@ impl<'a> Font<'a> { let glyph = match self.glyphs.get(&ch) { Some(glyph) => glyph, None => { - return Err(::std::io::Error::new( - ::std::io::ErrorKind::Other, - format!("glyph for {:} not in cache", ch), - )); + return Err(::std::io::Error::other(format!( + "glyph for {:} not in cache", + ch + ))); } }; glyphs.push(glyph); @@ -203,10 +203,10 @@ impl<'a> Font<'a> { let glyph = match self.glyphs.get(&ch) { Some(glyph) => glyph, None => { - return Err(::std::io::Error::new( - ::std::io::ErrorKind::Other, - format!("glyph for {:} not in cache", ch), - )); + return Err(::std::io::Error::other(format!( + "glyph for {:} not in cache", + ch + ))); } }; let width = glyph.advance; @@ -252,10 +252,10 @@ impl<'a> Font<'a> { let glyph = match self.glyphs.get(&ch) { Some(glyph) => glyph, None => { - return Err(::std::io::Error::new( - ::std::io::ErrorKind::Other, - format!("glyph for {:} not in cache", ch), - )); + return Err(::std::io::Error::other(format!( + "glyph for {:} not in cache", + ch + ))); } }; glyphs.push(glyph); @@ -284,10 +284,10 @@ impl<'a> Font<'a> { let glyph = match self.glyphs.get(&ch) { Some(glyph) => glyph, None => { - return Err(::std::io::Error::new( - ::std::io::ErrorKind::Other, - format!("glyph for {:} not in cache", ch), - )); + return Err(::std::io::Error::other(format!( + "glyph for {:} not in cache", + ch + ))); } }; glyphs.push(glyph); diff --git a/src/event.rs b/src/event.rs index b9b2b9d..80b9fb7 100644 --- a/src/event.rs +++ b/src/event.rs @@ -56,6 +56,6 @@ impl Events { pub fn flush(&mut self) -> Vec { self.dirty = false; - self.events.drain(..).collect() + std::mem::take(&mut self.events) } } diff --git a/src/fonts.rs b/src/fonts.rs index 831cfc8..8da2dcc 100644 --- a/src/fonts.rs +++ b/src/fonts.rs @@ -70,10 +70,13 @@ impl PartialEq for ComparableF32 { impl Eq for ComparableF32 {} +/// font name -> (font name, [(size, glyphs to preload)]) +type RequiredFonts = HashMap<&'static str, (&'static str, Vec<(f32, &'static str)>)>; + pub struct FontMap { fonts: HashMap<(&'static str, ComparableF32), draw::Font<'static>>, font_paths: HashMap<&'static str, String>, - required_fonts: HashMap<&'static str, (&'static str, Vec<(f32, &'static str)>)>, + required_fonts: RequiredFonts, } impl FontMap { diff --git a/src/keyboard/mod.rs b/src/keyboard/mod.rs index 7d68ee4..f9aa9b5 100644 --- a/src/keyboard/mod.rs +++ b/src/keyboard/mod.rs @@ -44,7 +44,7 @@ pub struct KeymapDescription { impl KeymapDescription { fn new(fd: OwnedFd, size: u32) -> KeymapDescription { - KeymapDescription { fd: fd, size: size } + KeymapDescription { fd, size } } } diff --git a/src/keyboard/state.rs b/src/keyboard/state.rs index bca7f8e..b361171 100644 --- a/src/keyboard/state.rs +++ b/src/keyboard/state.rs @@ -81,17 +81,15 @@ impl KbState { } pub(crate) fn compose_feed(&mut self, keysym: u32) -> Option { - match &mut self.xkb_compose_state { - Some(compose_state) => Some(compose_state.feed(xkb::Keysym::new(keysym))), - None => None, - } + self.xkb_compose_state + .as_mut() + .map(|compose_state| compose_state.feed(xkb::Keysym::new(keysym))) } pub(crate) fn compose_status(&mut self) -> Option { - match &mut self.xkb_compose_state { - Some(compose_state) => Some(compose_state.status()), - None => None, - } + self.xkb_compose_state + .as_mut() + .map(|compose_state| compose_state.status()) } pub(crate) fn compose_get_utf8(&mut self) -> Option { @@ -130,11 +128,11 @@ impl KbState { pub(crate) fn init_compose(&mut self) { let locale = env::var_os("LC_ALL") - .and_then(|v| if v.is_empty() { None } else { Some(v) }) + .filter(|v| !v.is_empty()) .or_else(|| env::var_os("LC_CTYPE")) - .and_then(|v| if v.is_empty() { None } else { Some(v) }) + .filter(|v| !v.is_empty()) .or_else(|| env::var_os("LANG")) - .and_then(|v| if v.is_empty() { None } else { Some(v) }) + .filter(|v| !v.is_empty()) .unwrap_or_else(|| "C".into()); let compose_table = match xkb::compose::Table::new_from_locale( diff --git a/src/main.rs b/src/main.rs index ddc4321..b7f0a8a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -159,7 +159,7 @@ fn main() { handle .insert_source( WaylandSource::new(event_queue).expect("Could not create WaylandSource!"), - |_event, queue, mut state| queue.dispatch_pending(&mut state), + |_event, queue, state| queue.dispatch_pending(state), ) .expect("Failed to insert event source!"); @@ -239,23 +239,20 @@ fn main() { .spawn(move || { let listener = UnixListener::bind(socket_path.clone()).unwrap(); loop { - match listener.accept() { - Ok((stream, _)) => { - let _ = thread::Builder::new().name("ipc_client".to_string()).spawn( - move || { - let r = BufReader::new(stream); - for line in r.lines() { - match line { - Ok(ref cmd) if cmd == "kill" => { - std::process::exit(0); - } - _ => {} + if let Ok((stream, _)) = listener.accept() { + let _ = thread::Builder::new().name("ipc_client".to_string()).spawn( + move || { + let r = BufReader::new(stream); + for line in r.lines() { + match line { + Ok(ref cmd) if cmd == "kill" => { + std::process::exit(0); } + _ => {} } - }, - ); - } - _ => (), + } + }, + ); } } }); diff --git a/src/state.rs b/src/state.rs index 6d9acfb..3d451e2 100644 --- a/src/state.rs +++ b/src/state.rs @@ -133,19 +133,16 @@ impl State { Ok(token) => token, Err(_) => return, }; - match ( + if let (Some(surface), Some(activation)) = ( &self.main_surface.wl_surface, &self.protocols.xdg_activation, ) { - (Some(surface), Some(activation)) => { - activation.activate(key, surface); - let activation_token = activation.get_activation_token(qh, ()); - activation_token.commit(); - unsafe { - env::remove_var("XDG_ACTIVATION_TOKEN"); - } + activation.activate(key, surface); + let activation_token = activation.get_activation_token(qh, ()); + activation_token.commit(); + unsafe { + env::remove_var("XDG_ACTIVATION_TOKEN"); } - _ => (), } } } @@ -333,9 +330,8 @@ impl Dispatch for State { _: &Connection, _: &QueueHandle, ) { - match event { - wl_callback::Event::Done { callback_data: _ } => state.ready = true, - _ => {} + if let wl_callback::Event::Done { callback_data: _ } = event { + state.ready = true } } } @@ -483,8 +479,8 @@ impl Dispatch for State { &Geometry { x: 0, y: 0, - width: width as u32, - height: height as u32, + width, + height, }, state, ); @@ -676,13 +672,11 @@ impl Dispatch for State { state.running = false; return; } - if repeats { - if let Event::KeyEvent(k) = ev { - state - .keyrepeat_sender - .send(RepeatMessage::KeyEvent(k)) - .unwrap(); - } + if repeats && let Event::KeyEvent(k) = ev { + state + .keyrepeat_sender + .send(RepeatMessage::KeyEvent(k)) + .unwrap(); } state.dirty = true; } @@ -742,8 +736,8 @@ impl Dispatch for State { state.pointer = Some((surface_x, surface_y)); } wl_pointer::Event::Leave { .. } => state.pointer = None, - wl_pointer::Event::Axis { axis, value, .. } => match state.pointer { - Some((x, y)) => { + wl_pointer::Event::Axis { axis, value, .. } => { + if let Some((x, y)) = state.pointer { let axis = match axis { WEnum::Value(wl_pointer::Axis::VerticalScroll) => { PointerButton::ScrollVertical(value) @@ -758,24 +752,22 @@ impl Dispatch for State { let geo = widget.geometry(); if geo.contains(pos) { let pos = (pos.0 - geo.x, pos.1 - geo.y); - let ev = Event::PointerEvent(PointerEvent { - button: axis, - pos: pos, - }); + let ev = Event::PointerEvent(PointerEvent { button: axis, pos }); widget.event(&ev); state.dirty = true; break; } } } - _ => (), - }, + } wl_pointer::Event::Button { button, state: button_state, .. - } => match (button, button_state, state.pointer) { - (button, WEnum::Value(wl_pointer::ButtonState::Pressed), Some((x, y))) => { + } => { + if let (button, WEnum::Value(wl_pointer::ButtonState::Pressed), Some((x, y))) = + (button, button_state, state.pointer) + { let button = match button { BTN_LEFT => PointerButton::Left, BTN_RIGHT => PointerButton::Right, @@ -787,18 +779,14 @@ impl Dispatch for State { let geo = widget.geometry(); if geo.contains(pos) { let pos = (pos.0 - geo.x, pos.1 - geo.y); - let ev = Event::PointerEvent(PointerEvent { - button: button, - pos: pos, - }); + let ev = Event::PointerEvent(PointerEvent { button, pos }); widget.event(&ev); state.dirty = true; break; } } } - _ => (), - }, + } _ => (), } } @@ -861,14 +849,11 @@ impl Dispatch for State { _: &Connection, _: &QueueHandle, ) { - match event { - xdg_activation_token_v1::Event::Done { token } => { - let ev = Event::TokenUpdate(token); - for widget in state.widgets.iter_mut() { - widget.event(&ev); - } + if let xdg_activation_token_v1::Event::Done { token } = event { + let ev = Event::TokenUpdate(token); + for widget in state.widgets.iter_mut() { + widget.event(&ev); } - _ => (), } } } diff --git a/src/utils/desktop.rs b/src/utils/desktop.rs index 657b5fe..a336b52 100644 --- a/src/utils/desktop.rs +++ b/src/utils/desktop.rs @@ -45,10 +45,10 @@ impl Desktop { .map(|x| { x.split(';') .map(|y| y.trim().to_string()) - .filter(|z| z != "") + .filter(|z| !z.is_empty()) .collect() }) - .unwrap_or_else(|| vec![]), + .unwrap_or_else(std::vec::Vec::new), }), None => Err(Box::new(io_error::new( ErrorKind::NotFound, diff --git a/src/utils/inish.rs b/src/utils/inish.rs index 13bb056..3c8595d 100644 --- a/src/utils/inish.rs +++ b/src/utils/inish.rs @@ -1,6 +1,6 @@ use std::{collections::HashMap, error::Error}; -fn parse_field<'a>(line: &'a str) -> Result<(&'a str, &'a str), Box> { +fn parse_field(line: &str) -> Result<(&str, &str), Box> { let split = match line.find('=') { Some(v) => v, None => return Err("expected equals sign on line, but found none".into()), @@ -30,8 +30,8 @@ pub fn parse<'a>(s: &'a str) -> Result, Box> { (Some('['), Some(']')) => { sections.insert(current_section_name, current_section); current_section = HashMap::new(); - let len = line.bytes().count(); - current_section_name = &line[1..len - 1].trim(); + let len = line.len(); + current_section_name = line[1..len - 1].trim(); } (Some('['), v) => { return Err(format!( diff --git a/src/widgets/backlight.rs b/src/widgets/backlight.rs index cfb7244..a3393ad 100644 --- a/src/widgets/backlight.rs +++ b/src/widgets/backlight.rs @@ -1,6 +1,6 @@ use std::{ fs::{self, OpenOptions}, - io::{Error, ErrorKind, Write}, + io::{Error, Write}, path::{Path, PathBuf}, }; @@ -17,7 +17,7 @@ fn read_file_as_u64(path: &Path) -> Result { let mut s = fs::read_to_string(path)?; s.pop(); s.parse::() - .map_err(|_e| Error::new(ErrorKind::Other, "unable to parse value")) + .map_err(|_e| Error::other("unable to parse value")) } fn write_file_as_u64(path: &Path, value: u64) -> Result<(), Error> { @@ -54,6 +54,8 @@ impl Backlight { } } + // Wraps the impl in the generic BarWidget on purpose. + #[allow(clippy::new_ret_no_self)] pub fn new(path: Option<&str>, fm: &mut FontMap, font: &'static str, size: f32) -> BarWidget { let device_path = match path { Some(path) => Some(Path::new("/sys/class/backlight").join(path)), @@ -104,9 +106,8 @@ impl BarWidgetImpl for Backlight { } fn click(&mut self, pos: f32, btn: PointerButton) { self.dirty = true; - match btn { - PointerButton::Left => self.set(pos), - _ => (), + if let PointerButton::Left = btn { + self.set(pos) }; } } diff --git a/src/widgets/bar_widget.rs b/src/widgets/bar_widget.rs index 336d24e..8336071 100644 --- a/src/widgets/bar_widget.rs +++ b/src/widgets/bar_widget.rs @@ -79,7 +79,7 @@ impl Widget for BarWidget { x += geometry.width - w; } self.geometry = Geometry { - x: x, + x, y: geometry.y, width: w, height: self.size.ceil() as u32, @@ -97,15 +97,12 @@ impl Widget for BarWidget { } fn event(&mut self, event: &Event) { - match event { - Event::PointerEvent(ev) => { - let offset = 4 * self.size.ceil() as u32; - if ev.pos.0 >= offset && self.geometry.width > offset { - let val = (ev.pos.0 - offset) as f32 / (self.geometry.width - offset) as f32; - self.inner_widget.click(val, ev.button); - } + if let Event::PointerEvent(ev) = event { + let offset = 4 * self.size.ceil() as u32; + if ev.pos.0 >= offset && self.geometry.width > offset { + let val = (ev.pos.0 - offset) as f32 / (self.geometry.width - offset) as f32; + self.inner_widget.click(val, ev.button); } - _ => (), } } } diff --git a/src/widgets/battery.rs b/src/widgets/battery.rs index 1605b13..2deb7df 100644 --- a/src/widgets/battery.rs +++ b/src/widgets/battery.rs @@ -138,6 +138,8 @@ pub struct Battery { } impl Battery { + // Wraps the impl in the generic BarWidget on purpose. + #[allow(clippy::new_ret_no_self)] pub fn new( events: Arc>, fm: &mut FontMap, diff --git a/src/widgets/calendar.rs b/src/widgets/calendar.rs index 9ede2ff..04a1dc6 100644 --- a/src/widgets/calendar.rs +++ b/src/widgets/calendar.rs @@ -69,7 +69,7 @@ impl Calendar { fn draw_month( &mut self, fonts: &mut FontMap, - mut view: &mut BufferView, + view: &mut BufferView, orig: NaiveDate, mut time: NaiveDate, ) { @@ -100,7 +100,7 @@ impl Calendar { + (self.size * DAY_FACTOR * 0.2).ceil() as u32, )), c, - &wk_chr, + wk_chr, ) .unwrap(); } @@ -124,9 +124,7 @@ impl Calendar { 12 => "December", _ => panic!("impossible value"), }; - month_font - .draw_text(&mut view, Color::WHITE, month_str) - .unwrap(); + month_font.draw_text(view, Color::WHITE, month_str).unwrap(); // // Draw the year @@ -199,7 +197,7 @@ impl Calendar { } } -impl<'a> Widget for Calendar { +impl Widget for Calendar { fn get_dirty(&self) -> bool { self.dirty } @@ -209,11 +207,8 @@ impl<'a> Widget for Calendar { } fn event(&mut self, event: &Event) { - match event { - Event::NewMinute => { - self.dirty = true; - } - _ => (), + if let Event::NewMinute = event { + self.dirty = true; } } @@ -299,9 +294,8 @@ impl<'a> Widget for Calendar { self.geometry = Geometry { x: geometry.x, y: geometry.y, - width: cal_width * self.shown_sections_x as u32 - + cal_pad * (self.shown_sections_x as u32 - 1), - height: cal_height * self.shown_sections_y as u32, + width: cal_width * self.shown_sections_x + cal_pad * (self.shown_sections_x - 1), + height: cal_height * self.shown_sections_y, }; self.geometry } @@ -344,8 +338,8 @@ impl<'a> Widget for Calendar { Geometry { x: 0, y: 0, - width: width, - height: height, + width, + height, } } } diff --git a/src/widgets/clock.rs b/src/widgets/clock.rs index 9f932ce..92d8bcb 100644 --- a/src/widgets/clock.rs +++ b/src/widgets/clock.rs @@ -19,7 +19,7 @@ pub struct Clock { colon_width: u32, } -impl<'a> Clock { +impl Clock { pub fn new(fm: &mut FontMap, font: &'static str, size: f32) -> Clock { fm.queue_font(font, size, "0123456789: "); Clock { @@ -33,7 +33,7 @@ impl<'a> Clock { } } -impl<'a> Widget for Clock { +impl Widget for Clock { fn get_dirty(&self) -> bool { self.dirty } @@ -43,11 +43,8 @@ impl<'a> Widget for Clock { } fn event(&mut self, event: &Event) { - match event { - Event::NewMinute => { - self.dirty = true; - } - _ => (), + if let Event::NewMinute = event { + self.dirty = true; } } diff --git a/src/widgets/date.rs b/src/widgets/date.rs index 2dce5c9..528c76d 100644 --- a/src/widgets/date.rs +++ b/src/widgets/date.rs @@ -29,7 +29,7 @@ impl Date { } } -impl<'a> Widget for Date { +impl Widget for Date { fn get_dirty(&self) -> bool { self.dirty } @@ -39,11 +39,8 @@ impl<'a> Widget for Date { } fn event(&mut self, event: &Event) { - match event { - Event::NewMinute => { - self.dirty = true; - } - _ => (), + if let Event::NewMinute = event { + self.dirty = true; } } diff --git a/src/widgets/launcher.rs b/src/widgets/launcher.rs index 526eb20..fbed4a5 100644 --- a/src/widgets/launcher.rs +++ b/src/widgets/launcher.rs @@ -120,7 +120,7 @@ impl Prompt { } fn append(&mut self, v: &str) { - if self.input.len() == 0 { + if self.input.is_empty() { match v { "!" => { self.mode = PromptMode::Shell; @@ -271,7 +271,7 @@ impl Launcher { Launcher { next_token: None, - options: options, + options, matches: Vec::new(), launch_cmd, } @@ -354,7 +354,7 @@ impl InterfaceWidget for Launcher { .auto_draw_text_with_cursor( &mut prompt_line, fg, - &format!(" {}", &intf.prompt.input), + &format!(" {}", intf.prompt.input), intf.prompt.cursor + 3, ) .unwrap(); @@ -474,7 +474,7 @@ impl InterfaceWidget for Shell { .auto_draw_text_with_cursor( &mut prompt_line, fg, - &format!(" {}", &intf.prompt.input), + &format!(" {}", intf.prompt.input), intf.prompt.cursor + 3, ) .unwrap(); @@ -531,7 +531,7 @@ impl Calc { let old = self.old.lock().unwrap(); for (input, res) in old.iter() { - write!(f, "{}={}\n", input, res).unwrap(); + writeln!(f, "{}={}", input, res).unwrap(); } f.sync_data().unwrap(); } @@ -567,10 +567,7 @@ impl InterfaceWidget for Calc { rcalc_lib::parse::eval(&intf.prompt.input, &mut rcalc_lib::parse::CalcState::new()) .map(|x| format!("{}", x)) .map_err(|x| format!("{}", x)); - self.result = match res { - Ok(v) => Some(v), - Err(_) => None, - }; + self.result = res.ok(); } fn draw( @@ -593,7 +590,7 @@ impl InterfaceWidget for Calc { font.auto_draw_text_with_cursor( &mut prompt_line, fg, - &format!(" {} ", &intf.prompt.input), + &format!(" {} ", intf.prompt.input), intf.prompt.cursor + 3, ) .unwrap(); @@ -712,7 +709,7 @@ impl Interface { } fn exit(&mut self) { - if self.inner.prompt.input.len() == 0 { + if self.inner.prompt.input.is_empty() { std::process::exit(0); } @@ -810,7 +807,7 @@ impl Widget for Interface { } fn geometry_update(&mut self, _fonts: &mut FontMap, geometry: &Geometry) -> Geometry { - self.inner.geometry = geometry.clone(); + self.inner.geometry = *geometry; self.inner.geometry } diff --git a/src/widgets/layout.rs b/src/widgets/layout.rs index 47ff3a7..1aa0473 100644 --- a/src/widgets/layout.rs +++ b/src/widgets/layout.rs @@ -1,3 +1,6 @@ +// Constructors here deliberately return Box so configs compose. +#![allow(clippy::new_ret_no_self)] + use std::cmp::max; use crate::{fonts::FontMap, state::State, widgets::Geometry}; @@ -30,7 +33,7 @@ impl Layout for HorizontalLayout { geometry: &Geometry, state: &mut State, ) -> Geometry { - let mut geo = geometry.clone(); + let mut geo = *geometry; let mut max_width = 0; let mut max_height = 0; for w in self.widgets.iter() { @@ -79,7 +82,7 @@ impl Layout for VerticalLayout { geometry: &Geometry, state: &mut State, ) -> Geometry { - let mut geo = geometry.clone(); + let mut geo = *geometry; let mut max_width = 0; let mut max_height = 0; for w in self.widgets.iter() { @@ -159,10 +162,10 @@ impl Layout for InvertedHorizontalLayout { geometry: &Geometry, state: &mut State, ) -> Geometry { - let mut geo = geometry.clone(); + let mut geo = *geometry; let mut max_height = 0; for w in self.widgets.iter() { - let mut temp_geo = geo.clone(); + let mut temp_geo = geo; let temp_result = w.geometry_update(fonts, &temp_geo, state); temp_geo.x = geo.x + (geo.width - temp_result.width); let result = w.geometry_update(fonts, &temp_geo, state); @@ -207,10 +210,10 @@ impl Layout for InvertedVerticalLayout { geometry: &Geometry, state: &mut State, ) -> Geometry { - let mut geo = geometry.clone(); + let mut geo = *geometry; let mut max_width = 0; for w in self.widgets.iter() { - let mut temp_geo = geo.clone(); + let mut temp_geo = geo; let temp_result = w.geometry_update(fonts, &temp_geo, state); temp_geo.y = geo.y + (geo.height - temp_result.height); let result = w.geometry_update(fonts, &temp_geo, state); diff --git a/src/widgets/pulseaudio.rs b/src/widgets/pulseaudio.rs index 9503f8a..1a91c17 100644 --- a/src/widgets/pulseaudio.rs +++ b/src/widgets/pulseaudio.rs @@ -67,15 +67,15 @@ fn monitor( let local_inner = inner.clone(); let local_events = events.clone(); context.borrow_mut().set_subscribe_callback(Some(Box::new( - move |facility, _operation, index| match facility { - Some(Facility::Sink) => { + move |facility, _operation, index| { + if let Some(Facility::Sink) = facility { let introspector = local_context.borrow_mut().introspect(); let inner = local_inner.clone(); let events = local_events.clone(); introspector.get_sink_info_by_index(index, move |res| match res { ListResult::End | ListResult::Error => (), ListResult::Item(sink_info) => { - if let Some(_) = &sink_info.name { + if sink_info.name.is_some() { let avg = sink_info.volume.avg().0 as f32 / Volume::NORMAL.0 as f32; let mut inner = inner.lock().unwrap(); inner.volume = avg; @@ -88,7 +88,6 @@ fn monitor( } }); } - _ => (), }, ))); @@ -101,7 +100,7 @@ fn monitor( introspector.get_sink_info_by_name("@DEFAULT_SINK@", move |res| match res { ListResult::End | ListResult::Error => (), ListResult::Item(sink_info) => { - if let Some(_) = &sink_info.name { + if sink_info.name.is_some() { let avg = sink_info.volume.avg().0 as f32 / Volume::NORMAL.0 as f32; let mut inner = local_inner.lock().unwrap(); inner.volume = avg; @@ -119,7 +118,7 @@ fn monitor( let local_context = context.clone(); let local_inner = inner.clone(); let event_source = mainloop.new_io_event( - pipe as i32, + pipe, IoFlagSet::INPUT, Box::new(move |_, _, _| { let mut buf = [0; 8]; @@ -201,6 +200,8 @@ pub struct PulseAudio { } impl PulseAudio { + // Wraps the impl in the generic BarWidget on purpose. + #[allow(clippy::new_ret_no_self)] pub fn new( events: Arc>, fm: &mut FontMap,