diff --git a/crates/nu-cli/src/types/deduction.rs b/crates/nu-cli/src/types/deduction.rs index babb16eb3d..beee9a436e 100644 --- a/crates/nu-cli/src/types/deduction.rs +++ b/crates/nu-cli/src/types/deduction.rs @@ -377,10 +377,7 @@ impl VarSyntaxShapeDeductor { .iter() .map(|decl| { let usage: VarUsage = decl.into(); - let deduction = match deducer.inferences.get(&usage) { - Some(vec) => Some(vec.clone()), - None => None, - }; + let deduction = deducer.inferences.get(&usage).cloned(); (decl.clone(), deduction) }) .collect()) @@ -1023,7 +1020,7 @@ impl VarSyntaxShapeDeductor { Some(combination) } }) - .filter_map(|elem| elem) + .flatten() .collect() } //No any's intersection of both is result @@ -1044,7 +1041,7 @@ impl VarSyntaxShapeDeductor { Some(combination) } }) - .filter_map(|elem| elem) + .flatten() .collect(); if intersection.is_empty() { //TODO pass all labels somehow diff --git a/crates/nu-command/src/commands/each/window.rs b/crates/nu-command/src/commands/each/window.rs index 0f8dbd669c..26ad9028fb 100644 --- a/crates/nu-command/src/commands/each/window.rs +++ b/crates/nu-command/src/commands/each/window.rs @@ -87,7 +87,7 @@ impl WholeStreamCommand for EachWindow { None } }) - .filter_map(|x| x) + .flatten() .flatten() .to_action_stream()) } diff --git a/crates/nu-command/src/commands/get.rs b/crates/nu-command/src/commands/get.rs index 6da881f8d5..58ad7c9756 100644 --- a/crates/nu-command/src/commands/get.rs +++ b/crates/nu-command/src/commands/get.rs @@ -228,8 +228,8 @@ pub fn get_column_from_row_error( } => { let primary_label = format!("There isn't a column named '{}'", &column); - if let Some(suggestions) = did_you_mean(&obj_source, column_path_tried.as_string()) { - Some(ShellError::labeled_error_with_secondary( + did_you_mean(&obj_source, column_path_tried.as_string()).map(|suggestions| { + ShellError::labeled_error_with_secondary( "Unknown column", primary_label, column_path_tried.span, @@ -239,10 +239,8 @@ pub fn get_column_from_row_error( &obj_source.data_descriptors().join(", ") ), column_path_tried.span.since(path_members_span), - )) - } else { - None - } + ) + }) } PathMember { unspanned: UnspannedPathMember::Int(idx), diff --git a/crates/nu-command/src/commands/histogram.rs b/crates/nu-command/src/commands/histogram.rs index f81da3a446..6d24658518 100644 --- a/crates/nu-command/src/commands/histogram.rs +++ b/crates/nu-command/src/commands/histogram.rs @@ -76,10 +76,10 @@ pub fn histogram(args: CommandArgs) -> Result { }; let column_grouper = if !columns.is_empty() { - match columns.remove(0).split_last() { - Some((key, _)) => Some(key.as_string().tagged(&name)), - None => None, - } + columns + .remove(0) + .split_last() + .map(|(key, _)| key.as_string().tagged(&name)) } else { None }; diff --git a/crates/nu-command/src/commands/move_/command.rs b/crates/nu-command/src/commands/move_/command.rs index 4a74bae41a..bfe0d2144e 100644 --- a/crates/nu-command/src/commands/move_/command.rs +++ b/crates/nu-command/src/commands/move_/command.rs @@ -272,10 +272,7 @@ fn move_after(table: &Value, columns: &[String], from: &ColumnPath) -> Result>(), + &reordered_columns.into_iter().flatten().collect::>(), &table.tag, )) } @@ -321,10 +318,7 @@ fn move_before(table: &Value, columns: &[String], from: &ColumnPath) -> Result>(), + &reordered_columns.into_iter().flatten().collect::>(), &table.tag, )) } diff --git a/crates/nu-command/src/commands/where_.rs b/crates/nu-command/src/commands/where_.rs index 7c28bc516e..698effd3c2 100644 --- a/crates/nu-command/src/commands/where_.rs +++ b/crates/nu-command/src/commands/where_.rs @@ -95,10 +95,10 @@ fn where_command(raw_args: CommandArgs) -> Result { }; Ok(WhereIterator { - block, condition, context, input, + block, } .to_output_stream()) } diff --git a/crates/nu-command/src/utils/test_bins.rs b/crates/nu-command/src/utils/test_bins.rs index 54824109f4..8fb9cd1850 100644 --- a/crates/nu-command/src/utils/test_bins.rs +++ b/crates/nu-command/src/utils/test_bins.rs @@ -68,18 +68,16 @@ pub fn chop() { let stdin = io::stdin(); let mut stdout = io::stdout(); - for line in stdin.lock().lines() { - if let Ok(given) = line { - let chopped = if given.is_empty() { - &given - } else { - let to = given.len() - 1; - &given[..to] - }; + for given in stdin.lock().lines().flatten() { + let chopped = if given.is_empty() { + &given + } else { + let to = given.len() - 1; + &given[..to] + }; - if let Err(_e) = writeln!(stdout, "{}", chopped) { - break; - } + if let Err(_e) = writeln!(stdout, "{}", chopped) { + break; } } diff --git a/crates/nu-data/src/config/nuconfig.rs b/crates/nu-data/src/config/nuconfig.rs index 74d20d9188..22bf968ac5 100644 --- a/crates/nu-data/src/config/nuconfig.rs +++ b/crates/nu-data/src/config/nuconfig.rs @@ -59,8 +59,8 @@ impl NuConfig { }; Ok(NuConfig { - file_path, vars, + file_path, modified_at, }) } diff --git a/crates/nu-data/src/utils/internal.rs b/crates/nu-data/src/utils/internal.rs index 6101e6493d..8ab874430c 100644 --- a/crates/nu-data/src/utils/internal.rs +++ b/crates/nu-data/src/utils/internal.rs @@ -16,19 +16,11 @@ pub struct Labels { impl Labels { pub fn at(&self, idx: usize) -> Option<&str> { - if let Some(k) = self.x.get(idx) { - Some(&k[..]) - } else { - None - } + self.x.get(idx).map(|k| &k[..]) } pub fn at_split(&self, idx: usize) -> Option<&str> { - if let Some(k) = self.y.get(idx) { - Some(&k[..]) - } else { - None - } + self.y.get(idx).map(|k| &k[..]) } pub fn grouped(&self) -> impl Iterator { diff --git a/crates/nu-engine/src/env/host.rs b/crates/nu-engine/src/env/host.rs index d914453830..2c6b03d509 100644 --- a/crates/nu-engine/src/env/host.rs +++ b/crates/nu-engine/src/env/host.rs @@ -114,10 +114,7 @@ impl Host for FakeHost { fn env_get(&mut self, key: OsString) -> Option { let key = key.into_string().expect("Couldn't convert to string."); - match self.env_vars.get(&key) { - Some(env) => Some(OsString::from(env)), - None => None, - } + self.env_vars.get(&key).map(OsString::from) } fn env_set(&mut self, key: OsString, value: OsString) { diff --git a/crates/nu-engine/src/evaluation_context.rs b/crates/nu-engine/src/evaluation_context.rs index e98e32b4d5..b00d6b1c77 100644 --- a/crates/nu-engine/src/evaluation_context.rs +++ b/crates/nu-engine/src/evaluation_context.rs @@ -178,10 +178,8 @@ impl EvaluationContext { Some(env_paths) } else if let Some(env_paths) = self.scope.get_env("Path") { Some(env_paths) - } else if let Some(env_paths) = self.scope.get_env("path") { - Some(env_paths) } else { - None + self.scope.get_env("path") }; if let Some(env_paths) = env_paths { @@ -249,10 +247,8 @@ impl EvaluationContext { Some(env_paths) } else if let Some(env_paths) = self.scope.get_env("Path") { Some(env_paths) - } else if let Some(env_paths) = self.scope.get_env("path") { - Some(env_paths) } else { - None + self.scope.get_env("path") }; if let Some(env_paths) = env_paths { diff --git a/crates/nu-engine/src/filesystem/filesystem_shell.rs b/crates/nu-engine/src/filesystem/filesystem_shell.rs index 98697b4bf6..2a531aa3a0 100644 --- a/crates/nu-engine/src/filesystem/filesystem_shell.rs +++ b/crates/nu-engine/src/filesystem/filesystem_shell.rs @@ -386,82 +386,80 @@ impl Shell for FilesystemShell { )); } - for entry in sources { - if let Ok(entry) = entry { - let mut sources = FileStructure::new(); - sources.walk_decorate(&entry)?; + for entry in sources.into_iter().flatten() { + let mut sources = FileStructure::new(); + sources.walk_decorate(&entry)?; - if entry.is_file() { - let sources = sources.paths_applying_with(|(source_file, _depth_level)| { - if destination.is_dir() { - let mut dest = canonicalize(&path, &dst.item)?; - if let Some(name) = entry.file_name() { - dest.push(name); - } - Ok((source_file, dest)) - } else { - Ok((source_file, destination.clone())) + if entry.is_file() { + let sources = sources.paths_applying_with(|(source_file, _depth_level)| { + if destination.is_dir() { + let mut dest = canonicalize(&path, &dst.item)?; + if let Some(name) = entry.file_name() { + dest.push(name); } - })?; + Ok((source_file, dest)) + } else { + Ok((source_file, destination.clone())) + } + })?; - for (src, dst) in sources { - if src.is_file() { - std::fs::copy(src, dst).map_err(|e| { - ShellError::labeled_error(e.to_string(), e.to_string(), &name_tag) - })?; + for (src, dst) in sources { + if src.is_file() { + std::fs::copy(src, dst).map_err(|e| { + ShellError::labeled_error(e.to_string(), e.to_string(), &name_tag) + })?; + } + } + } else if entry.is_dir() { + let destination = if !destination.exists() { + destination.clone() + } else { + match entry.file_name() { + Some(name) => destination.join(name), + None => { + return Err(ShellError::labeled_error( + "Copy aborted. Not a valid path", + "not a valid path", + dst.tag, + )) } } - } else if entry.is_dir() { - let destination = if !destination.exists() { - destination.clone() - } else { - match entry.file_name() { - Some(name) => destination.join(name), - None => { - return Err(ShellError::labeled_error( - "Copy aborted. Not a valid path", - "not a valid path", - dst.tag, - )) - } - } - }; + }; - std::fs::create_dir_all(&destination).map_err(|e| { - ShellError::labeled_error(e.to_string(), e.to_string(), &dst.tag) - })?; + std::fs::create_dir_all(&destination).map_err(|e| { + ShellError::labeled_error(e.to_string(), e.to_string(), &dst.tag) + })?; - let sources = sources.paths_applying_with(|(source_file, depth_level)| { - let mut dest = destination.clone(); - let path = canonicalize(&path, &source_file)?; + let sources = sources.paths_applying_with(|(source_file, depth_level)| { + let mut dest = destination.clone(); + let path = canonicalize(&path, &source_file)?; - let comps: Vec<_> = path - .components() - .map(|fragment| fragment.as_os_str()) - .rev() - .take(1 + depth_level) - .collect(); + let comps: Vec<_> = path + .components() + .map(|fragment| fragment.as_os_str()) + .rev() + .take(1 + depth_level) + .collect(); - for fragment in comps.into_iter().rev() { - dest.push(fragment); - } + for fragment in comps.into_iter().rev() { + dest.push(fragment); + } - Ok((PathBuf::from(&source_file), dest)) - })?; + Ok((PathBuf::from(&source_file), dest)) + })?; - let dst_tag = &dst.tag; - for (src, dst) in sources { - if src.is_dir() && !dst.exists() { - std::fs::create_dir_all(&dst).map_err(|e| { - ShellError::labeled_error(e.to_string(), e.to_string(), dst_tag) - })?; - } + let dst_tag = &dst.tag; + for (src, dst) in sources { + if src.is_dir() && !dst.exists() { + std::fs::create_dir_all(&dst).map_err(|e| { + ShellError::labeled_error(e.to_string(), e.to_string(), dst_tag) + })?; + } - if src.is_file() { - std::fs::copy(&src, &dst).map_err(|e| { - ShellError::labeled_error(e.to_string(), e.to_string(), &name_tag) - })?; - } + if src.is_file() { + std::fs::copy(&src, &dst).map_err(|e| { + ShellError::labeled_error(e.to_string(), e.to_string(), &name_tag) + })?; } } } @@ -574,13 +572,11 @@ impl Shell for FilesystemShell { .collect(); } - for entry in sources { - if let Ok(entry) = entry { - move_file( - TaggedPathBuf(&entry, &src.tag), - TaggedPathBuf(&destination, &dst.tag), - )? - } + for entry in sources.into_iter().flatten() { + move_file( + TaggedPathBuf(&entry, &src.tag), + TaggedPathBuf(&destination, &dst.tag), + )? } Ok(ActionStream::empty()) diff --git a/crates/nu-engine/src/maybe_text_codec.rs b/crates/nu-engine/src/maybe_text_codec.rs index 781f37c9d7..ec54254dc3 100644 --- a/crates/nu-engine/src/maybe_text_codec.rs +++ b/crates/nu-engine/src/maybe_text_codec.rs @@ -27,8 +27,8 @@ pub struct BufCodecReader { impl BufCodecReader { pub fn new(input: BufReader, maybe_text_codec: MaybeTextCodec) -> Self { BufCodecReader { - input, maybe_text_codec, + input, } } } diff --git a/crates/nu-json/src/value.rs b/crates/nu-json/src/value.rs index 628f07db42..7b27449586 100644 --- a/crates/nu-json/src/value.rs +++ b/crates/nu-json/src/value.rs @@ -937,7 +937,7 @@ impl<'de> de::Deserializer<'de> for Value { } }; - visitor.visit_enum(EnumDeserializer { value, variant }) + visitor.visit_enum(EnumDeserializer { variant, value }) } #[inline] diff --git a/crates/nu-plugin/src/test_helpers.rs b/crates/nu-plugin/src/test_helpers.rs index 1345656deb..cac50d3227 100644 --- a/crates/nu-plugin/src/test_helpers.rs +++ b/crates/nu-plugin/src/test_helpers.rs @@ -62,10 +62,9 @@ impl<'a, T: Plugin> PluginTest<'a, T> { self.configure(|flags_configured| { let flags_registered = &call_stub.args.named; - let flag_passed = match flags_registered { - Some(names) => Some(names.keys().map(String::from).collect::>()), - None => None, - }; + let flag_passed = flags_registered + .as_ref() + .map(|names| names.keys().map(String::from).collect::>()); if let Some(flags) = flag_passed { for flag in flags { diff --git a/crates/nu-protocol/src/value/dict.rs b/crates/nu-protocol/src/value/dict.rs index 129a392051..9806f706a3 100644 --- a/crates/nu-protocol/src/value/dict.rs +++ b/crates/nu-protocol/src/value/dict.rs @@ -181,14 +181,10 @@ impl Dictionary { /// Get a mutable entry that matches a key, if possible pub fn get_mut_data_by_key(&mut self, name: &str) -> Option<&mut Value> { - match self - .entries + self.entries .iter_mut() .find(|(desc_name, _)| *desc_name == name) - { - Some((_, v)) => Some(v), - None => None, - } + .map(|(_, v)| v) } /// Insert a new key/value pair into the dictionary diff --git a/crates/nu-source/src/meta.rs b/crates/nu-source/src/meta.rs index 671c215090..370fff1ef8 100644 --- a/crates/nu-source/src/meta.rs +++ b/crates/nu-source/src/meta.rs @@ -794,10 +794,7 @@ where impl HasFallibleSpan for Option> { fn maybe_span(&self) -> Option { - match self { - None => None, - Some(value) => Some(value.span), - } + self.as_ref().map(|value| value.span) } } @@ -815,10 +812,7 @@ where impl HasFallibleSpan for Option> { fn maybe_span(&self) -> Option { - match self { - None => None, - Some(value) => Some(value.tag.span), - } + self.as_ref().map(|value| value.tag.span) } } diff --git a/crates/nu_plugin_binaryview/src/binaryview.rs b/crates/nu_plugin_binaryview/src/binaryview.rs index 2b65674544..d05ee5243c 100644 --- a/crates/nu_plugin_binaryview/src/binaryview.rs +++ b/crates/nu_plugin_binaryview/src/binaryview.rs @@ -213,15 +213,9 @@ pub fn view_contents( skip: Option<&Value>, length: Option<&Value>, ) -> Result<(), Box> { - let skip_bytes = match skip { - Some(s) => Some(s.as_usize().unwrap_or(0)), - None => None, - }; + let skip_bytes = skip.map(|s| s.as_usize().unwrap_or(0)); - let num_bytes = match length { - Some(b) => Some(b.as_usize().unwrap_or(0)), - None => None, - }; + let num_bytes = length.map(|b| b.as_usize().unwrap_or(0)); let config = HexConfig { title: true, diff --git a/crates/nu_plugin_start/src/nu/mod.rs b/crates/nu_plugin_start/src/nu/mod.rs index 3b3be751ea..4136973cca 100644 --- a/crates/nu_plugin_start/src/nu/mod.rs +++ b/crates/nu_plugin_start/src/nu/mod.rs @@ -19,6 +19,6 @@ impl Plugin for Start { } fn begin_filter(&mut self, call_info: CallInfo) -> Result, ShellError> { self.parse(call_info)?; - self.exec().map(|_| vec![]) + self.exec().map(|_| Vec::new()) } }