From ceea5eacd814c5bbd8c6789610c87a882e3d72cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Wed, 31 Mar 2021 23:42:16 +0900 Subject: [PATCH] clippy lint --- helix-lsp/src/client.rs | 22 +++++++++++----------- helix-lsp/src/transport.rs | 8 ++++---- helix-term/src/application.rs | 2 +- helix-term/src/commands.rs | 17 ++++++++--------- helix-term/src/main.rs | 2 +- helix-term/src/ui/editor.rs | 6 +++--- helix-term/src/ui/menu.rs | 4 ++-- helix-term/src/ui/mod.rs | 5 +---- helix-term/src/ui/prompt.rs | 9 ++------- helix-view/src/document.rs | 8 +++----- helix-view/src/editor.rs | 1 + helix-view/src/tree.rs | 19 ++++++++----------- 12 files changed, 45 insertions(+), 58 deletions(-) diff --git a/helix-lsp/src/client.rs b/helix-lsp/src/client.rs index 1d84c4f3..74f5fa06 100644 --- a/helix-lsp/src/client.rs +++ b/helix-lsp/src/client.rs @@ -384,11 +384,13 @@ impl Client { let capabilities = self.capabilities.as_ref().unwrap(); let sync_capabilities = match capabilities.text_document_sync { - Some(lsp::TextDocumentSyncCapability::Kind(kind)) => kind, - Some(lsp::TextDocumentSyncCapability::Options(lsp::TextDocumentSyncOptions { - change: Some(kind), - .. - })) => kind, + Some( + lsp::TextDocumentSyncCapability::Kind(kind) + | lsp::TextDocumentSyncCapability::Options(lsp::TextDocumentSyncOptions { + change: Some(kind), + .. + }), + ) => kind, // None | SyncOptions { changes: None } _ => return Ok(()), }; @@ -537,9 +539,8 @@ impl Client { let capabilities = self.capabilities.as_ref().unwrap(); // check if we're able to format - let _capabilities = match capabilities.document_formatting_provider { - Some(lsp::OneOf::Left(true)) => (), - Some(lsp::OneOf::Right(_)) => (), + match capabilities.document_formatting_provider { + Some(lsp::OneOf::Left(true) | lsp::OneOf::Right(_)) => (), // None | Some(false) _ => return Ok(Vec::new()), }; @@ -567,9 +568,8 @@ impl Client { let capabilities = self.capabilities.as_ref().unwrap(); // check if we're able to format - let _capabilities = match capabilities.document_range_formatting_provider { - Some(lsp::OneOf::Left(true)) => (), - Some(lsp::OneOf::Right(_)) => (), + match capabilities.document_range_formatting_provider { + Some(lsp::OneOf::Left(true) | lsp::OneOf::Right(_)) => (), // None | Some(false) _ => return Ok(Vec::new()), }; diff --git a/helix-lsp/src/transport.rs b/helix-lsp/src/transport.rs index ff1eedaf..d3e25b9c 100644 --- a/helix-lsp/src/transport.rs +++ b/helix-lsp/src/transport.rs @@ -19,7 +19,7 @@ use smol::{ Executor, }; -pub(crate) enum Payload { +pub enum Payload { Request { chan: Sender>, value: jsonrpc::MethodCall, @@ -40,7 +40,7 @@ enum Message { Call(jsonrpc::Call), } -pub(crate) struct Transport { +pub struct Transport { incoming: Sender, outgoing: Receiver, @@ -68,8 +68,8 @@ impl Transport { stderr, incoming, outgoing, - pending_requests: Default::default(), - headers: Default::default(), + pending_requests: HashMap::default(), + headers: HashMap::default(), }; ex.spawn(transport.duplex()).detach(); diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 2a813081..53fd086b 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -122,7 +122,7 @@ impl Application { pub fn handle_terminal_events(&mut self, event: Option>) { let mut cx = crate::compositor::Context { editor: &mut self.editor, - executor: &self.executor, + executor: self.executor, callbacks: &mut self.callbacks, scroll: None, }; diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 844cb429..9da65813 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -14,6 +14,7 @@ use crate::{ ui::{self, Completion, Picker, Popup, Prompt, PromptEvent}, }; +use std::borrow::Cow; use std::path::{Path, PathBuf}; use helix_view::{ @@ -333,7 +334,7 @@ where let text = doc.text().slice(..); let selection = doc.selection(view_id).transform(|mut range| { - if let Some(pos) = search::find_nth_next(text, ch, range.head, count, inclusive) { + search::find_nth_next(text, ch, range.head, count, inclusive).map_or(range, |pos| { if extend { Range::new(range.anchor, pos) } else { @@ -341,9 +342,7 @@ where Range::new(range.head, pos) } // or (pos, pos) to move to found val - } else { - range - } + }) }); doc.set_selection(view_id, selection); @@ -642,7 +641,7 @@ fn _search(doc: &mut Document, view_id: ViewId, contents: &str, regex: &Regex) { let start = doc.selection(view_id).cursor(); // TODO: use find_at to find the next match after the cursor, loop around the end - if let Some(mat) = regex.find_at(&contents, start) { + if let Some(mat) = regex.find_at(contents, start) { let start = text.byte_to_char(mat.start()); let end = text.byte_to_char(mat.end()); let selection = Selection::single(start, end - 1); @@ -859,15 +858,15 @@ pub fn command_mode(cx: &mut Context) { let parts = input.split_ascii_whitespace().collect::>(); match *parts.as_slice() { - ["q"] | ["quit"] => { + ["q" | "quit"] => { editor.close(editor.view().id); // editor.should_close = true, } - ["o", path] | ["open", path] => { + ["o" | "open", path] => { use helix_view::editor::Action; editor.open(path.into(), Action::Replace); } - ["w"] | ["write"] => { + ["w" | "write"] => { // TODO: non-blocking via save() command let id = editor.view().doc; let doc = &mut editor.documents[id]; @@ -1487,7 +1486,7 @@ pub fn yank(cx: &mut Context) { let values: Vec = doc .selection(view_id) .fragments(doc.text().slice(..)) - .map(|cow| cow.into_owned()) + .map(Cow::into_owned) .collect(); // TODO: allow specifying reg diff --git a/helix-term/src/main.rs b/helix-term/src/main.rs index dc6f31f4..33792947 100644 --- a/helix-term/src/main.rs +++ b/helix-term/src/main.rs @@ -77,7 +77,7 @@ fn main() { use helix_core::syntax::{Loader, LOADER}; let toml = include_str!("../../languages.toml"); LOADER.get_or_init(|| { - let config = toml::from_str(&toml).expect("Could not parse languages.toml"); + let config = toml::from_str(toml).expect("Could not parse languages.toml"); Loader::new(config) }); diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 7ceeb6ca..d0822500 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -58,13 +58,13 @@ impl EditorView { viewport.width - OFFSET, viewport.height.saturating_sub(1), ); // - 1 for statusline - self.render_buffer(&doc, view, area, surface, theme, is_focused); + self.render_buffer(doc, view, area, surface, theme, is_focused); // clear with background color // TODO: this seems to prevent setting style later // surface.set_style(viewport, theme.get("ui.background")); - self.render_diagnostics(&doc, view, area, surface, theme, is_focused); + self.render_diagnostics(doc, view, area, surface, theme, is_focused); let area = Rect::new( viewport.x, @@ -72,7 +72,7 @@ impl EditorView { viewport.width, 1, ); - self.render_statusline(&doc, area, surface, theme, is_focused); + self.render_statusline(doc, area, surface, theme, is_focused); // render status if let Some(status_msg) = &self.status_msg { diff --git a/helix-term/src/ui/menu.rs b/helix-term/src/ui/menu.rs index 5c3ff654..fbd25a6d 100644 --- a/helix-term/src/ui/menu.rs +++ b/helix-term/src/ui/menu.rs @@ -90,13 +90,13 @@ impl Menu { pub fn move_up(&mut self) { // TODO: wrap around to end - let pos = self.cursor.map(|i| i.saturating_sub(1)).unwrap_or(0) % self.options.len(); + let pos = self.cursor.map_or(0, |i| i.saturating_sub(1)) % self.options.len(); self.cursor = Some(pos); self.adjust_scroll(); } pub fn move_down(&mut self) { - let pos = self.cursor.map(|i| i + 1).unwrap_or(0) % self.options.len(); + let pos = self.cursor.map_or(0, |i| i + 1) % self.options.len(); self.cursor = Some(pos); self.adjust_scroll(); } diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index 341a30e0..479e684a 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -149,10 +149,7 @@ pub mod completers { .build() .filter_map(|file| { file.ok().map(|entry| { - let is_dir = entry - .file_type() - .map(|entry| entry.is_dir()) - .unwrap_or(false); + let is_dir = entry.file_type().map_or(false, |entry| entry.is_dir()); let mut path = entry.path().strip_prefix(&dir).unwrap().to_path_buf(); diff --git a/helix-term/src/ui/prompt.rs b/helix-term/src/ui/prompt.rs index c61f0bd1..3588853e 100644 --- a/helix-term/src/ui/prompt.rs +++ b/helix-term/src/ui/prompt.rs @@ -81,8 +81,7 @@ impl Prompt { if self.completion.is_empty() { return; } - let index = - self.completion_selection_index.map(|i| i + 1).unwrap_or(0) % self.completion.len(); + let index = self.completion_selection_index.map_or(0, |i| i + 1) % self.completion.len(); self.completion_selection_index = Some(index); let (range, item) = &self.completion[index]; @@ -183,11 +182,7 @@ impl Component for Prompt { // char or shift char KeyEvent { code: KeyCode::Char(c), - modifiers: KeyModifiers::NONE, - } - | KeyEvent { - code: KeyCode::Char(c), - modifiers: KeyModifiers::SHIFT, + modifiers: KeyModifiers::NONE | KeyModifiers::SHIFT, } => { self.insert_char(c); (self.callback_fn)(cx.editor, &self.line, PromptEvent::Update); diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 93b5e854..d63e8591 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -242,7 +242,7 @@ impl Document { }); } - let success = self._apply(&transaction, view_id); + let success = self._apply(transaction, view_id); self.modified = true; // TODO: be smarter about modified by keeping track of saved version instead. That way if @@ -342,8 +342,7 @@ impl Document { self.language .as_ref() .and_then(|config| config.indent.as_ref()) - .map(|config| config.tab_width) - .unwrap_or(4) // fallback to 4 columns + .map_or(4, |config| config.tab_width) // fallback to 4 columns } /// Returns a string containing a single level of indentation. @@ -351,8 +350,7 @@ impl Document { self.language .as_ref() .and_then(|config| config.indent.as_ref()) - .map(|config| config.unit.as_str()) - .unwrap_or(" ") // fallback to 2 spaces + .map_or(" ", |config| config.unit.as_str()) // fallback to 2 spaces // " ".repeat(TAB_WIDTH) } diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index f74bbb13..b2408eb9 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -16,6 +16,7 @@ pub struct Editor { pub executor: &'static smol::Executor<'static>, } +#[derive(Copy, Clone)] pub enum Action { Replace, HorizontalSplit, diff --git a/helix-view/src/tree.rs b/helix-view/src/tree.rs index f7ef7806..5fed2d5a 100644 --- a/helix-view/src/tree.rs +++ b/helix-view/src/tree.rs @@ -29,14 +29,14 @@ pub enum Content { impl Node { pub fn container(layout: Layout) -> Self { - Node { + Self { parent: ViewId::default(), content: Content::Container(Box::new(Container::new(layout))), } } pub fn view(view: View) -> Self { - Node { + Self { parent: ViewId::default(), content: Content::View(Box::new(view)), } @@ -414,15 +414,12 @@ impl Tree { let mut iter = iter.skip_while(|&(key, _view)| key != self.focus); iter.next(); // take the focused value - match iter.next() { - Some((key, _)) => { - self.focus = key; - } - None => { - // extremely crude, take the first item again - let (key, _) = self.traverse().next().unwrap(); - self.focus = key; - } + if let Some((key, _)) = iter.next() { + self.focus = key; + } else { + // extremely crude, take the first item again + let (key, _) = self.traverse().next().unwrap(); + self.focus = key; } } }