fmt: do not write single line or blocks with multi line stmts (#8952)

pull/8980/head
Lukas Neubert 2021-02-26 07:41:24 +01:00 committed by GitHub
parent 8dff168e01
commit 2c609411dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 10 deletions

View File

@ -794,10 +794,12 @@ fn (mut b Builder) cc_linux_cross() {
if b.pref.show_cc {
println(cc_cmd)
}
cc_res := os.exec(cc_cmd) or { os.Result{
cc_res := os.exec(cc_cmd) or {
os.Result{
exit_code: 1
output: 'no `cc` command found'
} }
}
}
if cc_res.exit_code != 0 {
println('Cross compilation for Linux failed (first step, cc). Make sure you have clang installed.')
verror(cc_res.output)

View File

@ -459,11 +459,10 @@ pub fn (mut f Fmt) stmt(node ast.Stmt) {
}
fn stmt_is_single_line(stmt ast.Stmt) bool {
match stmt {
ast.ExprStmt { return expr_is_single_line(stmt.expr) }
ast.Return { return true }
ast.AssignStmt { return true }
else { return false }
return match stmt {
ast.ExprStmt, ast.AssertStmt { expr_is_single_line(stmt.expr) }
ast.Return, ast.AssignStmt, ast.BranchStmt { true }
else { false }
}
}
@ -1065,7 +1064,7 @@ pub fn (mut f Fmt) or_expr(node ast.OrExpr) {
if node.stmts.len == 0 {
f.write(' or { }')
return
} else if node.stmts.len == 1 {
} else if node.stmts.len == 1 && stmt_is_single_line(node.stmts[0]) {
// the control stmts (return/break/continue...) print a newline inside them,
// so, since this'll all be on one line, trim any possible whitespace
str := f.stmt_str(node.stmts[0]).trim_space()

View File

@ -18,3 +18,11 @@ fn unwrapped_single_line_if() {
println('Another stmt')
}
}
fn or_with_one_multi_line_stmt() {
b := or_func() or {
MyStruct{
val: 'xyz'
}
}
}

View File

@ -0,0 +1,8 @@
fn single_line_stmts() {
// Wouldn't be the or-block's stmt be single line, the block would be written as multi line
foo() or { assert false }
for {
foo() or { break }
}
foo() or { return }
}