From 1affc4d5562bd72cff75c90e5bd5c99b507dace3 Mon Sep 17 00:00:00 2001 From: Dheepak Krishnamurthy Date: Thu, 11 Feb 2021 01:45:39 -0700 Subject: [PATCH] Run cargo clippy --- src/app.rs | 57 ++++++++++++++++++++-------------------------- src/calendar.rs | 29 ++++++++--------------- src/config.rs | 48 ++++++++------------------------------ src/help.rs | 8 ++++++- src/task_report.rs | 8 +++---- 5 files changed, 55 insertions(+), 95 deletions(-) diff --git a/src/app.rs b/src/app.rs index b1d94e9..e9d7bb1 100644 --- a/src/app.rs +++ b/src/app.rs @@ -510,7 +510,7 @@ impl TTApp { } let selected = self.task_table_state.selected().unwrap_or_default(); let task_id = self.tasks.lock().unwrap()[selected].id().unwrap_or_default(); - let task_uuid = self.tasks.lock().unwrap()[selected].uuid().clone(); + let task_uuid = *self.tasks.lock().unwrap()[selected].uuid(); let output = Command::new("task") .arg("rc.color=off") .arg(format!("{}", task_uuid)) @@ -571,8 +571,8 @@ impl TTApp { pub fn calculate_widths( &self, - tasks: &Vec>, - headers: &Vec, + tasks: &[Vec], + headers: &[String], maximum_column_width: u16, ) -> Vec { // naive implementation of calculate widths @@ -607,7 +607,7 @@ impl TTApp { widths[index] -= 1; } - return widths; + widths } fn draw_task_report(&mut self, f: &mut Frame, rect: Rect) { @@ -826,7 +826,7 @@ impl TTApp { } } else { i.checked_add(self.task_report_height as usize) - .unwrap_or(self.tasks.lock().unwrap().len()) + .unwrap_or_else(|| self.tasks.lock().unwrap().len()) } } None => 0, @@ -847,7 +847,7 @@ impl TTApp { 0 } } else { - i.checked_sub(self.task_report_height as usize).unwrap_or(0) + i.saturating_sub(self.task_report_height as usize) } } None => 0, @@ -863,7 +863,7 @@ impl TTApp { for (i, line) in data.trim().split('\n').enumerate() { let line = line.trim(); - let mut s = line.split(" "); + let mut s = line.split(' '); let name = s.next().unwrap_or_default(); let active = s.last().unwrap_or_default(); let definition = line.replacen(name, "", 1); @@ -893,7 +893,7 @@ impl TTApp { task.arg("rc.confirmation=off"); task.arg("export"); - let filter = if self.current_context_filter != "" { + let filter = if self.current_context_filter.is_empty() { let t = format!("{} {}", self.filter.as_str(), self.current_context_filter); t } else { @@ -994,7 +994,7 @@ impl TTApp { } let selected = self.task_table_state.selected().unwrap_or_default(); let task_id = self.tasks.lock().unwrap()[selected].id().unwrap_or_default(); - let task_uuid = self.tasks.lock().unwrap()[selected].uuid().clone(); + let task_uuid = *self.tasks.lock().unwrap()[selected].uuid(); let mut command = Command::new("task"); command.arg("rc.confirmation=off"); command.arg(format!("{}", task_uuid)).arg("modify"); @@ -1039,7 +1039,7 @@ impl TTApp { } let selected = self.task_table_state.selected().unwrap_or_default(); let task_id = self.tasks.lock().unwrap()[selected].id().unwrap_or_default(); - let task_uuid = self.tasks.lock().unwrap()[selected].uuid().clone(); + let task_uuid = *self.tasks.lock().unwrap()[selected].uuid(); let mut command = Command::new("task"); command.arg(format!("{}", task_uuid)).arg("annotate"); @@ -1111,7 +1111,7 @@ impl TTApp { Ok(output) => { let data = String::from_utf8_lossy(&output.stdout); for line in data.split('\n') { - for prefix in vec!["Virtual tags", "Virtual"] { + for prefix in &["Virtual tags", "Virtual"] { if line.starts_with(prefix) { let line = line.to_string(); let line = line.replace(prefix, ""); @@ -1137,7 +1137,7 @@ impl TTApp { } let selected = self.task_table_state.selected().unwrap_or_default(); let task_id = self.tasks.lock().unwrap()[selected].id().unwrap_or_default(); - let task_uuid = self.tasks.lock().unwrap()[selected].uuid().clone(); + let task_uuid = *self.tasks.lock().unwrap()[selected].uuid(); let mut command = "start"; for tag in TTApp::task_virtual_tags(task_uuid)?.split(' ') { if tag == "ACTIVE" { @@ -1161,7 +1161,7 @@ impl TTApp { } let selected = self.task_table_state.selected().unwrap_or_default(); let task_id = self.tasks.lock().unwrap()[selected].id().unwrap_or_default(); - let task_uuid = self.tasks.lock().unwrap()[selected].uuid().clone(); + let task_uuid = *self.tasks.lock().unwrap()[selected].uuid(); let output = Command::new("task") .arg("rc.confirmation=off") @@ -1183,7 +1183,7 @@ impl TTApp { } let selected = self.task_table_state.selected().unwrap_or_default(); let task_id = self.tasks.lock().unwrap()[selected].id().unwrap_or_default(); - let task_uuid = self.tasks.lock().unwrap()[selected].uuid().clone(); + let task_uuid = *self.tasks.lock().unwrap()[selected].uuid(); let output = Command::new("task").arg(format!("{}", task_uuid)).arg("done").output(); match output { Ok(_) => Ok(()), @@ -1212,7 +1212,7 @@ impl TTApp { } let selected = self.task_table_state.selected().unwrap_or_default(); let task_id = self.tasks.lock().unwrap()[selected].id().unwrap_or_default(); - let task_uuid = self.tasks.lock().unwrap()[selected].uuid().clone(); + let task_uuid = *self.tasks.lock().unwrap()[selected].uuid(); let r = Command::new("task").arg(format!("{}", task_uuid)).arg("edit").spawn(); match r { @@ -1300,17 +1300,13 @@ impl TTApp { if task.annotations().is_some() { add_tag(&mut task, "ANNOTATED".to_string()); } - if task.tags().is_some() { - if !task - .tags() - .unwrap() - .iter() - .filter(|s| !self.task_report_table.virtual_tags.contains(s)) - .next() - .is_none() - { - add_tag(&mut task, "TAGGED".to_string()); - } + if task.tags().is_some() && task + .tags() + .unwrap() + .iter() + .find(|s| !self.task_report_table.virtual_tags.contains(s)) + .is_some() { + add_tag(&mut task, "TAGGED".to_string()); } if task.mask().is_some() { add_tag(&mut task, "TEMPLATE".to_string()); @@ -1368,10 +1364,7 @@ impl TTApp { Key::Char('r') => self.update()?, Key::End | Key::Char('G') => self.task_report_bottom(), Key::Home => self.task_report_top(), - Key::Char('g') => match events.next()? { - Event::Input(Key::Char('g')) => self.task_report_top(), - _ => (), - }, + Key::Char('g') => if let Event::Input(Key::Char('g')) = events.next()? { self.task_report_top() }, Key::Down | Key::Char('j') => self.task_report_next(), Key::Up | Key::Char('k') => self.task_report_previous(), Key::PageDown | Key::Char('J') => self.task_report_next_page(), @@ -1473,13 +1466,13 @@ impl TTApp { } Key::Char('j') => { self.help_popup.scroll = self.help_popup.scroll.checked_add(1).unwrap_or(0); - let th = (self.help_popup.text_height as u16).checked_sub(1).unwrap_or(0); + let th = (self.help_popup.text_height as u16).saturating_sub(1); if self.help_popup.scroll > th { self.help_popup.scroll = th } } Key::Char('k') => { - self.help_popup.scroll = self.help_popup.scroll.checked_sub(1).unwrap_or(0); + self.help_popup.scroll = self.help_popup.scroll.saturating_sub(1); } _ => {} }, diff --git a/src/calendar.rs b/src/calendar.rs index bc61dbd..65c9d2f 100644 --- a/src/calendar.rs +++ b/src/calendar.rs @@ -129,7 +129,7 @@ impl<'a> Widget for Calendar<'a> { }) .collect(); - let mut startm = 0 as usize; + let mut startm = 0_usize; if self.months_per_row > area.width as usize / 8 / 3 || self.months_per_row == 0 { self.months_per_row = area.width as usize / 8 / 3; } @@ -152,11 +152,10 @@ impl<'a> Widget for Calendar<'a> { loop { let endm = std::cmp::min(startm + self.months_per_row, 12); let mut x = area.x + startx; - for c in startm..endm { + for (c, d) in days.iter_mut().enumerate().take(endm).skip(startm) { if c > startm { x += 1; } - let d = &mut days[c]; let m = d.0.month() as usize; let s = format!("{:^20}", month_names[m - 1]); let style = Style::default().bg(self.title_background_color); @@ -169,25 +168,15 @@ impl<'a> Widget for Calendar<'a> { } y += 1; let mut x = area.x + startx; - for c in startm..endm { - let d = &mut days[c]; + for d in days.iter_mut().take(endm).skip(startm) { let m = d.0.month() as usize; let style = Style::default().bg(self.title_background_color); - if m == today.month() as usize && self.year + year as i32 == today.year() { - buf.set_string( - x as u16, - y, - "Su Mo Tu We Th Fr Sa", - style.add_modifier(Modifier::UNDERLINED), - ); - } else { - buf.set_string( - x as u16, - y, - "Su Mo Tu We Th Fr Sa", - style.add_modifier(Modifier::UNDERLINED), - ); - } + buf.set_string( + x as u16, + y, + "Su Mo Tu We Th Fr Sa", + style.add_modifier(Modifier::UNDERLINED), + ); x += 21 + 1; } y += 1; diff --git a/src/config.rs b/src/config.rs index 6ddc869..45630d8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -95,12 +95,12 @@ impl Config { uda_selection_dim: Self::get_uda_selection_dim(), uda_selection_blink: Self::get_uda_selection_blink(), uda_calendar_months_per_row: Self::get_uda_months_per_row(), - uda_style_calendar_title: Self::get_uda_style("calendar.title").unwrap_or(TColor::new( + uda_style_calendar_title: Self::get_uda_style("calendar.title").unwrap_or_else(|| TColor::new( Color::Reset, Color::Reset, vec![], )), - uda_style_context_active: Self::get_uda_style("context.active").unwrap_or(TColor::new( + uda_style_context_active: Self::get_uda_style("context.active").unwrap_or_else(|| TColor::new( Color::Reset, Color::Reset, vec![], @@ -115,7 +115,7 @@ impl Config { fn get_uda_style(config: &str) -> Option { let c = format!("uda.taskwarrior-tui.style.{}", config); let s = Self::get_config(&c); - if s == "" { + if s.is_empty() { None } else { Some(Self::get_tcolor(&s)) @@ -317,19 +317,11 @@ impl Config { } fn get_uda_task_report_show_info() -> bool { - let s = Self::get_config("uda.taskwarrior-tui.task-report.show-info"); - match s.get_bool() { - Some(b) => b, - None => true, - } + Self::get_config("uda.taskwarrior-tui.task-report.show-info").get_bool().unwrap_or(true) } fn get_uda_task_report_looping() -> bool { - let s = Self::get_config("uda.taskwarrior-tui.task-report.looping"); - match s.get_bool() { - Some(b) => b, - None => true, - } + Self::get_config("uda.taskwarrior-tui.task-report.looping").get_bool().unwrap_or(true) } fn get_uda_selection_indicator() -> String { @@ -342,43 +334,23 @@ impl Config { } fn get_uda_selection_bold() -> bool { - let s = Self::get_config("uda.taskwarrior-tui.selection.bold"); - match s.get_bool() { - Some(b) => b, - None => true, - } + Self::get_config("uda.taskwarrior-tui.selection.bold").get_bool().unwrap_or(true) } fn get_uda_selection_italic() -> bool { - let s = Self::get_config("uda.taskwarrior-tui.selection.italic"); - match s.get_bool() { - Some(b) => b, - None => false, - } + Self::get_config("uda.taskwarrior-tui.selection.italic").get_bool().unwrap_or(false) } fn get_uda_selection_dim() -> bool { - let s = Self::get_config("uda.taskwarrior-tui.selection.dim"); - match s.get_bool() { - Some(b) => b, - None => false, - } + Self::get_config("uda.taskwarrior-tui.selection.dim").get_bool().unwrap_or(false) } fn get_uda_selection_blink() -> bool { - let s = Self::get_config("uda.taskwarrior-tui.selection.blink"); - match s.get_bool() { - Some(b) => b, - None => false, - } + Self::get_config("uda.taskwarrior-tui.selection.blink").get_bool().unwrap_or(false) } fn get_uda_months_per_row() -> usize { - let s = Self::get_config("uda.taskwarrior-tui.calendar.months-per-row"); - match s.parse::() { - Ok(i) => i, - Err(e) => 4, - } + Self::get_config("uda.taskwarrior-tui.calendar.months-per-row").parse::().unwrap_or(4) } } diff --git a/src/help.rs b/src/help.rs index 7a3741a..824fbf5 100644 --- a/src/help.rs +++ b/src/help.rs @@ -21,11 +21,17 @@ impl Help { Self { title: "Help".to_string(), scroll: 0, - text_height: TEXT.lines().map(|line| Spans::from(format!("{}\n", line))).count(), + text_height: TEXT.lines().count(), } } } +impl Default for Help { + fn default() -> Self { + Self::new() + } +} + impl Widget for &Help { fn render(self, area: Rect, buf: &mut Buffer) { let text: Vec = TEXT.lines().map(|line| Spans::from(format!("{}\n", line))).collect(); diff --git a/src/task_report.rs b/src/task_report.rs index 7fc1580..d0a33f1 100644 --- a/src/task_report.rs +++ b/src/task_report.rs @@ -121,7 +121,7 @@ impl TaskReportTable { // get all tasks as their string representation for task in tasks { - if self.columns.len() == 0 { + if self.columns.is_empty() { break; } let mut item = vec![]; @@ -246,7 +246,7 @@ impl TaskReportTable { end += 1; } d.truncate(end); - if d != task.description().to_string() { + if d != *task.description() { d = format!("{}…", d); } format!("{}{}", d, c) @@ -258,10 +258,10 @@ impl TaskReportTable { end += 1; } d.truncate(end); - if d != task.description().to_string() { + if d != *task.description() { d = format!("{}…", d); } - format!("{}", d) + d.to_string() } "description" => task.description().to_string(), "urgency" => match &task.urgency() {