Repository navigation
fix(health): suppress defer_in_loop when the next statement exits the loop - #2942
Merged
RaghavChamadiya merged 1 commit intoOct 3, 2026
Merged
Conversation
… loop A defer immediately followed by a return, or by a break that exits the for loop (not a switch/select case, and not only an inner loop of a nest), is reached at most once per entry, so the deferred call cannot pile up per iteration. Unlabeled break resolution walks to the innermost breakable statement; a labeled break counts only when the label names a for statement with no further loop above it up to the function scope. Everything else - a call between the defer and the exit, continue, switch/select-case breaks, labels naming inner statements, nested-loop breaks - keeps firing, and the existing fixture count is unchanged. Closes repowise-dev#2938
Contributor
Repowise checks
Tests: which tests reach the changed files (inferred)
Inferred from the code graph of the last indexed commit ( Configure checks: bot settings |
6 tasks done
RaghavChamadiya
approved these changes
Oct 3, 2026
RaghavChamadiya
left a comment
Member
There was a problem hiding this comment.
Thanks @Pushpak731, this is careful work. Requiring the exit to leave the outermost loop is the part that makes it right: in for { for { defer f(); break } } the outer iteration re-enters and registers the defer again, and the test file pins that along with the func-literal return and labelled break cases. Merging.
7 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
loop_stmt_markerreturnsNonefor adefer_statementwhose next statement in the same block is areturn, or abreakthat exits the loop: such a defer can only be reached once per entry, so the deferred call cannot pile up per iteration (Godeferthat runs once because the loop exits right after it is reported as defer in a loop #2938's hugotransform.goshape).breakresolves to the innermost breakable statement and only counts when that is theforAND no further loop wraps it up to the function scope (otherwise each outer iteration re-enters and registers the defer again). A labeledbreakcounts only when the label names aforstatement, again with no loop above it.switch/select-case breaks, labels naming inner statements,continue, and any other statement between the defer and the exit keep firing. Kept to the syntactic next-statement form per the issue; comments and the;separator of single-line bodies are skipped.deferInLoopkeeps its count of 1.Related Issues
Closes #2938
Test Plan
pytest)ruff check .)New
tests/unit/health/test_perf_go_defer_exit.py: 33 tests covering all four loop forms, bare/value/multi-value returns, the hugoif { defer; break }shape, comment and;skipping, labeled breaks naming the loop vs. naming an inner switch/if, labeled break out of a nested loop, switch/select/type-switch case breaks, inner-loop-only breaks in a nest,continue,goto, break-before-defer, per-defer coverage in one body, and the five repro shapes from the issue.tests/unit/health/in full: 2858 passed, 1 skipped;ruff checkclean.Checklist