@@ -185,15 +185,26 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
185185 // easier to use a temp var, than do C tricks with commas, introduce special vars etc
186186 // (as it used to be done).
187187 // Always use this in -autofree, since ?: can have tmp expressions that have to be freed.
188- needs_tmp_var := g.need_tmp_var_in_if (node)
188+ needs_tmp_var := g.inside_if_option || g. need_tmp_var_in_if (node)
189189 needs_conds_order := g.needs_conds_order (node)
190- tmp := if node.typ != ast.void_type && needs_tmp_var { g.new_tmp_var () } else { '' }
190+ tmp := if g.inside_if_option || (node.typ != ast.void_type && needs_tmp_var) {
191+ g.new_tmp_var ()
192+ } else {
193+ ''
194+ }
191195 mut cur_line := ''
192196 mut raw_state := false
197+ tmp_if_option_type := g.last_if_option_type
193198 if needs_tmp_var {
194199 mut styp := g.styp (node.typ)
195- if node.typ.has_flag (.option) {
200+ if g.inside_if_option || node.typ.has_flag (.option) {
196201 raw_state = g.inside_if_option
202+ if node.typ != ast.void_type {
203+ g.last_if_option_type = node.typ
204+ defer {
205+ g.last_if_option_type = tmp_if_option_type
206+ }
207+ }
197208 defer {
198209 g.inside_if_option = raw_state
199210 }
@@ -210,7 +221,13 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
210221 cur_line = g.go_before_last_stmt ()
211222 g.empty_line = true
212223 if tmp != '' {
213- g.writeln ('${styp} ${tmp} ; /* if prepend */' )
224+ if node.typ == ast.void_type && g.last_if_option_type != 0 {
225+ // nested if on return stmt
226+ g.write2 (g.styp (g.last_if_option_type), ' ' )
227+ } else {
228+ g.write ('${styp} ' )
229+ }
230+ g.writeln ('${tmp} ; /* if prepend */' )
214231 }
215232 if g.infix_left_var_name.len > 0 {
216233 g.writeln ('if (${g.infix_left_var_name} ) {' )
0 commit comments