From 76c3bc4254328345322361757130b7da6d7d43d3 Mon Sep 17 00:00:00 2001 From: Clement Tsang <34804052+ClementTsang@users.noreply.github.com> Date: Fri, 4 Nov 2022 04:17:22 -0400 Subject: [PATCH] refactor: clippy and small cleanup/comments (#865) * refactor: small cleanup and comments in some code * clippy * more clippy * fixes for macos/freebsd --- build.rs | 2 +- src/app.rs | 20 ++++++++------------ src/app/data_harvester/disks/freebsd.rs | 2 +- src/app/data_harvester/processes/freebsd.rs | 2 +- src/app/data_harvester/processes/macos.rs | 2 +- src/app/widgets/process_table.rs | 18 ++++++++---------- src/canvas/canvas_colours.rs | 10 +++++----- src/clap.rs | 2 +- src/components/data_table/draw.rs | 2 +- src/components/data_table/sortable.rs | 1 - 10 files changed, 27 insertions(+), 34 deletions(-) diff --git a/build.rs b/build.rs index cb7d380d..fd8ce637 100644 --- a/build.rs +++ b/build.rs @@ -8,7 +8,7 @@ use std::{ include!("src/clap.rs"); fn create_dir(dir: &Path) -> Result<()> { - let res = fs::create_dir_all(&dir); + let res = fs::create_dir_all(dir); match &res { Ok(()) => {} Err(err) => { diff --git a/src/app.rs b/src/app.rs index 7a00e848..e22da239 100644 --- a/src/app.rs +++ b/src/app.rs @@ -839,7 +839,7 @@ impl App { &self.current_widget.top_left_corner, &self.current_widget.bottom_right_corner, ) { - let border_offset = if self.is_drawing_border() { 1 } else { 0 }; + let border_offset = u16::from(self.is_drawing_border()); let header_offset = self.header_offset(&self.current_widget); let height = brc_y - tlc_y - 2 * border_offset - header_offset; self.change_position_count(-(height as i64)); @@ -867,7 +867,7 @@ impl App { &self.current_widget.top_left_corner, &self.current_widget.bottom_right_corner, ) { - let border_offset = if self.is_drawing_border() { 1 } else { 0 }; + let border_offset = u16::from(self.is_drawing_border()); let header_offset = self.header_offset(&self.current_widget); let height = brc_y - tlc_y - 2 * border_offset - header_offset; self.change_position_count(height as i64); @@ -886,7 +886,7 @@ impl App { &self.current_widget.top_left_corner, &self.current_widget.bottom_right_corner, ) { - let border_offset = if self.is_drawing_border() { 1 } else { 0 }; + let border_offset = u16::from(self.is_drawing_border()); let header_offset = self.header_offset(&self.current_widget); let height = brc_y - tlc_y - 2 * border_offset - header_offset; self.change_position_count(-(height as i64) / 2); @@ -905,7 +905,7 @@ impl App { &self.current_widget.top_left_corner, &self.current_widget.bottom_right_corner, ) { - let border_offset = if self.is_drawing_border() { 1 } else { 0 }; + let border_offset = u16::from(self.is_drawing_border()); let header_offset = self.header_offset(&self.current_widget); let height = brc_y - tlc_y - 2 * border_offset - header_offset; self.change_position_count(height as i64 / 2); @@ -1330,13 +1330,13 @@ impl App { pub fn kill_highlighted_process(&mut self) -> Result<()> { if let BottomWidgetType::Proc = self.current_widget.widget_type { - if let Some(current_selected_processes) = &self.to_delete_process_list { + if let Some((_, pids)) = &self.to_delete_process_list { #[cfg(target_family = "unix")] let signal = match self.delete_dialog_state.selected_signal { KillSignal::Kill(sig) => sig, KillSignal::Cancel => 15, // should never happen, so just TERM }; - for pid in ¤t_selected_processes.1 { + for pid in pids { #[cfg(target_family = "unix")] { process_killer::kill_process_given_pid(*pid, signal)?; @@ -2456,7 +2456,7 @@ impl App { &self.current_widget.top_left_corner, &self.current_widget.bottom_right_corner, ) { - let border_offset = if self.is_drawing_border() { 1 } else { 0 }; + let border_offset = u16::from(self.is_drawing_border()); // This check ensures the click isn't actually just clicking on the bottom border. if y < (brc_y - border_offset) { @@ -2616,11 +2616,7 @@ impl App { 1 + self.app_config_fields.table_gap } else { let min_height_for_header = if self.is_drawing_border() { 3 } else { 1 }; - if height_diff > min_height_for_header { - 1 - } else { - 0 - } + u16::from(height_diff > min_height_for_header) } } else { 1 + self.app_config_fields.table_gap diff --git a/src/app/data_harvester/disks/freebsd.rs b/src/app/data_harvester/disks/freebsd.rs index 3f15dff4..d14dd793 100644 --- a/src/app/data_harvester/disks/freebsd.rs +++ b/src/app/data_harvester/disks/freebsd.rs @@ -99,7 +99,7 @@ fn matches_ignore_list(filter_check_map: &[(&Option, &String)]) -> bool fn get_disk_info() -> io::Result { let output = std::process::Command::new("df") - .args(&["--libxo", "json", "-k", "-t", "ufs,msdosfs,zfs"]) + .args(["--libxo", "json", "-k", "-t", "ufs,msdosfs,zfs"]) .output()?; deserialize_xo("storage-system-information", &output.stdout) } diff --git a/src/app/data_harvester/processes/freebsd.rs b/src/app/data_harvester/processes/freebsd.rs index d050e5aa..9250c308 100644 --- a/src/app/data_harvester/processes/freebsd.rs +++ b/src/app/data_harvester/processes/freebsd.rs @@ -42,7 +42,7 @@ fn get_freebsd_process_cpu_usage(pids: &[i32]) -> io::Result std::io::Result> { use itertools::Itertools; let output = std::process::Command::new("ps") - .args(&["-o", "pid=,pcpu=", "-p"]) + .args(["-o", "pid=,pcpu=", "-p"]) .arg( // Has to look like this since otherwise, it you hit a `unstable_name_collisions` warning. Itertools::intersperse(pids.iter().map(i32::to_string), ",".to_string()) diff --git a/src/app/widgets/process_table.rs b/src/app/widgets/process_table.rs index adba1133..f0253da2 100644 --- a/src/app/widgets/process_table.rs +++ b/src/app/widgets/process_table.rs @@ -1,4 +1,4 @@ -use std::{borrow::Cow, collections::hash_map::Entry}; +use std::borrow::Cow; use crate::{ app::{ @@ -499,7 +499,7 @@ impl ProcWidget { let mut id_pid_map: FxHashMap> = FxHashMap::default(); let mut filtered_data: Vec = if let ProcWidgetMode::Grouped = self.mode { - let mut id_process_mapping: FxHashMap = FxHashMap::default(); + let mut id_process_mapping: FxHashMap<&String, ProcessHarvest> = FxHashMap::default(); for process in filtered_iter { let id = if is_using_command { &process.command @@ -508,19 +508,17 @@ impl ProcWidget { }; let pid = process.pid; - match id_pid_map.entry(id.clone()) { - Entry::Occupied(mut occupied) => { - occupied.get_mut().push(pid); - } - Entry::Vacant(vacant) => { - vacant.insert(vec![pid]); - } + if let Some(entry) = id_pid_map.get_mut(id) { + entry.push(pid); + } else { + id_pid_map.insert(id.clone(), vec![pid]); } if let Some(grouped_process_harvest) = id_process_mapping.get_mut(id) { grouped_process_harvest.add(process); } else { - id_process_mapping.insert(id.clone(), process.clone()); + // FIXME: [PERF] could maybe eliminate an allocation here in the grouped mode... or maybe just avoid the entire transformation step, making an alloc fine. + id_process_mapping.insert(id, process.clone()); } } diff --git a/src/canvas/canvas_colours.rs b/src/canvas/canvas_colours.rs index ab9b079e..1fa9ce3a 100644 --- a/src/canvas/canvas_colours.rs +++ b/src/canvas/canvas_colours.rs @@ -100,19 +100,19 @@ impl CanvasColours { match colour_scheme { ColourScheme::Default => {} ColourScheme::DefaultLight => { - canvas_colours.set_colours_from_palette(&*DEFAULT_LIGHT_MODE_COLOUR_PALETTE)?; + canvas_colours.set_colours_from_palette(&DEFAULT_LIGHT_MODE_COLOUR_PALETTE)?; } ColourScheme::Gruvbox => { - canvas_colours.set_colours_from_palette(&*GRUVBOX_COLOUR_PALETTE)?; + canvas_colours.set_colours_from_palette(&GRUVBOX_COLOUR_PALETTE)?; } ColourScheme::GruvboxLight => { - canvas_colours.set_colours_from_palette(&*GRUVBOX_LIGHT_COLOUR_PALETTE)?; + canvas_colours.set_colours_from_palette(&GRUVBOX_LIGHT_COLOUR_PALETTE)?; } ColourScheme::Nord => { - canvas_colours.set_colours_from_palette(&*NORD_COLOUR_PALETTE)?; + canvas_colours.set_colours_from_palette(&NORD_COLOUR_PALETTE)?; } ColourScheme::NordLight => { - canvas_colours.set_colours_from_palette(&*NORD_LIGHT_COLOUR_PALETTE)?; + canvas_colours.set_colours_from_palette(&NORD_LIGHT_COLOUR_PALETTE)?; } ColourScheme::Custom => { if let Some(colors) = &config.colors { diff --git a/src/clap.rs b/src/clap.rs index c24498f3..b26c4c6a 100644 --- a/src/clap.rs +++ b/src/clap.rs @@ -230,7 +230,7 @@ pub fn build_app() -> Command<'static> { .long("color") .takes_value(true) .value_name("COLOR SCHEME") - .possible_values(&[ + .possible_values([ "default", "default-light", "gruvbox", diff --git a/src/components/data_table/draw.rs b/src/components/data_table/draw.rs index 0313153c..5459e51e 100644 --- a/src/components/data_table/draw.rs +++ b/src/components/data_table/draw.rs @@ -194,7 +194,7 @@ where } let show_header = inner_height > 1; - let header_height = if show_header { 1 } else { 0 }; + let header_height = u16::from(show_header); let table_gap = if !show_header || draw_loc.height < TABLE_GAP_HEIGHT_LIMIT { 0 } else { diff --git a/src/components/data_table/sortable.rs b/src/components/data_table/sortable.rs index f661937e..6980408c 100644 --- a/src/components/data_table/sortable.rs +++ b/src/components/data_table/sortable.rs @@ -336,7 +336,6 @@ where #[cfg(test)] mod test { - use super::*; #[derive(Clone, PartialEq, Eq, Debug)]