From b44de0c47a83d22da4db34645daf0efc9d473485 Mon Sep 17 00:00:00 2001 From: Alexander Mohr Date: Sun, 13 Jul 2025 12:45:01 +0200 Subject: [PATCH] correct clippy scoping #81 --- worf/src/lib/desktop.rs | 17 +++++++-------- worf/src/lib/gui.rs | 42 +++++++++++++++++++------------------- worf/src/lib/modes/drun.rs | 3 +-- worf/src/lib/modes/math.rs | 1 - worf/src/lib/modes/run.rs | 4 ++-- 5 files changed, 33 insertions(+), 34 deletions(-) diff --git a/worf/src/lib/desktop.rs b/worf/src/lib/desktop.rs index 3d1c5c5..b0ec3b5 100644 --- a/worf/src/lib/desktop.rs +++ b/worf/src/lib/desktop.rs @@ -2,7 +2,9 @@ use std::{ collections::HashMap, env, ffi::OsStr, - fs, io, + fs, + hash::BuildHasher, + io, os::unix::{fs::PermissionsExt, prelude::CommandExt}, path::{Path, PathBuf}, process::{Command, Stdio}, @@ -124,12 +126,10 @@ pub fn get_locale_variants() -> Vec { } /// Lookup a value from a hashmap with respect to current locale -// implicit hasher does not make sense here, it is only for desktop files -#[allow(clippy::implicit_hasher)] #[must_use] -pub fn lookup_name_with_locale( +pub fn lookup_name_with_locale( locale_variants: &[String], - variants: &HashMap, + variants: &HashMap, fallback: &str, ) -> Option { locale_variants @@ -272,9 +272,10 @@ pub fn load_cache_file(cache_path: &PathBuf) -> Result, Err /// # Errors /// `Error::Parsing` if converting into toml was not possible /// `Error::Io` if storing the file failed. -// implicit hasher does not make sense here, it is only for desktop files -#[allow(clippy::implicit_hasher)] -pub fn save_cache_file(path: &PathBuf, data: &HashMap) -> Result<(), Error> { +pub fn save_cache_file( + path: &PathBuf, + data: &HashMap, +) -> Result<(), Error> { // Convert the HashMap to TOML string let toml_string = toml::ser::to_string(&data).map_err(|e| Error::ParsingError(e.to_string()))?; diff --git a/worf/src/lib/gui.rs b/worf/src/lib/gui.rs index 3a67b90..9efe0d6 100644 --- a/worf/src/lib/gui.rs +++ b/worf/src/lib/gui.rs @@ -8,23 +8,21 @@ use std::{ }; use crossbeam::channel::{self, Sender}; -use gdk4::glib::SignalHandlerId; -use gdk4::prelude::ObjectExt; use gdk4::{ Display, Rectangle, gio::File, - glib::{self, MainContext, Propagation}, - prelude::{Cast, DisplayExt, MonitorExt, SurfaceExt}, + glib::{self, MainContext, Propagation, SignalHandlerId}, + prelude::{Cast, DisplayExt, MonitorExt, ObjectExt, SurfaceExt}, }; -use gtk4::prelude::{AdjustmentExt, EventControllerExt}; use gtk4::{ Align, Application, ApplicationWindow, CssProvider, EventControllerKey, Expander, FlowBox, FlowBoxChild, GestureClick, Image, Label, ListBox, ListBoxRow, NaturalWrapMode, Ordering, Orientation, PolicyType, ScrolledWindow, SearchEntry, Widget, glib::ControlFlow, prelude::{ - ApplicationExt, ApplicationExtManual, BoxExt, EditableExt, FlowBoxChildExt, - GestureSingleExt, GtkWindowExt, ListBoxRowExt, NativeExt, OrientableExt, WidgetExt, + AdjustmentExt, ApplicationExt, ApplicationExtManual, BoxExt, EditableExt, + EventControllerExt, FlowBoxChildExt, GestureSingleExt, GtkWindowExt, ListBoxRowExt, + NativeExt, OrientableExt, WidgetExt, }, }; use gtk4_layer_shell::{Edge, KeyboardMode, LayerShell}; @@ -164,10 +162,6 @@ pub struct MenuItem { /// Allows to store arbitrary additional information pub data: Option, - // /// If set to true, the item is _not_ an intermediate thing - // /// and is acceptable, i.e. will close the UI - // pub allow_submit: bool, - // todo /// Score the item got in the current search search_sort_score: f64, /// True if the item is visible @@ -985,7 +979,7 @@ fn setup_key_event_handler( custom_keys: Option<&CustomKeys>, ) { fn connect_key_handler< - T: gtk4::prelude::ObjectExt + Clone + 'static + WidgetExt, + T: ObjectExt + Clone + 'static + WidgetExt, Menu: Clone + 'static + Send, >( widget: &T, @@ -1028,7 +1022,6 @@ fn is_key_match( } } -#[allow(clippy::cast_sign_loss)] // ok because we only need positive values fn handle_key_press( ui: &Rc>, meta: &Rc>, @@ -1059,6 +1052,8 @@ fn handle_key_press( } else { pos }; + // position will not be negative + #[allow(clippy::cast_sign_loss)] if let Some((start, ch)) = query.char_indices().nth(del_pos as usize) { let end = start + ch.len_utf8(); query.replace_range(start..end, ""); @@ -1094,7 +1089,10 @@ fn handle_key_press( let search_text = ui.search_text.lock().unwrap(); search_text.clone() }; + let pos = ui.search.position(); + // position never is negative here. + #[allow(clippy::cast_sign_loss)] let byte_idx = query .char_indices() .nth(pos as usize) @@ -1502,7 +1500,6 @@ fn get_monitor_geometry(surface: Option<&gdk4::Surface>) -> Option { .map(|monitor| monitor.geometry()) } -#[allow(clippy::cast_possible_truncation)] // does not matter for calculating height fn calculate_row_height( ui: &UiElements, lines: i32, @@ -1523,8 +1520,12 @@ fn calculate_row_height( let factor = config.lines_size_factor(); if config.allow_images() && baseline < i32::from(config.image_size()) { + // not relevant for height + #[allow(clippy::cast_possible_truncation)] Some((f64::from(i32::from(config.image_size())) * factor) as i32) } else { + // not relevant for height + #[allow(clippy::cast_possible_truncation)] Some((f64::from(baseline) * factor) as i32) } } else { @@ -1944,6 +1945,7 @@ pub fn filtered_query(search_ignored_words: Option<&Vec>, query: &str) -> } query } + enum ChildPosition { Front, Back, @@ -1992,13 +1994,13 @@ fn select_visible_child( } // allowed because truncating is fine, we do no need the precision -#[allow(clippy::cast_possible_truncation)] -#[allow(clippy::cast_precision_loss)] fn percent_or_absolute(value: &str, base_value: i32) -> Option { if value.contains('%') { let value = value.replace('%', "").trim().to_string(); match value.parse::() { - Ok(n) => Some(((n as f32 / 100.0) * base_value as f32) as i32), + // okay to truncate the value for positioning. + #[allow(clippy::cast_possible_truncation)] + Ok(n) => Some(((f64::from(n) / 100.0) * f64::from(base_value)) as i32), Err(_) => None, } } else { @@ -2007,14 +2009,12 @@ fn percent_or_absolute(value: &str, base_value: i32) -> Option { } /// Sorts menu items in alphabetical order, while maintaining the initial score -// highly unlikely that we are dealing with > i64 items -#[allow(clippy::cast_possible_wrap)] -#[allow(clippy::cast_possible_truncation)] -#[allow(clippy::cast_precision_loss)] pub fn apply_sort(items: &mut [MenuItem], order: &SortOrder) { match order { SortOrder::Default => {} SortOrder::Alphabetical => { + // we won't deal w/ enough items that this matters + #[allow(clippy::cast_precision_loss)] let special_score = items.len() as f64; let mut regular_score = 0.0; items.sort_by(|l, r| r.label.cmp(&l.label)); diff --git a/worf/src/lib/modes/drun.rs b/worf/src/lib/modes/drun.rs index f634bfa..890f33c 100644 --- a/worf/src/lib/modes/drun.rs +++ b/worf/src/lib/modes/drun.rs @@ -70,8 +70,6 @@ impl DRunProvider { } } - #[allow(clippy::cast_possible_truncation)] - #[allow(clippy::cast_precision_loss)] fn load(&self) -> Vec> { let locale_variants = get_locale_variants(); let default_icon = "application-x-executable".to_string(); @@ -117,6 +115,7 @@ impl DRunProvider { .map(|s| s.content.clone()) .or(Some(default_icon.clone())); + #[allow(clippy::cast_precision_loss)] // we won't deal with enough values anyways let sort_score = *self.cache.get(&name).unwrap_or(&0) as f64; let mut entry = MenuItem::new( diff --git a/worf/src/lib/modes/math.rs b/worf/src/lib/modes/math.rs index 7c4e1ee..50c1a69 100644 --- a/worf/src/lib/modes/math.rs +++ b/worf/src/lib/modes/math.rs @@ -32,7 +32,6 @@ impl MathProvider { } impl ItemProvider for MathProvider { - #[allow(clippy::cast_possible_truncation)] fn get_elements(&mut self, search: Option<&str>) -> ProviderData { if let Some(search_text) = search { let result = calc(search_text); diff --git a/worf/src/lib/modes/run.rs b/worf/src/lib/modes/run.rs index 0a58540..85c7d6e 100644 --- a/worf/src/lib/modes/run.rs +++ b/worf/src/lib/modes/run.rs @@ -55,8 +55,6 @@ impl RunProvider { }) } - #[allow(clippy::cast_possible_truncation)] - #[allow(clippy::cast_precision_loss)] fn load(&self) -> Vec> { let path_var = env::var("PATH").unwrap_or_default(); let paths = env::split_paths(&path_var); @@ -75,6 +73,8 @@ impl RunProvider { } let label = path.file_name()?.to_str()?.to_string(); + // acceptable, because we most likely have not enough items anyways + #[allow(clippy::cast_precision_loss)] let sort_score = *self.cache.get(&label).unwrap_or(&0) as f64; Some(MenuItem::new(