From 48454c004700e359c3c496871643913fb20de84f Mon Sep 17 00:00:00 2001 From: Chris Copeland Date: Wed, 20 Jul 2022 01:24:27 -0700 Subject: [PATCH] Replace `map().unwrap_or()` with `map_or()` Use a `map_or` instead of a `map().unwrap_or()` chain. --- alacritty/src/display/hint.rs | 4 ++-- alacritty/src/display/mod.rs | 6 +++--- alacritty/src/event.rs | 4 ++-- alacritty/src/input.rs | 4 ++-- alacritty/src/string.rs | 2 +- alacritty_terminal/src/ansi.rs | 5 +++-- alacritty_terminal/src/grid/row.rs | 2 +- alacritty_terminal/src/tty/unix.rs | 2 +- 8 files changed, 15 insertions(+), 14 deletions(-) diff --git a/alacritty/src/display/hint.rs b/alacritty/src/display/hint.rs index 887840e9..ace39d4c 100644 --- a/alacritty/src/display/hint.rs +++ b/alacritty/src/display/hint.rs @@ -412,7 +412,7 @@ fn hyperlink_at(term: &Term, point: Point) -> Option<(Hyperlink, Match)> { // the hyperlink we've found at original `point`. let line_contains_hyperlink = grid[next_line] .into_iter() - .any(|cell| cell.hyperlink().map(|h| h == hyperlink).unwrap_or(false)); + .any(|cell| cell.hyperlink().map_or(false, |h| h == hyperlink)); // There's no hyperlink on the next line, break. if !line_contains_hyperlink { @@ -428,7 +428,7 @@ fn hyperlink_at(term: &Term, point: Point) -> Option<(Hyperlink, Match)> { let line_contains_hyperlink = grid[next_line] .into_iter() - .any(|cell| cell.hyperlink().map(|h| h == hyperlink).unwrap_or(false)); + .any(|cell| cell.hyperlink().map_or(false, |h| h == hyperlink)); if !line_contains_hyperlink { break; diff --git a/alacritty/src/display/mod.rs b/alacritty/src/display/mod.rs index 79e57307..ec3f9ac7 100644 --- a/alacritty/src/display/mod.rs +++ b/alacritty/src/display/mod.rs @@ -403,7 +403,7 @@ impl Display { // Guess scale_factor based on first monitor. On Wayland the initial frame always renders at // a scale factor of 1. let estimated_scale_factor = if cfg!(any(target_os = "macos", windows)) || is_x11 { - event_loop.available_monitors().next().map(|m| m.scale_factor()).unwrap_or(1.) + event_loop.available_monitors().next().map_or(1., |m| m.scale_factor()) } else { 1. }; @@ -628,7 +628,7 @@ impl Display { // Update number of column/lines in the viewport. let message_bar_lines = - message_buffer.message().map(|m| m.text(&self.size_info).len()).unwrap_or(0); + message_buffer.message().map_or(0, |m| m.text(&self.size_info).len()); let search_lines = if search_active { 1 } else { 0 }; self.size_info.reserve_lines(message_bar_lines + search_lines); @@ -997,7 +997,7 @@ impl Display { if highlighted_hint.is_some() { // If mouse changed the line, we should update the hyperlink preview, since the // highlighted hint could be disrupted by the old preview. - dirty = self.hint_mouse_point.map(|p| p.line != point.line).unwrap_or(false); + dirty = self.hint_mouse_point.map_or(false, |p| p.line != point.line); self.hint_mouse_point = Some(point); self.window.set_mouse_cursor(CursorIcon::Hand); } else if self.highlighted_hint.is_some() { diff --git a/alacritty/src/event.rs b/alacritty/src/event.rs index b4248dc7..ca8efed0 100644 --- a/alacritty/src/event.rs +++ b/alacritty/src/event.rs @@ -254,7 +254,7 @@ impl<'a, N: Notify + 'a, T: EventListener> input::ActionContext for ActionCon } fn selection_is_empty(&self) -> bool { - self.terminal.selection.as_ref().map(Selection::is_empty).unwrap_or(true) + self.terminal.selection.as_ref().map_or(true, Selection::is_empty) } fn clear_selection(&mut self) { @@ -546,7 +546,7 @@ impl<'a, N: Notify + 'a, T: EventListener> input::ActionContext for ActionCon fn search_pop_word(&mut self) { if let Some(regex) = self.search_state.regex_mut() { *regex = regex.trim_end().to_owned(); - regex.truncate(regex.rfind(' ').map(|i| i + 1).unwrap_or(0)); + regex.truncate(regex.rfind(' ').map_or(0, |i| i + 1)); self.update_search(); } } diff --git a/alacritty/src/input.rs b/alacritty/src/input.rs index 37cd4f92..9f076e1b 100644 --- a/alacritty/src/input.rs +++ b/alacritty/src/input.rs @@ -717,13 +717,13 @@ impl> Processor { { let size = self.ctx.size_info(); - let current_lines = self.ctx.message().map(|m| m.text(&size).len()).unwrap_or(0); + let current_lines = self.ctx.message().map_or(0, |m| m.text(&size).len()); self.ctx.clear_selection(); self.ctx.pop_message(); // Reset cursor when message bar height changed or all messages are gone. - let new_lines = self.ctx.message().map(|m| m.text(&size).len()).unwrap_or(0); + let new_lines = self.ctx.message().map_or(0, |m| m.text(&size).len()); let new_icon = match current_lines.cmp(&new_lines) { Ordering::Less => CursorIcon::Default, diff --git a/alacritty/src/string.rs b/alacritty/src/string.rs index 092c10e9..4a758b34 100644 --- a/alacritty/src/string.rs +++ b/alacritty/src/string.rs @@ -90,7 +90,7 @@ impl<'a> StrShortener<'a> { } // Consume the iterator to count the number of characters in it. - let num_chars = iter.last().map(|(idx, _)| idx + 1).unwrap_or(offset); + let num_chars = iter.last().map_or(offset, |(idx, _)| idx + 1); let skip_chars = num_chars - offset; let text_action = if num_chars <= max_width || shortener.is_none() { diff --git a/alacritty_terminal/src/ansi.rs b/alacritty_terminal/src/ansi.rs index e30ed0f5..3f33a1cb 100644 --- a/alacritty_terminal/src/ansi.rs +++ b/alacritty_terminal/src/ansi.rs @@ -1155,8 +1155,9 @@ where let mut params_iter = params.iter(); let handler = &mut self.handler; - let mut next_param_or = |default: u16| { - params_iter.next().map(|param| param[0]).filter(|¶m| param != 0).unwrap_or(default) + let mut next_param_or = |default: u16| match params_iter.next() { + Some(&[param, ..]) if param != 0 => param, + _ => default, }; match (action, intermediates) { diff --git a/alacritty_terminal/src/grid/row.rs b/alacritty_terminal/src/grid/row.rs index 900d2a76..2a92a78c 100644 --- a/alacritty_terminal/src/grid/row.rs +++ b/alacritty_terminal/src/grid/row.rs @@ -76,7 +76,7 @@ impl Row { // Split off cells for a new row. let mut new_row = self.inner.split_off(columns); - let index = new_row.iter().rposition(|c| !c.is_empty()).map(|i| i + 1).unwrap_or(0); + let index = new_row.iter().rposition(|c| !c.is_empty()).map_or(0, |i| i + 1); new_row.truncate(index); self.occ = min(self.occ, columns); diff --git a/alacritty_terminal/src/tty/unix.rs b/alacritty_terminal/src/tty/unix.rs index 2bb5e973..d1e5bdb5 100644 --- a/alacritty_terminal/src/tty/unix.rs +++ b/alacritty_terminal/src/tty/unix.rs @@ -173,7 +173,7 @@ pub fn new(config: &PtyConfig, window_size: WindowSize, window_id: Option // Set $SHELL environment variable on macOS, since login does not do it for us. #[cfg(target_os = "macos")] - builder.env("SHELL", config.shell.as_ref().map(|sh| sh.program()).unwrap_or(pw.shell)); + builder.env("SHELL", config.shell.as_ref().map_or(pw.shell, Program::program)); if let Some(window_id) = window_id { builder.env("WINDOWID", format!("{}", window_id));