From 79320e3ba7c12cb09fd0009adc7a4dbe5483cdcd Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Sat, 18 Jan 2025 09:40:59 +0100 Subject: [PATCH] Remove some .unwrap() --- src/env/environment.rs | 2 +- src/env/environment_impl.rs | 8 ++++---- src/env_dispatch.rs | 9 ++++----- src/exec.rs | 4 ++-- src/input.rs | 2 +- src/wutil/dir_iter.rs | 2 -- 6 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/env/environment.rs b/src/env/environment.rs index 8fe2edf08..e4722029b 100644 --- a/src/env/environment.rs +++ b/src/env/environment.rs @@ -805,7 +805,7 @@ pub fn env_init(paths: Option<&ConfigPaths>, do_uvars: bool, default_paths: bool // Look for a global exported variable with the same name. let global = EnvStack::globals().getf(name, EnvMode::GLOBAL | EnvMode::EXPORT); - if global.is_some() && global.unwrap().as_string() == uvar.as_string() { + if global.is_some_and(|x| x.as_string() == uvar.as_string()) { to_skip.push(name.to_owned()); } } diff --git a/src/env/environment_impl.rs b/src/env/environment_impl.rs index edb09656c..ae2c26349 100644 --- a/src/env/environment_impl.rs +++ b/src/env/environment_impl.rs @@ -769,8 +769,8 @@ impl EnvStackImpl { // This is distinct from the unspecified scope, // which is the global scope if no function exists. let mut node = self.base.locals.clone(); - while node.next().is_some() { - node = node.next().unwrap(); + while let Some(next_node) = node.next() { + node = next_node; // The first node that introduces a new scope is ours. // If this doesn't happen, we go on until we've reached the // topmost local scope. @@ -837,8 +837,8 @@ impl EnvStackImpl { result.status = remove_from_chain(&mut self.base.locals, key); } else if query.function { let mut node = self.base.locals.clone(); - while node.next().is_some() { - node = node.next().unwrap(); + while let Some(next_node) = node.next() { + node = next_node; if node.borrow().new_scope { break; } diff --git a/src/env_dispatch.rs b/src/env_dispatch.rs index 5b7f34b20..8a6db3fd0 100644 --- a/src/env_dispatch.rs +++ b/src/env_dispatch.rs @@ -157,8 +157,8 @@ fn guess_emoji_width(vars: &EnvStack) { if let Some(width_str) = vars.get(L!("fish_emoji_width")) { // The only valid values are 1 or 2; we default to 1 if it was an invalid int. - let new_width = fish_wcstoi(&width_str.as_string()).unwrap_or(1).clamp(1, 2); - FISH_EMOJI_WIDTH.store(isize::try_from(new_width).unwrap(), Ordering::Relaxed); + let new_width = fish_wcstoi(&width_str.as_string()).unwrap_or(1).clamp(1, 2) as isize; + FISH_EMOJI_WIDTH.store(new_width, Ordering::Relaxed); FLOG!( term_support, "Overriding default fish_emoji_width w/", @@ -228,9 +228,8 @@ fn handle_change_ambiguous_width(vars: &EnvStack) { .and_then(|fish_ambiguous_width| fish_wcstoi(&fish_ambiguous_width).ok()) .unwrap_or(1) // Clamp in case of negative values. - .max(0); - crate::fallback::FISH_AMBIGUOUS_WIDTH - .store(isize::try_from(new_width).unwrap(), Ordering::Relaxed); + .max(0) as isize; + crate::fallback::FISH_AMBIGUOUS_WIDTH.store(new_width, Ordering::Relaxed); } fn handle_term_size_change(vars: &EnvStack) { diff --git a/src/exec.rs b/src/exec.rs index 07f9a5689..f2be86e50 100644 --- a/src/exec.rs +++ b/src/exec.rs @@ -604,7 +604,7 @@ fn run_internal_process(p: &Process, outdata: Vec, errdata: Vec, ios: &I let mut status = f.success_status.clone(); if !f.skip_out() { if let Err(err) = write_loop(&f.src_outfd, &f.outdata) { - if err.raw_os_error().unwrap() != EPIPE { + if err.raw_os_error() != Some(EPIPE) { perror("write"); } if status.is_success() { @@ -614,7 +614,7 @@ fn run_internal_process(p: &Process, outdata: Vec, errdata: Vec, ios: &I } if !f.skip_err() { if let Err(err) = write_loop(&f.src_errfd, &f.errdata) { - if err.raw_os_error().unwrap() != EPIPE { + if err.raw_os_error() != Some(EPIPE) { perror("write"); } if status.is_success() { diff --git a/src/input.rs b/src/input.rs index 6c4a61580..123f54c11 100644 --- a/src/input.rs +++ b/src/input.rs @@ -990,7 +990,7 @@ impl InputMappingSet { } else { &mut self.preset_mapping_list }; - let should_erase = |m: &InputMapping| mode.is_none() || mode.unwrap() == m.mode; + let should_erase = |m: &InputMapping| mode.is_none_or(|x| x == m.mode); ml.retain(|m| !should_erase(m)); } diff --git a/src/wutil/dir_iter.rs b/src/wutil/dir_iter.rs index 0acab6861..dbc192bfd 100644 --- a/src/wutil/dir_iter.rs +++ b/src/wutil/dir_iter.rs @@ -335,7 +335,6 @@ fn test_no_dots() { // DirIter does not return . or .. by default. let dir = DirIter::new(L!(".")).expect("Should be able to open CWD"); for entry in dir { - assert!(entry.is_ok()); let entry = entry.unwrap(); assert_ne!(entry.name, "."); assert_ne!(entry.name, ".."); @@ -350,7 +349,6 @@ fn test_dots() { let mut seen_dot = false; let mut seen_dotdot = false; for entry in dir { - assert!(entry.is_ok()); let entry = entry.unwrap(); if entry.name == "." { seen_dot = true;