Make 'for' implicitly filter out nothings (#4536)

* Make 'for' implicitly filter out nothings

* Fix test
This commit is contained in:
JT 2022-02-18 13:41:41 -05:00 committed by GitHub
parent f65955ccc5
commit 786e4ab971
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 1 deletions

View file

@ -109,6 +109,7 @@ impl Command for For {
Err(error) => Value::Error { error }, Err(error) => Value::Error { error },
} }
}) })
.filter(|x| !x.is_nothing())
.into_pipeline_data(ctrlc)), .into_pipeline_data(ctrlc)),
Value::Range { val, .. } => Ok(val Value::Range { val, .. } => Ok(val
.into_range_iter()? .into_range_iter()?
@ -146,6 +147,7 @@ impl Command for For {
Err(error) => Value::Error { error }, Err(error) => Value::Error { error },
} }
}) })
.filter(|x| !x.is_nothing())
.into_pipeline_data(ctrlc)), .into_pipeline_data(ctrlc)),
x => { x => {
stack.add_var(var_id, x); stack.add_var(var_id, x);

View file

@ -562,6 +562,10 @@ impl Value {
} }
} }
pub fn is_nothing(&self) -> bool {
matches!(self, Value::Nothing { .. })
}
/// Create a new `Nothing` value /// Create a new `Nothing` value
pub fn nothing(span: Span) -> Value { pub fn nothing(span: Span) -> Value {
Value::Nothing { span } Value::Nothing { span }

View file

@ -149,7 +149,7 @@ fn proper_variable_captures_with_nesting() -> TestResult {
#[test] #[test]
fn proper_variable_for() -> TestResult { 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] #[test]