needsImplicitReturn checks if a block body needs an implicit return. Returns true if the block contains a single expression without return/if/for/switch.
()
| 125 | // needsImplicitReturn checks if a block body needs an implicit return. |
| 126 | // Returns true if the block contains a single expression without return/if/for/switch. |
| 127 | func (g *LambdaCodeGen) needsImplicitReturn() bool { |
| 128 | body := g.expr.Body |
| 129 | if len(body) < 2 { |
| 130 | return false |
| 131 | } |
| 132 | |
| 133 | // Remove outer braces and trim whitespace |
| 134 | inner := strings.TrimSpace(body[1 : len(body)-1]) |
| 135 | if inner == "" { |
| 136 | return false |
| 137 | } |
| 138 | |
| 139 | // If already has return, don't add another |
| 140 | if strings.HasPrefix(inner, "return ") || strings.HasPrefix(inner, "return\t") || inner == "return" { |
| 141 | return false |
| 142 | } |
| 143 | |
| 144 | // If contains control flow statements, don't add implicit return |
| 145 | // These indicate multi-statement blocks |
| 146 | if strings.Contains(inner, "if ") || strings.Contains(inner, "for ") || |
| 147 | strings.Contains(inner, "switch ") || strings.Contains(inner, "select ") || |
| 148 | strings.Contains(inner, "go ") || strings.Contains(inner, "defer ") { |
| 149 | return false |
| 150 | } |
| 151 | |
| 152 | // If contains semicolons or newlines with statements, it's multi-statement |
| 153 | if strings.Contains(inner, ";") || strings.Contains(inner, "\n") { |
| 154 | return false |
| 155 | } |
| 156 | |
| 157 | return true |
| 158 | } |
| 159 | |
| 160 | // writeBlockWithImplicitReturn writes a block body with return added. |
| 161 | // Transforms { expr } to { return expr } |