chore: enable go-critic deferInLoop lint #2825
Merged
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.
The goal here is to be more careful about leaking file handles and other resources.
Issue #2819 alerted us that we are not closing file handles aggressively enough.
This lint is not on by default, because it is still tagged as experimental, see https://go-critic.com/overview.html#deferinloop
Manual testing:
Introduce the following diff locally (note
defer reader.Close()
in the loop):On
main
,make lint
with this diff exits 0.On this branch,
make lint
fails like this:Also, the following command was used to confirm that enabling additional lints for
gocritic
appends to the set of enabled lints, rather than overwriting it (note that this is the set ofgocritic
lints, not the whole set ofgolangci-lint
lints):