fmt: don't remove `mut` from `if mut` smart cast (#6188)
parent
06967d9297
commit
1b914d217e
|
@ -472,9 +472,10 @@ pub:
|
|||
pos token.Position
|
||||
body_pos token.Position
|
||||
comments []Comment
|
||||
left_as_name string // `name` in `if cond is SumType as name`
|
||||
mut_name bool // `if mut name is`
|
||||
pub mut:
|
||||
smartcast bool // should only be true if cond is `x is sumtype`, it will be set in checker - if_expr
|
||||
left_as_name string // only used in x is SumType check
|
||||
smartcast bool // true when cond is `x is SumType`, set in checker.if_expr
|
||||
}
|
||||
|
||||
pub struct UnsafeExpr {
|
||||
|
|
|
@ -1357,35 +1357,30 @@ pub fn (mut f Fmt) if_expr(it ast.IfExpr) {
|
|||
}
|
||||
}
|
||||
if i == 0 {
|
||||
// first `if`
|
||||
f.comments(branch.comments, {})
|
||||
f.write('if ')
|
||||
f.expr(branch.cond)
|
||||
if smartcast_as {
|
||||
f.write(' as $branch.left_as_name')
|
||||
}
|
||||
f.write(' {')
|
||||
} else if i < it.branches.len - 1 || !it.has_else {
|
||||
} else {
|
||||
// `else`, close previous branch
|
||||
if branch.comments.len > 0 {
|
||||
f.writeln('}')
|
||||
f.comments(branch.comments, {})
|
||||
} else {
|
||||
f.write('} ')
|
||||
}
|
||||
f.write('else if ')
|
||||
f.expr(branch.cond)
|
||||
if smartcast_as {
|
||||
f.write(' as $branch.left_as_name')
|
||||
}
|
||||
f.write(' {')
|
||||
} else if i == it.branches.len - 1 && it.has_else {
|
||||
if branch.comments.len > 0 {
|
||||
f.writeln('}')
|
||||
f.comments(branch.comments, {})
|
||||
} else {
|
||||
f.write('} ')
|
||||
}
|
||||
f.write('else {')
|
||||
f.write('else ')
|
||||
}
|
||||
if i < it.branches.len - 1 || !it.has_else {
|
||||
f.write('if ')
|
||||
if branch.mut_name {
|
||||
f.write('mut ')
|
||||
}
|
||||
f.expr(branch.cond)
|
||||
if smartcast_as {
|
||||
f.write(' as $branch.left_as_name')
|
||||
}
|
||||
f.write(' ')
|
||||
}
|
||||
f.write('{')
|
||||
if single_line {
|
||||
f.write(' ')
|
||||
} else {
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
struct S1 {
|
||||
mut:
|
||||
i int
|
||||
}
|
||||
|
||||
struct S2 {
|
||||
|
@ -7,7 +9,8 @@ struct S2 {
|
|||
type Sum = S1 | S2
|
||||
|
||||
fn f(sum Sum) {
|
||||
if sum is S1 {
|
||||
if mut sum is S1 {
|
||||
sum.i++
|
||||
}
|
||||
if sum is S1 as s1 {
|
||||
}
|
||||
|
|
|
@ -21,15 +21,11 @@ fn (mut p Parser) if_expr() ast.IfExpr {
|
|||
for p.tok.kind in [.key_if, .key_else] {
|
||||
p.inside_if = true
|
||||
start_pos := p.tok.position()
|
||||
if p.tok.kind == .key_if {
|
||||
p.next()
|
||||
} else {
|
||||
if p.tok.kind == .key_else {
|
||||
comments << p.eat_comments()
|
||||
p.check(.key_else)
|
||||
comments << p.eat_comments()
|
||||
if p.tok.kind == .key_if {
|
||||
p.next()
|
||||
} else {
|
||||
if p.tok.kind == .lcbr {
|
||||
// else {
|
||||
has_else = true
|
||||
p.inside_if = false
|
||||
|
@ -64,9 +60,18 @@ fn (mut p Parser) if_expr() ast.IfExpr {
|
|||
break
|
||||
}
|
||||
}
|
||||
// `if` or `else if`
|
||||
p.check(.key_if)
|
||||
comments << p.eat_comments()
|
||||
// `if mut name is T`
|
||||
mut mut_name := false
|
||||
if p.tok.kind == .key_mut && p.peek_tok2.kind == .key_is {
|
||||
mut_name = true
|
||||
p.next()
|
||||
comments << p.eat_comments()
|
||||
}
|
||||
mut cond := ast.Expr{}
|
||||
mut is_guard := false
|
||||
comments << p.eat_comments()
|
||||
// `if x := opt() {`
|
||||
if p.peek_tok.kind == .decl_assign {
|
||||
p.open_scope()
|
||||
|
@ -121,6 +126,7 @@ fn (mut p Parser) if_expr() ast.IfExpr {
|
|||
body_pos: body_pos.extend(p.prev_tok.position())
|
||||
comments: comments
|
||||
left_as_name: left_as_name
|
||||
mut_name: mut_name
|
||||
}
|
||||
comments = p.eat_comments()
|
||||
if p.tok.kind != .key_else {
|
||||
|
|
Loading…
Reference in New Issue