Commit 4ffa5eb8 authored by Dominik Honnef's avatar Dominik Honnef Committed by Rob Pike

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: 's avatarRob Pike <r@golang.org>
parent d170d3ed
......@@ -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")
}
......
......@@ -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"
}()
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment