From 3de59dac42c05de43beba2f7d09b07111c711394 Mon Sep 17 00:00:00 2001 From: a-kenji Date: Wed, 15 Jun 2022 14:03:11 +0200 Subject: [PATCH] fix(clippy): clippy fixes (#1508) * fix(clippy): clippy fixes * chore(fmt): cargo fmt --- default-plugins/status-bar/src/first_line.rs | 2 +- src/commands.rs | 65 +++++++++---------- src/sessions.rs | 6 +- zellij-client/src/stdin_ansi_parser.rs | 15 ++--- zellij-server/src/os_input_output.rs | 9 ++- zellij-server/src/panes/grid.rs | 34 +++++----- zellij-server/src/panes/link_handler.rs | 42 ++++++------ zellij-server/src/panes/terminal_pane.rs | 2 + zellij-server/src/screen.rs | 2 +- zellij-server/src/tab/mod.rs | 3 +- .../src/tab/unit/tab_integration_tests.rs | 9 ++- 11 files changed, 86 insertions(+), 103 deletions(-) diff --git a/default-plugins/status-bar/src/first_line.rs b/default-plugins/status-bar/src/first_line.rs index a4fa91b8..2ffe5f72 100644 --- a/default-plugins/status-bar/src/first_line.rs +++ b/default-plugins/status-bar/src/first_line.rs @@ -303,7 +303,7 @@ fn key_indicators( } pub fn superkey(palette: ColoredElements, separator: &str) -> LinePart { - let prefix_text = if separator.len() == 0 { + let prefix_text = if separator.is_empty() { " Ctrl + " } else { " Ctrl +" diff --git a/src/commands.rs b/src/commands.rs index 7715d435..ae3d4f84 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -140,43 +140,40 @@ pub(crate) fn send_action_to_session(opts: zellij_utils::cli::CliArgs) { fn attach_with_fake_client(opts: zellij_utils::cli::CliArgs, name: &str) { if let Some(zellij_utils::cli::Command::Sessions(zellij_utils::cli::Sessions::Action { - action, + action: Some(action), })) = opts.command.clone() { - if let Some(action) = action.clone() { - let action = format!("[{}]", action); - match zellij_utils::serde_yaml::from_str::(&action).into_diagnostic() { - Ok(parsed) => { - let (config, _, config_options) = match Setup::from_options(&opts) { - Ok(results) => results, - Err(e) => { - eprintln!("{}", e); - process::exit(1); - }, - }; - let os_input = - get_os_input(zellij_client::os_input_output::get_client_os_input); + let action = format!("[{}]", action); + match zellij_utils::serde_yaml::from_str::(&action).into_diagnostic() { + Ok(parsed) => { + let (config, _, config_options) = match Setup::from_options(&opts) { + Ok(results) => results, + Err(e) => { + eprintln!("{}", e); + process::exit(1); + }, + }; + let os_input = get_os_input(zellij_client::os_input_output::get_client_os_input); - let actions = parsed.actions().to_vec(); - log::debug!("Starting fake Zellij client!"); - zellij_client::fake_client::start_fake_client( - Box::new(os_input), - opts, - *Box::new(config), - config_options, - ClientInfo::New(name.to_string()), - None, - actions, - ); - log::debug!("Quitting fake client now."); - std::process::exit(0); - }, - Err(e) => { - eprintln!("{:?}", e); - std::process::exit(1); - }, - }; - } + let actions = parsed.actions().to_vec(); + log::debug!("Starting fake Zellij client!"); + zellij_client::fake_client::start_fake_client( + Box::new(os_input), + opts, + *Box::new(config), + config_options, + ClientInfo::New(name.to_string()), + None, + actions, + ); + log::debug!("Quitting fake client now."); + std::process::exit(0); + }, + Err(e) => { + eprintln!("{:?}", e); + std::process::exit(1); + }, + }; }; } diff --git a/src/sessions.rs b/src/sessions.rs index 38f78397..fc42b610 100644 --- a/src/sessions.rs +++ b/src/sessions.rs @@ -57,10 +57,8 @@ fn assert_socket(name: &str) -> bool { sender.send(ClientToServerMsg::ConnStatus); let mut receiver: IpcReceiverWithContext = sender.get_receiver(); match receiver.recv() { - Some((instruction, _)) => { - matches!(instruction, ServerToClientMsg::Connected) - }, - None => false, + Some((ServerToClientMsg::Connected, _)) => true, + None | Some((_, _)) => false, } }, Err(e) if e.kind() == io::ErrorKind::ConnectionRefused => { diff --git a/zellij-client/src/stdin_ansi_parser.rs b/zellij-client/src/stdin_ansi_parser.rs index cf20e1f0..a3ab117c 100644 --- a/zellij-client/src/stdin_ansi_parser.rs +++ b/zellij-client/src/stdin_ansi_parser.rs @@ -97,11 +97,7 @@ impl StdinAnsiParser { Key::Alt(CharOrArrow::Char(']')) => true, Key::Alt(CharOrArrow::Char('\\')) => true, Key::Char(c) => { - if let '0'..='9' | 'a'..='f' = c { - true - } else { - false - } + matches!(c, '0'..='9' | 'a'..='f') }, _ => false, } @@ -117,7 +113,7 @@ pub enum AnsiStdinInstructionOrKeys { } impl AnsiStdinInstructionOrKeys { - pub fn pixel_dimensions_from_keys(keys: &Vec<(Key, Vec)>) -> Result { + pub fn pixel_dimensions_from_keys(keys: &[(Key, Vec)]) -> Result { lazy_static! { static ref RE: Regex = Regex::new(r"^\u{1b}\[(\d+);(\d+);(\d+)t$").unwrap(); } @@ -172,7 +168,7 @@ impl AnsiStdinInstructionOrKeys { Err("invalid sequence") } } - pub fn color_sequence_from_keys(keys: &Vec<(Key, Vec)>) -> Result { + pub fn color_sequence_from_keys(keys: &[(Key, Vec)]) -> Result { lazy_static! { static ref BACKGROUND_RE: Regex = Regex::new(r"11;(.*)$").unwrap(); } @@ -180,9 +176,8 @@ impl AnsiStdinInstructionOrKeys { static ref FOREGROUND_RE: Regex = Regex::new(r"10;(.*)$").unwrap(); } let key_string = keys.iter().fold(String::new(), |mut acc, (key, _)| { - match key { - Key::Char(c) => acc.push(*c), - _ => {}, + if let Key::Char(c) = key { + acc.push(*c) }; acc }); diff --git a/zellij-server/src/os_input_output.rs b/zellij-server/src/os_input_output.rs index d800da49..5059a8fc 100644 --- a/zellij-server/src/os_input_output.rs +++ b/zellij-server/src/os_input_output.rs @@ -389,11 +389,10 @@ impl ServerOsApi for ServerOsInputOutput { None } fn write_to_file(&mut self, buf: String, name: Option) { - let mut f: File; - match name { - Some(x) => f = File::create(x).unwrap(), - None => f = tempfile().unwrap(), - } + let mut f: File = match name { + Some(x) => File::create(x).unwrap(), + None => tempfile().unwrap(), + }; if let Err(e) = write!(f, "{}", buf) { log::error!("could not write to file: {}", e); } diff --git a/zellij-server/src/panes/grid.rs b/zellij-server/src/panes/grid.rs index 870d743c..f65a7d2b 100644 --- a/zellij-server/src/panes/grid.rs +++ b/zellij-server/src/panes/grid.rs @@ -720,12 +720,10 @@ impl Grid { } self.cursor.y = new_cursor_y; self.cursor.x = new_cursor_x; - self.saved_cursor_position - .as_mut() - .map(|saved_cursor_position| { - saved_cursor_position.y = new_cursor_y; - saved_cursor_position.x = new_cursor_x; - }); + if let Some(saved_cursor_position) = self.saved_cursor_position.as_mut() { + saved_cursor_position.y = new_cursor_y; + saved_cursor_position.x = new_cursor_x; + }; } else if new_columns != self.width && self.alternate_lines_above_viewport_and_cursor.is_some() { @@ -750,24 +748,22 @@ impl Grid { ); let rows_pulled = self.viewport.len() - current_viewport_row_count; self.cursor.y += rows_pulled; - self.saved_cursor_position - .as_mut() - .map(|saved_cursor_position| saved_cursor_position.y += rows_pulled); + if let Some(saved_cursor_position) = self.saved_cursor_position.as_mut() { + saved_cursor_position.y += rows_pulled + }; }, Ordering::Greater => { let row_count_to_transfer = current_viewport_row_count - new_rows; if row_count_to_transfer > self.cursor.y { self.cursor.y = 0; - self.saved_cursor_position - .as_mut() - .map(|saved_cursor_position| saved_cursor_position.y = 0); + if let Some(saved_cursor_position) = self.saved_cursor_position.as_mut() { + saved_cursor_position.y = 0 + }; } else { self.cursor.y -= row_count_to_transfer; - self.saved_cursor_position - .as_mut() - .map(|saved_cursor_position| { - saved_cursor_position.y -= row_count_to_transfer - }); + if let Some(saved_cursor_position) = self.saved_cursor_position.as_mut() { + saved_cursor_position.y -= row_count_to_transfer + }; } if self.alternate_lines_above_viewport_and_cursor.is_none() { transfer_rows_from_viewport_to_lines_above( @@ -868,7 +864,7 @@ impl Grid { pub fn rotate_scroll_region_up(&mut self, count: usize) { if let Some((scroll_region_top, scroll_region_bottom)) = self .scroll_region - .or(Some((0, self.height.saturating_sub(1)))) + .or_else(|| Some((0, self.height.saturating_sub(1)))) { self.pad_lines_until(scroll_region_bottom, EMPTY_TERMINAL_CHARACTER); for _ in 0..count { @@ -889,7 +885,7 @@ impl Grid { pub fn rotate_scroll_region_down(&mut self, count: usize) { if let Some((scroll_region_top, scroll_region_bottom)) = self .scroll_region - .or(Some((0, self.height.saturating_sub(1)))) + .or_else(|| Some((0, self.height.saturating_sub(1)))) { self.pad_lines_until(scroll_region_bottom, EMPTY_TERMINAL_CHARACTER); let mut pad_character = EMPTY_TERMINAL_CHARACTER; diff --git a/zellij-server/src/panes/link_handler.rs b/zellij-server/src/panes/link_handler.rs index bfccd947..d15aba18 100644 --- a/zellij-server/src/panes/link_handler.rs +++ b/zellij-server/src/panes/link_handler.rs @@ -50,31 +50,29 @@ impl LinkHandler { } pub fn output_osc8(&self, link_anchor: Option) -> Option { - link_anchor - .map(|link| match link { - LinkAnchor::Start(index) => { - let link = self.links.get(&index); + link_anchor.and_then(|link| match link { + LinkAnchor::Start(index) => { + let link = self.links.get(&index); - let output = link.map(|link| { - let id = link - .id - .as_ref() - .map_or("".to_string(), |id| format!("id={}", id)); - format!("\u{1b}]8;{};{}{}", id, link.uri, TERMINATOR) - }); + let output = link.map(|link| { + let id = link + .id + .as_ref() + .map_or("".to_string(), |id| format!("id={}", id)); + format!("\u{1b}]8;{};{}{}", id, link.uri, TERMINATOR) + }); - if output.is_none() { - log::warn!( - "attempted to output osc8 link start, but id: {} was not found!", - index - ); - } + if output.is_none() { + log::warn!( + "attempted to output osc8 link start, but id: {} was not found!", + index + ); + } - output - }, - LinkAnchor::End => Some(format!("\u{1b}]8;;{}", TERMINATOR)), - }) - .flatten() + output + }, + LinkAnchor::End => Some(format!("\u{1b}]8;;{}", TERMINATOR)), + }) } } diff --git a/zellij-server/src/panes/terminal_pane.rs b/zellij-server/src/panes/terminal_pane.rs index 53b6d138..ac441300 100644 --- a/zellij-server/src/panes/terminal_pane.rs +++ b/zellij-server/src/panes/terminal_pane.rs @@ -36,6 +36,7 @@ pub enum PaneId { // FIXME: This should hold an os_api handle so that terminal panes can set their own size via FD in // their `reflow_lines()` method. Drop a Box in here somewhere. +#[allow(clippy::too_many_arguments)] pub struct TerminalPane { pub grid: Grid, pub pid: RawFd, @@ -488,6 +489,7 @@ impl Pane for TerminalPane { } impl TerminalPane { + #[allow(clippy::too_many_arguments)] pub fn new( pid: RawFd, position_and_size: PaneGeom, diff --git a/zellij-server/src/screen.rs b/zellij-server/src/screen.rs index 83f3363d..cb55aaa3 100644 --- a/zellij-server/src/screen.rs +++ b/zellij-server/src/screen.rs @@ -792,7 +792,7 @@ impl Screen { self.render(); } - fn unblock_input(&self) -> () { + fn unblock_input(&self) { self.bus .senders .send_to_server(ServerInstruction::UnblockInputThread) diff --git a/zellij-server/src/tab/mod.rs b/zellij-server/src/tab/mod.rs index 5d95ac76..5962bf9b 100644 --- a/zellij-server/src/tab/mod.rs +++ b/zellij-server/src/tab/mod.rs @@ -844,8 +844,7 @@ impl Tab { || self .suppressed_panes .values() - .find(|s_p| s_p.pid() == PaneId::Terminal(pid)) - .is_some() + .any(|s_p| s_p.pid() == PaneId::Terminal(pid)) } pub fn handle_pty_bytes(&mut self, pid: RawFd, bytes: VteBytes) { if let Some(terminal_output) = self diff --git a/zellij-server/src/tab/unit/tab_integration_tests.rs b/zellij-server/src/tab/unit/tab_integration_tests.rs index 949affa3..bcad7184 100644 --- a/zellij-server/src/tab/unit/tab_integration_tests.rs +++ b/zellij-server/src/tab/unit/tab_integration_tests.rs @@ -90,11 +90,10 @@ impl ServerOsApi for FakeInputOutput { unimplemented!() } fn write_to_file(&mut self, buf: String, name: Option) { - let f: String; - match name { - Some(x) => f = x, - None => f = "tmp-name".to_owned(), - } + let f: String = match name { + Some(x) => x, + None => "tmp-name".to_owned(), + }; self.file_dumps.lock().unwrap().insert(f, buf); } }