From e8764de3c67281a513a36457656de80c652e8b06 Mon Sep 17 00:00:00 2001 From: Wind Date: Fri, 19 Jul 2024 15:21:02 +0800 Subject: [PATCH] don't allow break/continue in `each` and `items` command (#13398) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description Fixes: #11451 # User-Facing Changes ### Before ```nushell ❯ [1 2 3] | each {|e| break; print $e } ╭────────────╮ │ empty list │ ╰────────────╯ ``` ### After ``` ❯ [1 2 3] | each {|e| break; print $e } Error: nu::shell::eval_block_with_input × Eval block failed with pipeline input ╭─[entry #9:1:2] 1 │ [1 2 3] | each {|e| break; print $e } · ┬ · ╰── source value ╰──── Error: × Break used outside of loop ╭─[entry #9:1:21] 1 │ [1 2 3] | each {|e| break; print $e } · ──┬── · ╰── used outside of loop ╰──── ``` # Tests + Formatting Removes some tests. --- crates/nu-cmd-lang/src/core_commands/break_.rs | 4 +++- .../nu-cmd-lang/src/core_commands/continue_.rs | 4 +++- crates/nu-command/src/filters/each.rs | 8 -------- crates/nu-command/src/filters/items.rs | 1 - crates/nu-command/tests/commands/break_.rs | 9 --------- crates/nu-command/tests/commands/each.rs | 16 ---------------- 6 files changed, 6 insertions(+), 36 deletions(-) diff --git a/crates/nu-cmd-lang/src/core_commands/break_.rs b/crates/nu-cmd-lang/src/core_commands/break_.rs index 90cc1a73f2..943e32a531 100644 --- a/crates/nu-cmd-lang/src/core_commands/break_.rs +++ b/crates/nu-cmd-lang/src/core_commands/break_.rs @@ -21,7 +21,9 @@ impl Command for Break { fn extra_usage(&self) -> &str { r#"This command is a parser keyword. For details, check: - https://www.nushell.sh/book/thinking_in_nu.html"# + https://www.nushell.sh/book/thinking_in_nu.html + + break can only be used in while, loop, and for loops. It can not be used with each or other filter commands"# } fn command_type(&self) -> CommandType { diff --git a/crates/nu-cmd-lang/src/core_commands/continue_.rs b/crates/nu-cmd-lang/src/core_commands/continue_.rs index cfa3e38335..b795456c1d 100644 --- a/crates/nu-cmd-lang/src/core_commands/continue_.rs +++ b/crates/nu-cmd-lang/src/core_commands/continue_.rs @@ -21,7 +21,9 @@ impl Command for Continue { fn extra_usage(&self) -> &str { r#"This command is a parser keyword. For details, check: - https://www.nushell.sh/book/thinking_in_nu.html"# + https://www.nushell.sh/book/thinking_in_nu.html + + continue can only be used in while, loop, and for loops. It can not be used with each or other filter commands"# } fn command_type(&self) -> CommandType { diff --git a/crates/nu-command/src/filters/each.rs b/crates/nu-command/src/filters/each.rs index fa1bf390b8..a7035917b3 100644 --- a/crates/nu-command/src/filters/each.rs +++ b/crates/nu-command/src/filters/each.rs @@ -132,8 +132,6 @@ with 'transpose' first."# Ok(data) => Some(data.into_value(head).unwrap_or_else(|err| { Value::error(chain_error_with_input(err, is_error, span), span) })), - Err(ShellError::Continue { span }) => Some(Value::nothing(span)), - Err(ShellError::Break { .. }) => None, Err(error) => { let error = chain_error_with_input(error, is_error, span); Some(Value::error(error, span)) @@ -149,10 +147,6 @@ with 'transpose' first."# .map_while(move |value| { let value = match value { Ok(value) => value, - Err(ShellError::Continue { span }) => { - return Some(Value::nothing(span)) - } - Err(ShellError::Break { .. }) => return None, Err(err) => return Some(Value::error(err, head)), }; @@ -163,8 +157,6 @@ with 'transpose' first."# .and_then(|data| data.into_value(head)) { Ok(value) => Some(value), - Err(ShellError::Continue { span }) => Some(Value::nothing(span)), - Err(ShellError::Break { .. }) => None, Err(error) => { let error = chain_error_with_input(error, is_error, span); Some(Value::error(error, span)) diff --git a/crates/nu-command/src/filters/items.rs b/crates/nu-command/src/filters/items.rs index 04a4c6d672..fa7b680afd 100644 --- a/crates/nu-command/src/filters/items.rs +++ b/crates/nu-command/src/filters/items.rs @@ -60,7 +60,6 @@ impl Command for Items { match result { Ok(value) => Some(value), - Err(ShellError::Break { .. }) => None, Err(err) => { let err = chain_error_with_input(err, false, span); Some(Value::error(err, head)) diff --git a/crates/nu-command/tests/commands/break_.rs b/crates/nu-command/tests/commands/break_.rs index 92a6564658..7398ac62e5 100644 --- a/crates/nu-command/tests/commands/break_.rs +++ b/crates/nu-command/tests/commands/break_.rs @@ -15,12 +15,3 @@ fn break_while_loop() { assert_eq!(actual.out, "hello"); } - -#[test] -fn break_each() { - let actual = nu!(" - [1, 2, 3, 4, 5] | each {|x| if $x > 3 { break }; $x} | math sum - "); - - assert_eq!(actual.out, "6"); -} diff --git a/crates/nu-command/tests/commands/each.rs b/crates/nu-command/tests/commands/each.rs index f8e87d5537..65b922bb6e 100644 --- a/crates/nu-command/tests/commands/each.rs +++ b/crates/nu-command/tests/commands/each.rs @@ -58,22 +58,6 @@ fn each_while_uses_enumerate_index() { assert_eq!(actual.out, "[0, 1, 2, 3]"); } -#[test] -fn each_element_continue_command() { - let actual = - nu!("[1,2,3,4,6,7] | each { |x| if ($x mod 2 == 0) {continue} else { $x }} | to nuon"); - - assert_eq!(actual.out, "[1, 3, 7]"); -} - -#[test] -fn each_element_break_command() { - let actual = - nu!("[1,2,5,4,6,7] | each { |x| if ($x mod 3 == 0) {break} else { $x }} | to nuon"); - - assert_eq!(actual.out, "[1, 2, 5, 4]"); -} - #[test] fn errors_in_nested_each_show() { let actual = nu!("[[1,2]] | each {|x| $x | each {|y| error make {msg: \"oh noes\"} } }");