diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index e696a215ce..e6df84d0d8 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -1267,6 +1267,7 @@ pub mut: cond_type Type // type of `x` in `match x {` expected_type Type // for debugging only is_sum_type bool + no_lcbr bool // for match statements without {} } pub struct MatchBranch { diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 7029ee7c98..569f691e50 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -2858,8 +2858,12 @@ fn (mut f Fmt) match_branch(branch ast.MatchBranch, single_line bool) { pub fn (mut f Fmt) match_expr(node ast.MatchExpr) { f.write('match ') f.expr(node.cond) - f.writeln(' {') - f.indent++ + if node.no_lcbr { + f.writeln('') + } else { + f.writeln(' {') + f.indent++ + } f.comments(node.comments) mut single_line := true for branch in node.branches { @@ -2886,8 +2890,10 @@ pub fn (mut f Fmt) match_expr(node ast.MatchExpr) { if else_idx >= 0 { f.match_branch(node.branches[else_idx], single_line) } - f.indent-- - f.write('}') + if !node.no_lcbr { + f.indent-- + f.write('}') + } } pub fn (mut f Fmt) offset_of(node ast.OffsetOf) { diff --git a/vlib/v/parser/if_match.v b/vlib/v/parser/if_match.v index 0327d306f2..3edb3da7f9 100644 --- a/vlib/v/parser/if_match.v +++ b/vlib/v/parser/if_match.v @@ -375,7 +375,7 @@ fn (mut p Parser) match_expr() ast.MatchExpr { len: match_last_pos.pos - match_first_pos.pos + match_last_pos.len col: match_first_pos.col } - if p.tok.kind == .rcbr { + if p.tok.kind == .rcbr && !no_lcbr { p.check(.rcbr) } // return ast.StructInit{} @@ -386,6 +386,7 @@ fn (mut p Parser) match_expr() ast.MatchExpr { is_sum_type: is_sum_type pos: pos comments: comments + no_lcbr: no_lcbr } } diff --git a/vlib/v/tests/options/option_match_test.v b/vlib/v/tests/options/option_match_test.v index ef9e082210..24995910af 100644 --- a/vlib/v/tests/options/option_match_test.v +++ b/vlib/v/tests/options/option_match_test.v @@ -1,35 +1,32 @@ fn test_simple_match_expr() { mut a := ?int(12) - match a? { - 12 { - println(a) - } - else { - println('else') - assert false - } + match a? + 12 { + println(a) + } + else { + println('else') + assert false } - match a { - none { - println('none') - assert false - } - else { - println('else') - } + match a + none { + println('none') + assert false + } + else { + println('else') } a = none - match a { - none { - println('none') - } - else { - println('else') - assert false - } + match a + none { + println('none') + } + else { + println('else') + assert false } mut b := ?string('aaa') @@ -43,24 +40,23 @@ fn test_simple_match_expr() { } } - match b { - none { - println('none') - assert false - } - else { - println('else') - } + match b + none { + println('none') + assert false + } + else { + println('else') } b = none - match b { - none { - println('none') - } - else { - println('else') - assert false - } + match b + none { + println('none') } + else { + println('else') + assert false + } + }