From 9fd680ae2bcfb3ae6b7ed26990f4bcd28c06ddfd Mon Sep 17 00:00:00 2001 From: kiloson Date: Thu, 9 Dec 2021 20:19:51 +0800 Subject: [PATCH] fix: Implicit coercion of boolean false and empty value #4094 (#4120) Signed-off-by: closetool --- crates/nu-data/src/base.rs | 4 ++-- .../nu-engine/tests/evaluate/subexpression.rs | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/crates/nu-data/src/base.rs b/crates/nu-data/src/base.rs index 0f6f5f2d1f..f0b76d7acb 100644 --- a/crates/nu-data/src/base.rs +++ b/crates/nu-data/src/base.rs @@ -164,8 +164,8 @@ pub fn coerce_compare_primitive( (Date(left), Date(right)) => CompareValues::Date(*left, *right), (Date(left), Duration(right)) => CompareValues::DateDuration(*left, right.clone()), (Boolean(left), Boolean(right)) => CompareValues::Booleans(*left, *right), - (Boolean(left), Nothing) => CompareValues::Booleans(*left, false), - (Nothing, Boolean(right)) => CompareValues::Booleans(false, *right), + (Boolean(left), Nothing) => CompareValues::Ints(if *left { 1 } else { 0 }, -1), + (Nothing, Boolean(right)) => CompareValues::Ints(-1, if *right { 1 } else { 0 }), (String(left), Nothing) => CompareValues::String(left.clone(), std::string::String::new()), (Nothing, String(right)) => { CompareValues::String(std::string::String::new(), right.clone()) diff --git a/crates/nu-engine/tests/evaluate/subexpression.rs b/crates/nu-engine/tests/evaluate/subexpression.rs index 20aa7c3ab1..d1713568f0 100644 --- a/crates/nu-engine/tests/evaluate/subexpression.rs +++ b/crates/nu-engine/tests/evaluate/subexpression.rs @@ -34,3 +34,21 @@ fn compare_to_nothing() { ); assert_eq!(actual.out, "true"); } + +#[test] +fn compare_nothing_and_boolean() { + let actual = nu!( + cwd: ".", + r#" + if $true == $nothing {echo $true} {echo $false} + "# + ); + assert_eq!(actual.out, "false"); + let actual = nu!( + cwd: ".", + r#" + if $false == $nothing {echo $true} {echo $false} + "# + ); + assert_eq!(actual.out, "false"); +}