Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix return in filter closure eval #12292

Merged
merged 2 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions crates/nu-command/src/filters/filter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::utils::chain_error_with_input;
use nu_engine::{get_eval_block, CallExt};
use nu_engine::{get_eval_block_with_early_return, CallExt};
use nu_protocol::ast::Call;

use nu_protocol::engine::{Closure, Command, EngineState, Stack};
Expand Down Expand Up @@ -62,7 +62,7 @@ a variable. On the other hand, the "row condition" syntax is not supported."#
let orig_env_vars = stack.env_vars.clone();
let orig_env_hidden = stack.env_hidden.clone();
let span = call.head;
let eval_block = get_eval_block(&engine_state);
let eval_block_with_early_return = get_eval_block_with_early_return(&engine_state);

match input {
PipelineData::Empty => Ok(PipelineData::Empty),
Expand All @@ -84,7 +84,7 @@ a variable. On the other hand, the "row condition" syntax is not supported."#
}
}

match eval_block(
match eval_block_with_early_return(
&engine_state,
&mut stack,
&block,
Expand Down Expand Up @@ -126,7 +126,7 @@ a variable. On the other hand, the "row condition" syntax is not supported."#
}
}

match eval_block(
match eval_block_with_early_return(
&engine_state,
&mut stack,
&block,
Expand Down Expand Up @@ -159,7 +159,7 @@ a variable. On the other hand, the "row condition" syntax is not supported."#
}
}

Ok(match eval_block(
Ok(match eval_block_with_early_return(
&engine_state,
&mut stack,
&block,
Expand Down
18 changes: 18 additions & 0 deletions crates/nu-command/tests/commands/filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use nu_test_support::{nu, pipeline};

#[test]
fn filter_with_return_in_closure() {
let actual = nu!(pipeline(
"
1..10 | filter { |it|
if $it mod 2 == 0 {
return true
};
return false;
} | to nuon --raw
"
));

assert_eq!(actual.out, "[2, 4, 6, 8, 10]");
assert!(actual.err.is_empty());
}
1 change: 1 addition & 0 deletions crates/nu-command/tests/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod every;
mod exec;
mod export_def;
mod fill;
mod filter;
mod find;
mod first;
mod flatten;
Expand Down