Skip to content

loopvarcapture: do not flag defer within local closure#83418

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
renatolabs:better-defer-loopvarcapture
Jun 30, 2022
Merged

loopvarcapture: do not flag defer within local closure#83418
craig[bot] merged 1 commit intocockroachdb:masterfrom
renatolabs:better-defer-loopvarcapture

Conversation

@renatolabs
Copy link
Copy Markdown

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:

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:

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.

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.
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@renatolabs renatolabs requested review from a team and srosenberg and removed request for a team June 27, 2022 15:26
Copy link
Copy Markdown
Collaborator

@dhartunian dhartunian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can confirm that this fixes the linter issue with #79663. Thanks!

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @srosenberg)

Copy link
Copy Markdown
Collaborator

@dhartunian dhartunian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

friendly ping! can we merge this so I can unblock my PR? the test case looks straightforward to me.

Copy link
Copy Markdown
Member

@srosenberg srosenberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 2 of 2 files at r1, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @renatolabs)

@renatolabs
Copy link
Copy Markdown
Author

bors r=srosenberg,dhartunian

Thanks!

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Jun 30, 2022

Build succeeded:

@craig craig bot merged commit 05ca68a into cockroachdb:master Jun 30, 2022
@renatolabs renatolabs deleted the better-defer-loopvarcapture branch June 30, 2022 19:29
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.

4 participants