1
0
mirror of https://github.com/golang/go synced 2024-11-11 23:10:23 -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:
Dominik Honnef 2016-03-26 14:23:56 +01:00 committed by Rob Pike
parent d170d3edd7
commit 4ffa5eb876
2 changed files with 13 additions and 0 deletions

View File

@ -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")
}

View File

@ -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"
}()
}
}