From 0934410b3871a270bd91419226e77ed964271a18 Mon Sep 17 00:00:00 2001 From: "Joseph T. Lyons" Date: Tue, 14 Jul 2020 20:11:41 -0400 Subject: [PATCH] Use `matches!()` for true/false returning match statements (#2176) --- crates/nu-cli/src/commands/each.rs | 11 ++++------- crates/nu-cli/src/commands/echo.rs | 5 +---- crates/nu-cli/src/commands/keep_until.rs | 5 +---- crates/nu-cli/src/commands/keep_while.rs | 5 +---- crates/nu-cli/src/commands/skip_until.rs | 5 +---- crates/nu-cli/src/commands/skip_while.rs | 5 +---- crates/nu-cli/src/data/command.rs | 5 +---- crates/nu-cli/src/utils.rs | 5 +---- crates/nu-protocol/src/hir.rs | 13 ++---------- crates/nu-protocol/src/value.rs | 25 +++++------------------- crates/nu-source/src/pretty.rs | 5 +---- 11 files changed, 19 insertions(+), 70 deletions(-) diff --git a/crates/nu-cli/src/commands/each.rs b/crates/nu-cli/src/commands/each.rs index 9b3f107d7..8b816538e 100644 --- a/crates/nu-cli/src/commands/each.rs +++ b/crates/nu-cli/src/commands/each.rs @@ -74,13 +74,10 @@ impl WholeStreamCommand for Each { } fn is_expanded_it_usage(head: &SpannedExpression) -> bool { - match &*head { - SpannedExpression { - expr: Expression::Synthetic(Synthetic::String(s)), - .. - } if s == "expanded-each" => true, - _ => false, - } + matches!(&*head, SpannedExpression { + expr: Expression::Synthetic(Synthetic::String(s)), + .. + } if s == "expanded-each") } pub async fn process_row( diff --git a/crates/nu-cli/src/commands/echo.rs b/crates/nu-cli/src/commands/echo.rs index 80a5441eb..1073d883f 100644 --- a/crates/nu-cli/src/commands/echo.rs +++ b/crates/nu-cli/src/commands/echo.rs @@ -90,10 +90,7 @@ impl RangeIterator { curr: range.from.0.item, end: range.to.0.item, tag, - is_end_inclusive: match range.to.1 { - RangeInclusion::Inclusive => true, - RangeInclusion::Exclusive => false, - }, + is_end_inclusive: matches!(range.to.1, RangeInclusion::Inclusive), is_done: false, } } diff --git a/crates/nu-cli/src/commands/keep_until.rs b/crates/nu-cli/src/commands/keep_until.rs index 326a38080..c7f9e0efa 100644 --- a/crates/nu-cli/src/commands/keep_until.rs +++ b/crates/nu-cli/src/commands/keep_until.rs @@ -100,10 +100,7 @@ impl WholeStreamCommand for KeepUntil { .await; trace!("RESULT = {:?}", result); - match result { - Ok(ref v) if v.is_true() => false, - _ => true, - } + !matches!(result, Ok(ref v) if v.is_true()) } }) .to_output_stream()) diff --git a/crates/nu-cli/src/commands/keep_while.rs b/crates/nu-cli/src/commands/keep_while.rs index a449bf381..42554d333 100644 --- a/crates/nu-cli/src/commands/keep_while.rs +++ b/crates/nu-cli/src/commands/keep_while.rs @@ -100,10 +100,7 @@ impl WholeStreamCommand for KeepWhile { .await; trace!("RESULT = {:?}", result); - match result { - Ok(ref v) if v.is_true() => true, - _ => false, - } + matches!(result, Ok(ref v) if v.is_true()) } }) .to_output_stream()) diff --git a/crates/nu-cli/src/commands/skip_until.rs b/crates/nu-cli/src/commands/skip_until.rs index 8eddae37e..7a51e7d3c 100644 --- a/crates/nu-cli/src/commands/skip_until.rs +++ b/crates/nu-cli/src/commands/skip_until.rs @@ -99,10 +99,7 @@ impl WholeStreamCommand for SkipUntil { .await; trace!("RESULT = {:?}", result); - match result { - Ok(ref v) if v.is_true() => false, // stop skipping - _ => true, - } + !matches!(result, Ok(ref v) if v.is_true()) } }) .to_output_stream()) diff --git a/crates/nu-cli/src/commands/skip_while.rs b/crates/nu-cli/src/commands/skip_while.rs index 3abc8f06b..19f9d73ed 100644 --- a/crates/nu-cli/src/commands/skip_while.rs +++ b/crates/nu-cli/src/commands/skip_while.rs @@ -99,10 +99,7 @@ impl WholeStreamCommand for SkipWhile { .await; trace!("RESULT = {:?}", result); - match result { - Ok(ref v) if v.is_true() => true, - _ => false, - } + matches!(result, Ok(ref v) if v.is_true()) } }) .to_output_stream()) diff --git a/crates/nu-cli/src/data/command.rs b/crates/nu-cli/src/data/command.rs index 947b5d04b..c470dfc40 100644 --- a/crates/nu-cli/src/data/command.rs +++ b/crates/nu-cli/src/data/command.rs @@ -38,10 +38,7 @@ fn signature_dict(signature: Signature, tag: impl Into) -> Value { let mut sig = TaggedListBuilder::new(&tag); for arg in signature.positional.iter() { - let is_required = match arg.0 { - PositionalType::Mandatory(_, _) => true, - PositionalType::Optional(_, _) => false, - }; + let is_required = matches!(arg.0, PositionalType::Mandatory(_, _)); sig.push_value(for_spec(arg.0.name(), "argument", is_required, &tag)); } diff --git a/crates/nu-cli/src/utils.rs b/crates/nu-cli/src/utils.rs index 37f4619c8..72db7890d 100644 --- a/crates/nu-cli/src/utils.rs +++ b/crates/nu-cli/src/utils.rs @@ -8,10 +8,7 @@ use nu_protocol::{UntaggedValue, Value}; use std::path::{Component, Path, PathBuf}; fn is_value_tagged_dir(value: &Value) -> bool { - match &value.value { - UntaggedValue::Row(_) | UntaggedValue::Table(_) => true, - _ => false, - } + matches!(&value.value, UntaggedValue::Row(_) | UntaggedValue::Table(_)) } #[derive(Debug, Eq, Ord, PartialEq, PartialOrd)] diff --git a/crates/nu-protocol/src/hir.rs b/crates/nu-protocol/src/hir.rs index c5df1d6e3..8405aa148 100644 --- a/crates/nu-protocol/src/hir.rs +++ b/crates/nu-protocol/src/hir.rs @@ -265,13 +265,7 @@ impl ExternalCommand { .. } => { let Path { head, .. } = &**path; - match head { - SpannedExpression { - expr: Expression::Variable(Variable::It(_)), - .. - } => true, - _ => false, - } + matches!(head, SpannedExpression{expr: Expression::Variable(Variable::It(_)), ..}) } _ => false, }) @@ -1352,10 +1346,7 @@ impl NamedArguments { pub fn switch_present(&self, switch: &str) -> bool { self.named .get(switch) - .map(|t| match t { - NamedValue::PresentSwitch(_) => true, - _ => false, - }) + .map(|t| matches!(t, NamedValue::PresentSwitch(_))) .unwrap_or(false) } } diff --git a/crates/nu-protocol/src/value.rs b/crates/nu-protocol/src/value.rs index 8ceaa2b86..0bd68d1fd 100644 --- a/crates/nu-protocol/src/value.rs +++ b/crates/nu-protocol/src/value.rs @@ -77,18 +77,12 @@ impl UntaggedValue { /// Returns true if this value represents boolean true pub fn is_true(&self) -> bool { - match self { - UntaggedValue::Primitive(Primitive::Boolean(true)) => true, - _ => false, - } + matches!(self, UntaggedValue::Primitive(Primitive::Boolean(true))) } /// Returns true if this value represents a table pub fn is_table(&self) -> bool { - match self { - UntaggedValue::Table(_) => true, - _ => false, - } + matches!(self, UntaggedValue::Table(_)) } /// Returns true if the value represents something other than Nothing @@ -98,18 +92,12 @@ impl UntaggedValue { /// Returns true if the value represents Nothing pub fn is_none(&self) -> bool { - match self { - UntaggedValue::Primitive(Primitive::Nothing) => true, - _ => false, - } + matches!(self, UntaggedValue::Primitive(Primitive::Nothing)) } /// Returns true if the value represents an error pub fn is_error(&self) -> bool { - match self { - UntaggedValue::Error(_err) => true, - _ => false, - } + matches!(self, UntaggedValue::Error(_err)) } /// Expect this value to be an error and return it @@ -341,10 +329,7 @@ impl Value { /// View the Value as a Primitive value, if possible pub fn is_primitive(&self) -> bool { - match &self.value { - UntaggedValue::Primitive(_) => true, - _ => false, - } + matches!(&self.value, UntaggedValue::Primitive(_)) } /// View the Value as unsigned 64-bit, if possible diff --git a/crates/nu-source/src/pretty.rs b/crates/nu-source/src/pretty.rs index 38b607e17..7be1ce533 100644 --- a/crates/nu-source/src/pretty.rs +++ b/crates/nu-source/src/pretty.rs @@ -264,10 +264,7 @@ impl DebugDocBuilder { } pub fn is_empty(&self) -> bool { - match &self.inner.1 { - pretty::Doc::Nil => true, - _ => false, - } + matches!(&self.inner.1, pretty::Doc::Nil) } pub fn or(self, doc: DebugDocBuilder) -> DebugDocBuilder {