diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 7ee9b4296c..99ac0ff21b 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1537,7 +1537,8 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type { } if call_expr.generic_types.len > 0 && method.return_type != 0 { if typ := c.resolve_generic_type(method.return_type, method.generic_names, - call_expr.generic_types) { + call_expr.generic_types) + { call_expr.return_type = typ return typ } diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 810f313173..1396460aba 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1564,8 +1564,17 @@ pub fn (mut f Fmt) if_expr(it ast.IfExpr) { } if i < it.branches.len - 1 || !it.has_else { f.write('${dollar}if ') + cur_pos := f.out.len f.expr(branch.cond) - f.write(' ') + cond_len := f.out.len - cur_pos + is_cond_wrapped := cond_len > 0 + && (branch.cond is ast.IfGuardExpr || branch.cond is ast.CallExpr) + && '\n' in f.out.last_n(cond_len) + if is_cond_wrapped { + f.writeln('') + } else { + f.write(' ') + } } f.write('{') if single_line { diff --git a/vlib/v/fmt/tests/if_brace_on_newline_expected.vv b/vlib/v/fmt/tests/if_brace_on_newline_expected.vv new file mode 100644 index 0000000000..66131089ad --- /dev/null +++ b/vlib/v/fmt/tests/if_brace_on_newline_expected.vv @@ -0,0 +1,12 @@ +fn get_typ() Type { + { + { + // The opening brace should be put on a new line here for readability + if typ := c.resolve_generic_type(method.return_type, method.generic_names, + call_expr.generic_types) + { + return typ + } + } + } +} diff --git a/vlib/v/fmt/tests/if_brace_on_newline_input.vv b/vlib/v/fmt/tests/if_brace_on_newline_input.vv new file mode 100644 index 0000000000..52b9b2a2d5 --- /dev/null +++ b/vlib/v/fmt/tests/if_brace_on_newline_input.vv @@ -0,0 +1,10 @@ +fn get_typ() Type { + { + { + // The opening brace should be put on a new line here for readability + if typ := c.resolve_generic_type(method.return_type, method.generic_names, call_expr.generic_types) { + return typ + } + } + } +}