fmt: fix fmt error of anon fn with if expr (fix #14393) (#14413)

master
yuyi 2022-05-16 14:48:21 +08:00 committed by GitHub
parent 5068b8b293
commit 7fe3ef9a6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 4 deletions

View File

@ -1951,7 +1951,8 @@ pub fn (mut f Fmt) if_expr(node ast.IfExpr) {
}
fn branch_is_single_line(b ast.IfBranch) bool {
if b.stmts.len == 1 && b.comments.len == 0 && stmt_is_single_line(b.stmts[0]) {
if b.stmts.len == 1 && b.comments.len == 0 && stmt_is_single_line(b.stmts[0])
&& b.pos.line_nr == b.stmts[0].pos.line_nr {
return true
}
return false

View File

@ -0,0 +1,10 @@
fn main() {
a := fn () {
if true {
println('a')
} else {
println('a')
}
}
a()
}

View File

@ -0,0 +1,10 @@
fn main() {
a := fn () {
if true {
println('a')
} else {
println('a')
}
}
a()
}

View File

@ -64,8 +64,7 @@ a: 5
lock x { // wait for ongoing reads to finish, don't start new ones
x.a = 17 // this value should never be read
time.sleep(50* time.millisecond)
x.a = if (i & 1) == 0 {
7} else {5}
x.a = if (i & 1) == 0 { 7 } else { 5 }
} // now new reads are possible again
time.sleep(20*time.millisecond)
}

View File

@ -60,7 +60,11 @@ fn main() {
(aa % gcd) == big.zero_int,
(bb % gcd) == big.zero_int,
].all(it == true)
{ true } else { false }
{
true
} else {
false
}
}
cfgs := [