From 04f45ccae1498630a81edd34923c5920e864bacc Mon Sep 17 00:00:00 2001 From: Jake Stanger Date: Wed, 14 Aug 2024 21:23:28 +0100 Subject: [PATCH] refactor: fix some pedantic clippy warnings --- src/bar.rs | 2 +- src/clients/music/mpris.rs | 1 + src/clients/sway.rs | 2 +- .../wayland/wlr_foreign_toplevel/handle.rs | 2 +- src/config/impl.rs | 18 ++++++++---------- src/ipc/server/bar.rs | 11 ++++++----- src/main.rs | 17 ++++++++--------- src/modules/cairo.rs | 18 ++++++++---------- src/modules/sway/mode.rs | 6 +++--- src/modules/sysinfo.rs | 4 ++-- src/modules/tray/icon.rs | 2 +- src/popup.rs | 6 +++--- 12 files changed, 43 insertions(+), 46 deletions(-) diff --git a/src/bar.rs b/src/bar.rs index 0a4ea751..518cadbf 100644 --- a/src/bar.rs +++ b/src/bar.rs @@ -346,7 +346,7 @@ impl Bar { /// Sets the window visibility status pub fn set_visible(&self, visible: bool) { - self.window.set_visible(visible) + self.window.set_visible(visible); } pub fn set_exclusive(&self, exclusive: bool) { diff --git a/src/clients/music/mpris.rs b/src/clients/music/mpris.rs index 2b5e6434..95e63f23 100644 --- a/src/clients/music/mpris.rs +++ b/src/clients/music/mpris.rs @@ -54,6 +54,7 @@ impl Client { vec![] } }); + // Acquire the lock of current_player before players to avoid deadlock. // There are places where we lock on current_player and players, but we always lock on current_player first. // This is because we almost never need to lock on players without locking on current_player. diff --git a/src/clients/sway.rs b/src/clients/sway.rs index 5a28bfb4..492709e3 100644 --- a/src/clients/sway.rs +++ b/src/clients/sway.rs @@ -56,7 +56,7 @@ impl Client { T::EVENT_TYPE, Box::new(move |event| { let event = T::from_event(event).expect("event type mismatch"); - f(event) + f(event); }), ) .await diff --git a/src/clients/wayland/wlr_foreign_toplevel/handle.rs b/src/clients/wayland/wlr_foreign_toplevel/handle.rs index 91abfeaf..30ec99da 100644 --- a/src/clients/wayland/wlr_foreign_toplevel/handle.rs +++ b/src/clients/wayland/wlr_foreign_toplevel/handle.rs @@ -146,7 +146,7 @@ where ToplevelHandle { handle: handle.clone(), }, - ) + ); } Event::Done if !lock!(data.inner).closed => { { diff --git a/src/config/impl.rs b/src/config/impl.rs index 3c722c3f..cab524f6 100644 --- a/src/config/impl.rs +++ b/src/config/impl.rs @@ -37,20 +37,18 @@ impl<'de> Deserialize<'de> for MonitorConfig { pub fn deserialize_layer<'de, D>(deserializer: D) -> Result where - D: serde::Deserializer<'de>, + D: Deserializer<'de>, { use gtk_layer_shell::Layer; let value = Option::::deserialize(deserializer)?; - value - .map(|v| match v.as_str() { - "background" => Ok(Layer::Background), - "bottom" => Ok(Layer::Bottom), - "top" => Ok(Layer::Top), - "overlay" => Ok(Layer::Overlay), - _ => Err(serde::de::Error::custom("invalid value for orientation")), - }) - .unwrap_or(Ok(Layer::Top)) + value.map_or(Ok(Layer::Top), |v| match v.as_str() { + "background" => Ok(Layer::Background), + "bottom" => Ok(Layer::Bottom), + "top" => Ok(Layer::Top), + "overlay" => Ok(Layer::Overlay), + _ => Err(serde::de::Error::custom("invalid value for orientation")), + }) } #[cfg(feature = "schema")] diff --git a/src/ipc/server/bar.rs b/src/ipc/server/bar.rs index 3bd3b8c2..ab047d39 100644 --- a/src/ipc/server/bar.rs +++ b/src/ipc/server/bar.rs @@ -6,12 +6,13 @@ use crate::Ironbar; use std::rc::Rc; pub fn handle_command(command: BarCommand, ironbar: &Rc) -> Response { + use BarCommandType::*; + let bar = ironbar.bar_by_name(&command.name); let Some(bar) = bar else { return Response::error("Invalid bar name"); }; - use BarCommandType::*; match command.subcommand { Show => set_visible(&bar, true), Hide => set_visible(&bar, false), @@ -21,14 +22,14 @@ pub fn handle_command(command: BarCommand, ironbar: &Rc) -> Response { value: bar.visible().to_string(), }, - ShowPopup { widget_name } => show_popup(&bar, widget_name), + ShowPopup { widget_name } => show_popup(&bar, &widget_name), HidePopup => hide_popup(&bar), SetPopupVisible { widget_name, visible, } => { if visible { - show_popup(&bar, widget_name) + show_popup(&bar, &widget_name) } else { hide_popup(&bar) } @@ -37,7 +38,7 @@ pub fn handle_command(command: BarCommand, ironbar: &Rc) -> Response { if bar.popup().visible() { hide_popup(&bar) } else { - show_popup(&bar, widget_name) + show_popup(&bar, &widget_name) } } GetPopupVisible => Response::OkValue { @@ -56,7 +57,7 @@ fn set_visible(bar: &Bar, visible: bool) -> Response { Response::Ok } -fn show_popup(bar: &Bar, widget_name: String) -> Response { +fn show_popup(bar: &Bar, widget_name: &str) -> Response { let popup = bar.popup(); // only one popup per bar, so hide if open for another widget diff --git a/src/main.rs b/src/main.rs index e2aac18f..2d1699ce 100644 --- a/src/main.rs +++ b/src/main.rs @@ -88,7 +88,7 @@ fn run_with_args() { match args.command { Some(command) => { if args.debug { - eprintln!("REQUEST: {command:?}") + eprintln!("REQUEST: {command:?}"); } let rt = create_runtime(); @@ -97,10 +97,10 @@ fn run_with_args() { match ipc.send(command, args.debug).await { Ok(res) => { if args.debug { - eprintln!("RESPONSE: {res:?}") + eprintln!("RESPONSE: {res:?}"); } - cli::handle_response(res, args.format.unwrap_or_default()) + cli::handle_response(res, args.format.unwrap_or_default()); } Err(err) => error!("{err:?}"), }; @@ -374,12 +374,11 @@ fn load_output_bars( let map = INDEX_MAP.get_or_init(|| Mutex::new(vec![])); let index = lock!(map).iter().position(|n| n == monitor_name); - let index = match index { - Some(index) => index, - None => { - lock!(map).push(monitor_name.clone()); - lock!(map).len() - 1 - } + let index = if let Some(index) = index { + index + } else { + lock!(map).push(monitor_name.clone()); + lock!(map).len() - 1 }; let config = ironbar.config.borrow(); diff --git a/src/modules/cairo.rs b/src/modules/cairo.rs index bb0290b5..b5c144ff 100644 --- a/src/modules/cairo.rs +++ b/src/modules/cairo.rs @@ -74,7 +74,7 @@ impl Module for CairoModule { where >::SendMessage: Clone, { - let path = self.path.to_path_buf(); + let path = self.path.clone(); let tx = context.tx.clone(); spawn(async move { @@ -160,16 +160,14 @@ impl Module for CairoModule { let ptr = unsafe { cr.clone().into_glib_ptr().cast() }; // mlua needs a valid return type, even if we don't return anything - if let Err(err) = function.call::<_, Option>((id.as_str(), LightUserData(ptr))) { - match err { - Error::RuntimeError(message) => { - let message = message.split_once("]:").expect("to exist").1; - error!("[lua runtime error] {}:{message}", path.display()) - } - _ => error!("{err}"), + if let Error::RuntimeError(message) = err { + let message = message.split_once("]:").expect("to exist").1; + error!("[lua runtime error] {}:{message}", path.display()); + } else { + error!("{err}"); } return Propagation::Stop; @@ -196,10 +194,10 @@ impl Module for CairoModule { match res { Ok(script) => { match lua.load(&script).exec() { - Ok(_) => {}, + Ok(()) => {}, Err(Error::SyntaxError { message, ..}) => { let message = message.split_once("]:").expect("to exist").1; - error!("[lua syntax error] {}:{message}", self.path.display()) + error!("[lua syntax error] {}:{message}", self.path.display()); }, Err(err) => error!("lua error: {err:?}") } diff --git a/src/modules/sway/mode.rs b/src/modules/sway/mode.rs index 9836520d..1499d1ea 100644 --- a/src/modules/sway/mode.rs +++ b/src/modules/sway/mode.rs @@ -70,10 +70,10 @@ impl Module