Skip to content

Commit

Permalink
Support pattern matching null literals (nushell#10829)
Browse files Browse the repository at this point in the history
# Description
Support pattern matching against the `null` literal.  Fixes nushell#10799 

### Before
```nushell
> match null { null => "success", _ => "failure" }
failure
```

### After
```nushell
> match null { null => "success", _ => "failure" }
success
```

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Users can pattern match against a `null` literal.
  • Loading branch information
hudclark authored and hardfau1t committed Dec 14, 2023
1 parent d5560d0 commit 26dd8a8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
6 changes: 6 additions & 0 deletions crates/nu-command/tests/commands/match_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ fn match_constant_7() {
assert_eq!(actual.out, "success");
}

#[test]
fn match_null() {
let actual = nu!(r#"match null { null => { print "success"}, _ => { print "failure" }}"#);
assert_eq!(actual.out, "success");
}

#[test]
fn match_or_pattern() {
let actual = nu!(
Expand Down
3 changes: 3 additions & 0 deletions crates/nu-protocol/src/engine/pattern_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ impl Matcher for Pattern {
Pattern::Value(pattern_value) => {
// TODO: Fill this out with the rest of them
match &pattern_value.expr {
Expr::Nothing => {
matches!(value, Value::Nothing { .. })
}
Expr::Int(x) => {
if let Value::Int { val, .. } = &value {
x == val
Expand Down

0 comments on commit 26dd8a8

Please sign in to comment.