mirror of
https://github.com/golang/go
synced 2024-11-24 10:30:10 -07:00
cmd/vet: don't treat fields like variables in rangeloop check
Fixes #13236 Change-Id: If902ac66718e0a0790fab9835921ce4ef980965b Reviewed-on: https://go-review.googlesource.com/21183 Run-TryBot: Rob Pike <r@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
parent
d170d3edd7
commit
4ffa5eb876
@ -62,6 +62,10 @@ func checkRangeLoop(f *File, node ast.Node) {
|
||||
if !ok || id.Obj == nil {
|
||||
return true
|
||||
}
|
||||
if f.pkg.types[id].Type == nil {
|
||||
// Not referring to a variable
|
||||
return true
|
||||
}
|
||||
if key != nil && id.Obj == key.Obj || val != nil && id.Obj == val.Obj {
|
||||
f.Bad(id.Pos(), "range variable", id.Name, "captured by func literal")
|
||||
}
|
||||
|
9
src/cmd/vet/testdata/rangeloop.go
vendored
9
src/cmd/vet/testdata/rangeloop.go
vendored
@ -56,4 +56,13 @@ func RangeLoopTests() {
|
||||
_ = f // ERROR "range variable f captured by func literal"
|
||||
}()
|
||||
}
|
||||
type T struct {
|
||||
v int
|
||||
}
|
||||
for _, v := range s {
|
||||
go func() {
|
||||
_ = T{v: 1}
|
||||
_ = []int{v: 1} // ERROR "range variable v captured by func literal"
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user