From 42ae1b308038fd272a7c925efb3883952a14456e Mon Sep 17 00:00:00 2001 From: Krchi <997054144@qq.com> Date: Sun, 24 Aug 2025 04:49:47 +0800 Subject: [PATCH] fmt: fix call expr with single line comment in or expr (fix #24659) (#25159) --- vlib/v/fmt/fmt.v | 17 ++++++++++++++++- vlib/v/fmt/tests/comments_keep.vv | 7 +++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 4ff3cda1f2..283150aa1e 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -846,6 +846,21 @@ fn expr_is_single_line(expr ast.Expr) bool { ast.StringLiteral { 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 {} } return true @@ -2923,7 +2938,7 @@ pub fn (mut f Fmt) or_expr(node ast.OrExpr) { } f.write('}') 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, // so, since this'll all be on one line, trim any possible whitespace str := f.node_str(node.stmts[0]).trim_space() diff --git a/vlib/v/fmt/tests/comments_keep.vv b/vlib/v/fmt/tests/comments_keep.vv index 5af72801aa..61f359fa9a 100644 --- a/vlib/v/fmt/tests/comments_keep.vv +++ b/vlib/v/fmt/tests/comments_keep.vv @@ -99,3 +99,10 @@ fn ifs_comments_and_empty_lines() { // thereore keep the empty line something_else() } + +fn call_expr_with_single_line_comment_in_or_expr(arr []int) int { + i := arr[0] or { + arr.count() // 1 + } + return i +}