mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix return if_guard expr (#9375)
This commit is contained in:
parent
3f9e921c95
commit
ead0dff55a
@ -4357,6 +4357,9 @@ fn (mut g Gen) need_tmp_var_in_if(node ast.IfExpr) bool {
|
||||
return true
|
||||
}
|
||||
for branch in node.branches {
|
||||
if branch.cond is ast.IfGuardExpr {
|
||||
return true
|
||||
}
|
||||
if branch.stmts.len == 1 {
|
||||
if branch.stmts[0] is ast.ExprStmt {
|
||||
stmt := branch.stmts[0] as ast.ExprStmt
|
||||
|
@ -59,3 +59,22 @@ fn test_chan_pop() {
|
||||
assert res == [6.75, -3.25, -37.5]
|
||||
}
|
||||
|
||||
struct Thing {
|
||||
name string
|
||||
}
|
||||
|
||||
fn test_return_if_guard() {
|
||||
ret := option_check('zoo')
|
||||
println(ret)
|
||||
assert ret == 'zs'
|
||||
}
|
||||
|
||||
fn option_check(name string) string {
|
||||
return if thing := find_thing_by_name(name) { thing.name } else { 'safename' }
|
||||
}
|
||||
|
||||
fn find_thing_by_name(name string) ?&Thing {
|
||||
return &Thing{
|
||||
name: 'zs'
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user