From 786e4ab971b647b77756ccedc0f56133be8896e0 Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Fri, 18 Feb 2022 13:41:41 -0500 Subject: [PATCH] Make 'for' implicitly filter out nothings (#4536) * Make 'for' implicitly filter out nothings * Fix test --- crates/nu-command/src/core_commands/for_.rs | 2 ++ crates/nu-protocol/src/value/mod.rs | 4 ++++ src/tests/test_engine.rs | 2 +- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/nu-command/src/core_commands/for_.rs b/crates/nu-command/src/core_commands/for_.rs index a686f297cd..685db8c7cc 100644 --- a/crates/nu-command/src/core_commands/for_.rs +++ b/crates/nu-command/src/core_commands/for_.rs @@ -109,6 +109,7 @@ impl Command for For { Err(error) => Value::Error { error }, } }) + .filter(|x| !x.is_nothing()) .into_pipeline_data(ctrlc)), Value::Range { val, .. } => Ok(val .into_range_iter()? @@ -146,6 +147,7 @@ impl Command for For { Err(error) => Value::Error { error }, } }) + .filter(|x| !x.is_nothing()) .into_pipeline_data(ctrlc)), x => { stack.add_var(var_id, x); diff --git a/crates/nu-protocol/src/value/mod.rs b/crates/nu-protocol/src/value/mod.rs index 54cdc9c641..cf7bc1cf22 100644 --- a/crates/nu-protocol/src/value/mod.rs +++ b/crates/nu-protocol/src/value/mod.rs @@ -562,6 +562,10 @@ impl Value { } } + pub fn is_nothing(&self) -> bool { + matches!(self, Value::Nothing { .. }) + } + /// Create a new `Nothing` value pub fn nothing(span: Span) -> Value { Value::Nothing { span } diff --git a/src/tests/test_engine.rs b/src/tests/test_engine.rs index 95d95f3f69..8b23b28e5a 100644 --- a/src/tests/test_engine.rs +++ b/src/tests/test_engine.rs @@ -149,7 +149,7 @@ fn proper_variable_captures_with_nesting() -> TestResult { #[test] fn proper_variable_for() -> TestResult { - run_test(r#"for x in 1..3 { if $x == 2 { "bob" } } | get 1"#, "bob") + run_test(r#"for x in 1..3 { if $x == 2 { "bob" } } | get 0"#, "bob") } #[test]