From 829a945182e09245eef0fec3e9bac1e4fad71e0e Mon Sep 17 00:00:00 2001 From: Caleb Cox Date: Tue, 15 Oct 2024 20:59:01 -0500 Subject: [PATCH] Enable nursery lints --- cli/src/import.rs | 2 +- cli/src/main.rs | 16 ++++++---------- cli/src/message_components.rs | 8 ++++---- cli/src/truncate.rs | 2 +- cli/src/tui/app.rs | 8 ++++---- cli/src/tui/mod.rs | 28 ++++++++++++---------------- cli/src/tui/monotonic_counter.rs | 2 +- cli/src/tui/multiselect_list.rs | 2 +- cli/src/tui/navigable_list.rs | 15 +++++---------- cli/src/tui/tree_list.rs | 2 +- 10 files changed, 36 insertions(+), 49 deletions(-) diff --git a/cli/src/import.rs b/cli/src/import.rs index 0afba82..36ee69e 100644 --- a/cli/src/import.rs +++ b/cli/src/import.rs @@ -58,7 +58,7 @@ where // Add multiple messages to the database #[allow(clippy::module_name_repetitions)] pub async fn import_messages( - db: &mut Database, + db: &Database, config: &Option, new_messages: Vec, ) -> Result> { diff --git a/cli/src/main.rs b/cli/src/main.rs index 8bdcc47..aa60e50 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,5 +1,5 @@ -#![deny(clippy::clone_on_ref_ptr)] -#![deny(clippy::pedantic)] +#![warn(clippy::clone_on_ref_ptr, clippy::pedantic, clippy::nursery)] +#![allow(clippy::future_not_send, clippy::missing_const_for_fn)] mod cli; mod config; @@ -111,7 +111,7 @@ fn states_from_view_message_state(state: ViewMessageState) -> Vec { async fn run( config: Option, - mut db: Database, + db: Database, ) -> Result<()> { let cli = Cli::parse(); let formatter = create_formatter(&cli); @@ -132,17 +132,13 @@ async fn run( content, state: Some(cli_state), }]; - let messages = import_messages(&mut db, &config, raw_messages).await?; + let messages = import_messages(&db, &config, raw_messages).await?; print!("{}", formatter.format_messages(&messages)?); } Command::Import { format } => { - let messages = import_messages( - &mut db, - &config, - read_messages_stdin(stdin().lock(), format), - ) - .await?; + let messages = + import_messages(&db, &config, read_messages_stdin(stdin().lock(), format)).await?; print!("{}", formatter.format_messages(&messages)?); } diff --git a/cli/src/message_components.rs b/cli/src/message_components.rs index 4828b39..aa7d9f6 100644 --- a/cli/src/message_components.rs +++ b/cli/src/message_components.rs @@ -18,7 +18,7 @@ impl MessageComponents { // Attempt to truncate the combined length of the message components down // to max_length. If this isn't possible, the message components will be // truncated as much as possible. - pub fn truncate(self, max_length: usize) -> MessageComponents { + pub fn truncate(self, max_length: usize) -> Self { let total_length = 8 + self.content.width() + self.mailbox.width() + self.time.len() + self.appendix.len(); if total_length <= max_length { @@ -30,14 +30,14 @@ impl MessageComponents { let others_length = total_length - self.mailbox.width(); if others_length + 4 <= max_length { let mailbox = truncate_string(&self.mailbox, max_length - others_length).0; - return MessageComponents { mailbox, ..self }; + return Self { mailbox, ..self }; } // Next try to truncate the content let others_length = total_length - self.content.width(); if others_length + 4 <= max_length { let content = truncate_string(&self.content, max_length - others_length).0; - return MessageComponents { content, ..self }; + return Self { content, ..self }; } // Lastly, truncate the content and the mailbox @@ -45,7 +45,7 @@ impl MessageComponents { let mailbox_and_content_length = max(max_length.saturating_sub(others_length) / 2, 4); let mailbox = truncate_string(&self.mailbox, mailbox_and_content_length).0; let content = truncate_string(&self.content, mailbox_and_content_length).0; - MessageComponents { + Self { content, mailbox, ..self diff --git a/cli/src/truncate.rs b/cli/src/truncate.rs index f117e17..fc864c0 100644 --- a/cli/src/truncate.rs +++ b/cli/src/truncate.rs @@ -20,7 +20,7 @@ pub struct TruncatedLine { impl TruncatedLine { // Create a new instance pub fn new(max_columns: usize) -> Self { - TruncatedLine { + Self { available_columns: max_columns, sections: Vec::default(), } diff --git a/cli/src/tui/app.rs b/cli/src/tui/app.rs index 3bc8a58..a2da4cc 100644 --- a/cli/src/tui/app.rs +++ b/cli/src/tui/app.rs @@ -61,10 +61,10 @@ impl App { db: Database, initial_mailbox: Option, initial_states: Vec, - ) -> Result { + ) -> Result { let db = Arc::new(db); let (worker_tx, worker_rx) = spawn(Arc::clone(&db)); - let mut app = App { + let mut app = Self { active_pane: Pane::Messages, mailboxes: TreeList::new(), messages: MultiselectList::new(), @@ -145,7 +145,7 @@ impl App { } // Update the mailboxes list - pub fn update_mailboxes(&mut self) -> Result<()> { + pub fn update_mailboxes(&self) -> Result<()> { self.worker_tx.send(Request::LoadMailboxes( Filter::new().with_states(self.get_active_states()), ))?; @@ -153,7 +153,7 @@ impl App { } // Update the messages list based on the mailbox and other filters - pub fn update_messages(&mut self) -> Result<()> { + pub fn update_messages(&self) -> Result<()> { let filter = self.get_display_filter(); self.worker_tx.send(Request::LoadMessages(filter))?; Ok(()) diff --git a/cli/src/tui/mod.rs b/cli/src/tui/mod.rs index 94ff0a5..da8c411 100644 --- a/cli/src/tui/mod.rs +++ b/cli/src/tui/mod.rs @@ -134,19 +134,19 @@ fn handle_mailbox_key(app: &mut App, key: KeyEvent) -> Result<()> { app.mailboxes.parent(); } KeyCode::Char('a') => { - if let Some(active_mailbox) = old_active_mailbox.clone() { + if let Some(active_mailbox) = old_active_mailbox { app.set_mailbox_message_state(active_mailbox, State::Archived)?; } return Ok(()); } KeyCode::Char('r') => { - if let Some(active_mailbox) = old_active_mailbox.clone() { + if let Some(active_mailbox) = old_active_mailbox { app.set_mailbox_message_state(active_mailbox, State::Read)?; } return Ok(()); } KeyCode::Char('u') => { - if let Some(active_mailbox) = old_active_mailbox.clone() { + if let Some(active_mailbox) = old_active_mailbox { app.set_mailbox_message_state(active_mailbox, State::Unread)?; } return Ok(()); @@ -163,11 +163,9 @@ fn handle_mailbox_key(app: &mut App, key: KeyEvent) -> Result<()> { // If the new active mailbox is a descendant of the old one or if there wasn't an old active mailbox, the // messages list can be optimistically updated by filtering against the new active mailbox instead of needing // to refresh the whole list - let local_update = if let Some(old_active_mailbox) = old_active_mailbox { + let local_update = old_active_mailbox.map_or(true, |old_active_mailbox| { old_active_mailbox.is_ancestor_of(active_mailbox) - } else { - true - }; + }); if local_update { // Optimistically update the messages list @@ -258,7 +256,7 @@ fn ui(frame: &mut Frame, app: &mut App) { } // Render the footer section of the UI -fn render_footer(frame: &mut Frame, app: &mut App, area: Rect) { +fn render_footer(frame: &mut Frame, app: &App, area: Rect) { const ACTIVE_STYLE: Style = Style::new().fg(Color::Black).bg(Color::Green); const INACTIVE_STYLE: Style = Style::new(); const SELECTING_STYLE: Style = Style::new().fg(Color::LightBlue); @@ -338,10 +336,9 @@ fn render_mailboxes(frame: &mut Frame, app: &mut App, area: Rect) .border_style(border_style) .title(format!( "Mailboxes ({}{})", - match app.mailboxes.get_cursor() { - None => String::new(), - Some(index) => format!("{}/", index + 1), - }, + app.mailboxes + .get_cursor() + .map_or_else(String::new, |index| format!("{}/", index + 1)), app.mailboxes.get_items().len() )), ) @@ -398,10 +395,9 @@ fn render_messages(frame: &mut Frame, app: &mut App, area: Rect) .border_style(border_style) .title(format!( "Messages ({}{})", - match app.messages.get_cursor() { - None => String::new(), - Some(index) => format!("{}/", index + 1), - }, + app.messages + .get_cursor() + .map_or_else(String::new, |index| format!("{}/", index + 1)), app.messages.get_items().len() )), ) diff --git a/cli/src/tui/monotonic_counter.rs b/cli/src/tui/monotonic_counter.rs index a364cc4..0e7ea5a 100644 --- a/cli/src/tui/monotonic_counter.rs +++ b/cli/src/tui/monotonic_counter.rs @@ -18,7 +18,7 @@ pub struct MonotonicCounter { impl MonotonicCounter { // Create a new monotonic counter pub fn new() -> Self { - MonotonicCounter { + Self { last_id: Arc::new(AtomicU64::new(0)), } } diff --git a/cli/src/tui/multiselect_list.rs b/cli/src/tui/multiselect_list.rs index 5b914f1..3dd7f63 100644 --- a/cli/src/tui/multiselect_list.rs +++ b/cli/src/tui/multiselect_list.rs @@ -73,7 +73,7 @@ where { // Create a new list with no items pub fn new() -> Self { - MultiselectList { + Self { state: ListState::default(), items: vec![], selected_items: HashSet::new(), diff --git a/cli/src/tui/navigable_list.rs b/cli/src/tui/navigable_list.rs index c229cd3..9989173 100644 --- a/cli/src/tui/navigable_list.rs +++ b/cli/src/tui/navigable_list.rs @@ -42,10 +42,7 @@ where self.set_cursor(match self.get_items().len() { 0 => None, num_items => { - let new_index = (match self.get_cursor() { - None => -1, - Some(index) => index as i32, - }) + change; + let new_index = self.get_cursor().map_or(-1, |index| index as i32) + change; // Bounds check the new index Some((new_index).clamp(0, num_items as i32 - 1) as usize) } @@ -75,20 +72,18 @@ where // Replace the list's items with a new set of items fn replace_items(&mut self, items: Vec) { - let cursor_candidates = match self.get_cursor() { - None => vec![], + let cursor_candidates = self.get_cursor().map_or_else(Vec::new, |index| { // The items at and after the cursor are the first candidates, // starting with the items closest to the cursor, followed by // the items before the cursor, again starting with the items // closest to the cursor - Some(index) => self - .get_items() + self.get_items() .iter() .skip(index) .chain(self.get_items().iter().take(index).rev()) .map(Keyed::get_key) - .collect(), - }; + .collect() + }); *self.get_items_mut() = items; diff --git a/cli/src/tui/tree_list.rs b/cli/src/tui/tree_list.rs index 2e081d3..69f25e3 100644 --- a/cli/src/tui/tree_list.rs +++ b/cli/src/tui/tree_list.rs @@ -34,7 +34,7 @@ where impl TreeList { // Create a new list with no items pub fn new() -> Self { - TreeList { + Self { state: ListState::default(), items: vec![], }