Skip to content

Commit db4ecd5

Browse files
committed
Fix formatter crash on bare return in lambda
The return_stmt handler unconditionally accessed s.children[0], but a bare `return` (without expression) has zero children. This caused an IndexError when formatting lambdas like `func(): return`. The grammar correctly defines return_stmt with an optional expression (`return_stmt: "return" [expr]`), so the formatter now checks for empty children before accessing the list.
1 parent 9d84051 commit db4ecd5

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

gdtoolkit/formatter/function_statement_to_str.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def function_statement_to_str(statement: Tree) -> str:
1212
"func_var_stmt": lambda s: function_statement_to_str(s.children[0]),
1313
"const_stmt": lambda s: function_statement_to_str(s.children[0]),
1414
"expr_stmt": lambda s: expression_to_str(s.children[0]), # TODO: standalone?
15-
"return_stmt": lambda s: f"return {standalone_expression_to_str(s.children[0])}",
15+
"return_stmt": lambda s: "return" if len(s.children) == 0 else f"return {standalone_expression_to_str(s.children[0])}",
1616
"break_stmt": _not_implemented,
1717
"breakpoint_stmt": lambda _: "breakpoint",
1818
"continue_stmt": _not_implemented,

0 commit comments

Comments
 (0)