fmt: fix call expr with single line comment in or expr (fix #24659) (#25159)

This commit is contained in:
Krchi 2025-08-24 04:49:47 +08:00 committed by GitHub
parent 42777f0125
commit 42ae1b3080
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 23 additions and 1 deletions

View file

@ -846,6 +846,21 @@ fn expr_is_single_line(expr ast.Expr) bool {
ast.StringLiteral { ast.StringLiteral {
return expr.pos.line_nr == expr.pos.last_line return expr.pos.line_nr == expr.pos.last_line
} }
ast.OrExpr {
if expr.stmts.len == 1 && stmt_is_single_line(expr.stmts[0]) {
stmt := expr.stmts[0]
if stmt is ast.ExprStmt && stmt.expr is ast.CallExpr
&& (stmt.expr as ast.CallExpr).comments.len > 0 {
if comment := stmt.expr.comments[0] {
if !comment.is_multi {
return false
}
}
}
return true
}
return false
}
else {} else {}
} }
return true return true
@ -2923,7 +2938,7 @@ pub fn (mut f Fmt) or_expr(node ast.OrExpr) {
} }
f.write('}') f.write('}')
return return
} else if node.stmts.len == 1 && stmt_is_single_line(node.stmts[0]) { } else if expr_is_single_line(node) {
// the control stmts (return/break/continue...) print a newline inside them, // the control stmts (return/break/continue...) print a newline inside them,
// so, since this'll all be on one line, trim any possible whitespace // so, since this'll all be on one line, trim any possible whitespace
str := f.node_str(node.stmts[0]).trim_space() str := f.node_str(node.stmts[0]).trim_space()

View file

@ -99,3 +99,10 @@ fn ifs_comments_and_empty_lines() {
// thereore keep the empty line // thereore keep the empty line
something_else() something_else()
} }
fn call_expr_with_single_line_comment_in_or_expr(arr []int) int {
i := arr[0] or {
arr.count() // 1
}
return i
}