vfmt: fmt '!(a in/is b)' to 'a !in/is b' (#11335)

pull/11340/head
yuyi 2021-08-30 14:45:36 +08:00 committed by GitHub
parent 61ac7b671d
commit f44eb88a8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 59 additions and 5 deletions

View File

@ -551,8 +551,8 @@ fn test_in() {
assert 1 in a assert 1 in a
assert 2 in a assert 2 in a
assert 3 in a assert 3 in a
assert !(4 in a) assert 4 !in a
assert !(0 in a) assert 0 !in a
assert 0 !in a assert 0 !in a
assert 4 !in a assert 4 !in a
b := [1, 4, 0] b := [1, 4, 0]

View File

@ -2195,6 +2195,26 @@ pub fn (mut f Fmt) postfix_expr(node ast.PostfixExpr) {
} }
pub fn (mut f Fmt) prefix_expr(node ast.PrefixExpr) { pub fn (mut f Fmt) prefix_expr(node ast.PrefixExpr) {
// !(a in b) => a !in b, !(a is b) => a !is b
if node.op == .not && node.right is ast.ParExpr {
if node.right.expr is ast.InfixExpr {
if node.right.expr.op in [.key_in, .not_in, .key_is, .not_is]
&& node.right.expr.right !is ast.InfixExpr {
f.expr(node.right.expr.left)
if node.right.expr.op == .key_in {
f.write(' !in ')
} else if node.right.expr.op == .not_in {
f.write(' in ')
} else if node.right.expr.op == .key_is {
f.write(' !is ')
} else if node.right.expr.op == .not_is {
f.write(' is ')
}
f.expr(node.right.expr.right)
return
}
}
}
f.write(node.op.str()) f.write(node.op.str())
f.prefix_expr_cast_expr(node.right) f.prefix_expr_cast_expr(node.right)
f.or_expr(node.or_block) f.or_expr(node.or_block)

View File

@ -0,0 +1,17 @@
struct Foo1 {}
struct Foo2 {}
type Foo = Foo1 | Foo2
fn main() {
a := [1, 2, 3]
if 4 !in a {
println('4 not in a')
}
foo := Foo(Foo1{})
if foo !is Foo2 {
println('foo is not Foo2')
}
}

View File

@ -0,0 +1,17 @@
struct Foo1{}
struct Foo2{}
type Foo = Foo1 | Foo2
fn main() {
a := [1, 2, 3]
if !(4 in a) {
println('4 not in a')
}
foo := Foo(Foo1{})
if !(foo is Foo2) {
println('foo is not Foo2')
}
}

View File

@ -537,8 +537,8 @@ fn main() {
println(1 in a) println(1 in a)
println(2 in a) println(2 in a)
println(3 in a) println(3 in a)
println(!(4 in a)) println(4 !in a)
println(!(0 in a)) println(0 !in a)
println(0 !in a) println(0 !in a)
println(4 !in a) println(4 !in a)
b := [1, 4, 0] b := [1, 4, 0]

View File

@ -553,7 +553,7 @@ fn sumtype_match_with_string_interpolation(code int) string {
fn handle(e Expr) string { fn handle(e Expr) string {
is_literal := e is IntegerLiteral is_literal := e is IntegerLiteral
assert is_literal assert is_literal
assert !(e !is IntegerLiteral) assert e is IntegerLiteral
if e is IntegerLiteral { if e is IntegerLiteral {
assert typeof(e.val).name == 'string' assert typeof(e.val).name == 'string'
} }