loopvarcapture: do not flag defer within local closure#83418
Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom Jun 30, 2022
Merged
loopvarcapture: do not flag defer within local closure#83418craig[bot] merged 1 commit intocockroachdb:masterfrom
defer within local closure#83418craig[bot] merged 1 commit intocockroachdb:masterfrom
Conversation
Previously, handling of `defer` statements in the `loopvarcapture`
linter was naive: whenever a `defer` statement in the body of a loop
referenced a loop variable, the linter would flag it as an invalid
reference. However, that can be overly restrictive, as a relatively
common idiom is to create literal functions and immediately call them
so as to take advantage of `defer` semantics, as in the example below:
```go
for _, n := range numbers {
// ...
func() {
// ...
defer func() { doSomewithing(n) }() // always safe
// ...
}()
}
```
The above reference is valid because it is guaranteed to be called
with the correct value for the loop variable.
A similar scenario occurs when a closure is assigned to a local
variable for use within the loop:
```go
for _, n := range numbers {
// ...
helper := func() {
// ...
defer func() { doSomething(n) }()
// ...
}
// ...
helper() // always safe
}
```
In the snippet above, calling the `helper` function is also always
safe because the `defer` statement is scoped to the closure containing
it. However, it is still *not* safe to call the helper function within
a Go routine.
This commit updates the `loopvarcapture` linter to recognize when a
`defer` statement is safe because it is contained in a local
closure. The two cases illustrated above will no longer be flagged,
allowing for that idiom to be used freely.
Release note: None.
Member
dhartunian
reviewed
Jun 28, 2022
Collaborator
dhartunian
left a comment
There was a problem hiding this comment.
I can confirm that this fixes the linter issue with #79663. Thanks!
Reviewable status:
complete! 0 of 0 LGTMs obtained (waiting on @srosenberg)
dhartunian
approved these changes
Jun 30, 2022
Collaborator
dhartunian
left a comment
There was a problem hiding this comment.
friendly ping! can we merge this so I can unblock my PR? the test case looks straightforward to me.
srosenberg
approved these changes
Jun 30, 2022
Member
srosenberg
left a comment
There was a problem hiding this comment.
Reviewed 2 of 2 files at r1, all commit messages.
Reviewable status:complete! 1 of 0 LGTMs obtained (waiting on @renatolabs)
Author
|
bors r=srosenberg,dhartunian Thanks! |
Contributor
|
Build succeeded: |
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.
Previously, handling of
deferstatements in theloopvarcapturelinter was naive: whenever a
deferstatement in the body of a loopreferenced a loop variable, the linter would flag it as an invalid
reference. However, that can be overly restrictive, as a relatively
common idiom is to create literal functions and immediately call them
so as to take advantage of
defersemantics, as in the example below:The above reference is valid because it is guaranteed to be called
with the correct value for the loop variable.
A similar scenario occurs when a closure is assigned to a local
variable for use within the loop:
In the snippet above, calling the
helperfunction is also alwayssafe because the
deferstatement is scoped to the closure containingit. However, it is still not safe to call the helper function within
a Go routine.
This commit updates the
loopvarcapturelinter to recognize when adeferstatement is safe because it is contained in a localclosure. The two cases illustrated above will no longer be flagged,
allowing for that idiom to be used freely.
Release note: None.