Skip to content

Fix formatter crash on bare return in lambda#413

Open
kuruk-mm wants to merge 1 commit intoScony:masterfrom
kuruk-mm:fix/bare-return-in-lambda
Open

Fix formatter crash on bare return in lambda#413
kuruk-mm wants to merge 1 commit intoScony:masterfrom
kuruk-mm:fix/bare-return-in-lambda

Conversation

@kuruk-mm
Copy link
Copy Markdown

@kuruk-mm kuruk-mm commented Feb 9, 2026

Summary

  • Fix IndexError: list index out of range when formatting a bare return inside a lambda (e.g. func(): return)
  • The return_stmt handler in function_statement_to_str.py unconditionally accessed s.children[0], but a bare return has zero children
  • The grammar correctly defines return_stmt: "return" [expr] with an optional expression — this fix aligns the formatter with the grammar

Reproduction

Any GDScript file containing a lambda with a bare return triggers the crash:

tween_to(0.0, func(): return, true)

Test plan

  • gdformat no longer crashes on files with bare return in lambdas
  • All existing tests pass (1072 passed, 338 skipped)

@kuruk-mm kuruk-mm marked this pull request as draft February 9, 2026 13:56
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.
@kuruk-mm kuruk-mm force-pushed the fix/bare-return-in-lambda branch from db4ecd5 to 4816ef0 Compare February 9, 2026 13:56
@kuruk-mm kuruk-mm marked this pull request as ready for review February 9, 2026 13:56
@Scony
Copy link
Copy Markdown
Owner

Scony commented Mar 7, 2026

Thanks! Could you please add a test case for that? You can check input/output pairs on how to do this - in such case you just need to add 2 files. As for running tests, please check the README.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants