From 4205697d88400f172c66ea8a89a6c6719370075e Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 22 Jul 2025 11:45:59 +0300 Subject: [PATCH] fmt: fix `if x := g() { x } else { return }` adding newline after `return` (fix #24908) (#24945) --- vlib/v/fmt/fmt.v | 4 +++- vlib/v/fmt/tests/if_ternary_return_keep.vv | 8 ++++++++ vlib/v/tests/fns/closure_test.v | 4 +--- 3 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 vlib/v/fmt/tests/if_ternary_return_keep.vv diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index b6811980f2..c95643f223 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1613,7 +1613,9 @@ pub fn (mut f Fmt) return_stmt(node ast.Return) { } } } - f.writeln('') + if !f.single_line_if { + f.writeln('') + } } pub fn (mut f Fmt) sql_stmt(node ast.SqlStmt) { diff --git a/vlib/v/fmt/tests/if_ternary_return_keep.vv b/vlib/v/fmt/tests/if_ternary_return_keep.vv new file mode 100644 index 0000000000..ca90228682 --- /dev/null +++ b/vlib/v/fmt/tests/if_ternary_return_keep.vv @@ -0,0 +1,8 @@ +module main + +fn g() ?int { + return 123 +} + +x := if ff := g() { ff } else { return } +println(x) diff --git a/vlib/v/tests/fns/closure_test.v b/vlib/v/tests/fns/closure_test.v index d5d75b0c09..5495e68965 100644 --- a/vlib/v/tests/fns/closure_test.v +++ b/vlib/v/tests/fns/closure_test.v @@ -155,9 +155,7 @@ fn test_go_call_closure() { fn test_closures_with_ifstmt() { a := 1 f := fn [a] (x int) int { - if a > x { return 1 - } else { return -1 - } + if a > x { return 1 } else { return -1 } } g := fn [a] () int { if true {