Skip to content

cmd/vet: warn about capturing loop iterator variables #16520

Closed
@wolf0403

Description

@wolf0403

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)?
    go version go1.6 darwin/amd64
  2. What operating system and processor architecture are you using (go env)?
    GOARCH="amd64"
    GOBIN=""
    GOEXE=""
    GOHOSTARCH="amd64"
    GOHOSTOS="darwin"
    GOOS="darwin"
    GOPATH="/Users/ryangao/go"
    GORACE=""
    GOROOT="/Users/ryangao/homebrew/Cellar/go/1.6/libexec"
    GOTOOLDIR="/Users/ryangao/homebrew/Cellar/go/1.6/libexec/pkg/tool/darwin_amd64"
    GO15VENDOREXPERIMENT="1"
    CC="clang"
    GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
    CXX="clang++"
    CGO_ENABLED="1"
  3. What did you do?
    Run "go vet" on code from https://play.golang.org/p/mUiuNfZDHT
  4. What did you expect to see?
    Expect "go vet" warns about error mentioned in
    https://golang.org/doc/faq#closures_and_goroutines
  5. What did you see instead?
    No warning issued.

Activity

added this to the Go1.8Maybe milestone on Aug 1, 2016
added
NeedsFixThe path to resolution is known, but the work has not been done.
on Oct 10, 2016
rsc

rsc commented on Oct 20, 2016

@rsc
Contributor

This does seem like it would catch real problems if done well.
/cc @adonovan

modified the milestones: Go1.9, Go1.8Maybe on Oct 20, 2016
rsc

rsc commented on Oct 20, 2016

@rsc
Contributor

Actually, note that those captures - at least when they are definite bugs - should usually show up in the race detector. It's not clear vet can contribute more than the race detector does without significant false positives.

alandonovan

alandonovan commented on Oct 20, 2016

@alandonovan
Contributor

Although this bug pattern most often manifests with go statements (leading to data races), sequential (non-racy) instances are possible too, such as with defer or the simple example presented in this Issue.

vet already checks for patterns of the form:

for k, v := range seq {
    go/defer func() {
        ... k, v ...
    }()
}

The example presented in this Issue is more challenging to analyze because it requires proving that the function is not called within the loop, or at least failing to prove that it is called within the loop. Once the anonymous function has been stored in a data structure or passed to another function, vet can no longer precisely determine when it might be called.

In other words, I think the current vet check is probably as good as we can do without interprocedural analysis.

modified the milestones: Go1.10, Go1.9 on Jun 19, 2017

53 remaining items

gopherbot

gopherbot commented on Dec 5, 2022

@gopherbot
Contributor

Change https://go.dev/cl/455195 mentions this issue: go/analysis/passes/loopclosure: allow certain well-understood statements to trail problematic go and defer statements

added
AnalysisIssues related to static analysis (vet, x/tools/go/analysis)
on Apr 23, 2023
dmitris

dmitris commented on Feb 29, 2024

@dmitris
Contributor

Is this no longer relevant with go1.22 / should be closed? @adonovan

The long-standing “for” loop gotcha with accidental sharing of loop variables between iterations is now resolved.

timothy-king

timothy-king commented on Feb 29, 2024

@timothy-king
Contributor

Yep. It is fine to close this with the 1.22 lifetime rules.

zigo101

zigo101 commented on Feb 7, 2025

@zigo101
ianlancetaylor

ianlancetaylor commented on Feb 8, 2025

@ianlancetaylor
zigo101

zigo101 commented on Feb 8, 2025

@zigo101
zigo101

zigo101 commented on Feb 11, 2025

@zigo101
locked as resolved and limited conversation to collaborators on Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    AnalysisIssues related to static analysis (vet, x/tools/go/analysis)NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @dmitris@dominikh@rsc@quentinmit@glenjamin

        Issue actions

          cmd/vet: warn about capturing loop iterator variables · Issue #16520 · golang/go