From 9dd51e75e0cdff46dad397508573d43d732ffaf8 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Tue, 7 May 2024 11:35:39 -0400 Subject: [PATCH] Resolve new Clippy lints --- helix-core/src/indent.rs | 2 +- helix-core/src/syntax.rs | 13 ++++++++----- helix-core/src/transaction.rs | 9 +++++++-- helix-core/tests/indent.rs | 3 ++- helix-lsp/src/file_event.rs | 2 +- helix-term/src/application.rs | 2 +- helix-view/src/register.rs | 4 ++-- helix-view/src/tree.rs | 2 +- 8 files changed, 23 insertions(+), 14 deletions(-) diff --git a/helix-core/src/indent.rs b/helix-core/src/indent.rs index ca72fc3a..fd2b6c95 100644 --- a/helix-core/src/indent.rs +++ b/helix-core/src/indent.rs @@ -740,7 +740,7 @@ fn init_indent_query<'a, 'b>( crate::syntax::PARSER.with(|ts_parser| { let mut ts_parser = ts_parser.borrow_mut(); - let mut cursor = ts_parser.cursors.pop().unwrap_or_else(QueryCursor::new); + let mut cursor = ts_parser.cursors.pop().unwrap_or_default(); let query_result = query_indents( query, syntax, diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index bbea9bbe..8fda2935 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -21,7 +21,7 @@ use std::{ borrow::Cow, cell::RefCell, collections::{HashMap, HashSet, VecDeque}, - fmt::{self, Display}, + fmt::{self, Display, Write}, hash::{Hash, Hasher}, mem::replace, path::{Path, PathBuf}, @@ -729,8 +729,11 @@ pub fn read_query(language: &str, filename: &str) -> String { .replace_all(&query, |captures: ®ex::Captures| { captures[1] .split(',') - .map(|language| format!("\n{}\n", read_query(language, filename))) - .collect::() + .fold(String::new(), |mut output, language| { + // `write!` to a String cannot fail. + write!(output, "\n{}\n", read_query(language, filename)).unwrap(); + output + }) }) .to_string() } @@ -1245,7 +1248,7 @@ impl Syntax { PARSER.with(|ts_parser| { let ts_parser = &mut ts_parser.borrow_mut(); ts_parser.parser.set_timeout_micros(1000 * 500); // half a second is pretty generours - let mut cursor = ts_parser.cursors.pop().unwrap_or_else(QueryCursor::new); + let mut cursor = ts_parser.cursors.pop().unwrap_or_default(); // TODO: might need to set cursor range cursor.set_byte_range(0..usize::MAX); cursor.set_match_limit(TREE_SITTER_MATCH_LIMIT); @@ -1419,7 +1422,7 @@ impl Syntax { // Reuse a cursor from the pool if available. let mut cursor = PARSER.with(|ts_parser| { let highlighter = &mut ts_parser.borrow_mut(); - highlighter.cursors.pop().unwrap_or_else(QueryCursor::new) + highlighter.cursors.pop().unwrap_or_default() }); // The `captures` iterator borrows the `Tree` and the `QueryCursor`, which diff --git a/helix-core/src/transaction.rs b/helix-core/src/transaction.rs index f5a49cc1..f24f2094 100644 --- a/helix-core/src/transaction.rs +++ b/helix-core/src/transaction.rs @@ -378,7 +378,9 @@ impl ChangeSet { macro_rules! map { ($map: expr, $i: expr) => { loop { - let Some((pos, assoc)) = positions.peek_mut() else { return; }; + let Some((pos, assoc)) = positions.peek_mut() else { + return; + }; if **pos < old_pos { // Positions are not sorted, revert to the last Operation that // contains this position and continue iterating from there. @@ -405,7 +407,10 @@ impl ChangeSet { debug_assert!(old_pos <= **pos, "Reverse Iter across changeset works"); continue 'outer; } - let Some(new_pos) = $map(**pos, *assoc) else { break; }; + #[allow(clippy::redundant_closure_call)] + let Some(new_pos) = $map(**pos, *assoc) else { + break; + }; **pos = new_pos; positions.next(); } diff --git a/helix-core/tests/indent.rs b/helix-core/tests/indent.rs index 31946c56..56b4d2ba 100644 --- a/helix-core/tests/indent.rs +++ b/helix-core/tests/indent.rs @@ -36,7 +36,7 @@ fn test_treesitter_indent_rust_helix() { .unwrap(); let files = String::from_utf8(files.stdout).unwrap(); - let ignored_files = vec![ + let ignored_files = [ // Contains many macros that tree-sitter does not parse in a meaningful way and is otherwise not very interesting "helix-term/src/health.rs", ]; @@ -45,6 +45,7 @@ fn test_treesitter_indent_rust_helix() { if ignored_files.contains(&file) { continue; } + #[allow(clippy::single_range_in_vec_init)] let ignored_lines: Vec> = match file { "helix-term/src/application.rs" => vec![ // We can't handle complicated indent rules inside macros (`json!` in this case) since diff --git a/helix-lsp/src/file_event.rs b/helix-lsp/src/file_event.rs index 8f4c1675..c7297d67 100644 --- a/helix-lsp/src/file_event.rs +++ b/helix-lsp/src/file_event.rs @@ -139,7 +139,7 @@ impl Handler { registration_id ); - let entry = state.entry(client_id).or_insert_with(ClientState::default); + let entry = state.entry(client_id).or_default(); entry.client = client; let mut builder = GlobSetBuilder::new(); diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 6bdf60bc..f71eed20 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -724,7 +724,7 @@ impl Application { } Notification::PublishDiagnostics(mut params) => { let path = match params.uri.to_file_path() { - Ok(path) => helix_stdx::path::normalize(&path), + Ok(path) => helix_stdx::path::normalize(path), Err(_) => { log::error!("Unsupported file URI: {}", params.uri); return; diff --git a/helix-view/src/register.rs b/helix-view/src/register.rs index 5592c6af..a7f35612 100644 --- a/helix-view/src/register.rs +++ b/helix-view/src/register.rs @@ -123,7 +123,7 @@ impl Registers { _ => unreachable!(), }; let contents = self.clipboard_provider.get_contents(clipboard_type)?; - let saved_values = self.inner.entry(name).or_insert_with(Vec::new); + let saved_values = self.inner.entry(name).or_default(); if !contents_are_saved(saved_values, &contents) { anyhow::bail!("Failed to push to register {name}: clipboard does not match register contents"); @@ -140,7 +140,7 @@ impl Registers { Ok(()) } _ => { - self.inner.entry(name).or_insert_with(Vec::new).push(value); + self.inner.entry(name).or_default().push(value); Ok(()) } } diff --git a/helix-view/src/tree.rs b/helix-view/src/tree.rs index 307dbc71..be8bd4e5 100644 --- a/helix-view/src/tree.rs +++ b/helix-view/src/tree.rs @@ -560,7 +560,7 @@ impl Tree { id } else { // extremely crude, take the last item - let (key, _) = self.traverse().rev().next().unwrap(); + let (key, _) = self.traverse().next_back().unwrap(); key } }