fix: Implicit coercion of boolean false and empty value #4094 (#4120)

Signed-off-by: closetool <c299999999@qq.com>
This commit is contained in:
kiloson 2021-12-09 20:19:51 +08:00 committed by GitHub
parent ad94ed5e13
commit 9fd680ae2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 2 deletions

View File

@ -164,8 +164,8 @@ pub fn coerce_compare_primitive(
(Date(left), Date(right)) => CompareValues::Date(*left, *right), (Date(left), Date(right)) => CompareValues::Date(*left, *right),
(Date(left), Duration(right)) => CompareValues::DateDuration(*left, right.clone()), (Date(left), Duration(right)) => CompareValues::DateDuration(*left, right.clone()),
(Boolean(left), Boolean(right)) => CompareValues::Booleans(*left, *right), (Boolean(left), Boolean(right)) => CompareValues::Booleans(*left, *right),
(Boolean(left), Nothing) => CompareValues::Booleans(*left, false), (Boolean(left), Nothing) => CompareValues::Ints(if *left { 1 } else { 0 }, -1),
(Nothing, Boolean(right)) => CompareValues::Booleans(false, *right), (Nothing, Boolean(right)) => CompareValues::Ints(-1, if *right { 1 } else { 0 }),
(String(left), Nothing) => CompareValues::String(left.clone(), std::string::String::new()), (String(left), Nothing) => CompareValues::String(left.clone(), std::string::String::new()),
(Nothing, String(right)) => { (Nothing, String(right)) => {
CompareValues::String(std::string::String::new(), right.clone()) CompareValues::String(std::string::String::new(), right.clone())

View File

@ -34,3 +34,21 @@ fn compare_to_nothing() {
); );
assert_eq!(actual.out, "true"); 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");
}