1
0
mirror of https://github.com/golang/go synced 2024-10-01 01:38:33 -06:00
go/cmd/vet/testdata/rangeloop.go
Rob Pike 671b9204b8 go.tools/cmd/vet: change message for range check
s/enclosed by function/captured by func literal/
Users complained. They often do.

LGTM=josharian, adg
R=golang-codereviews, josharian, nightlyone, minux, adg
CC=golang-codereviews
https://golang.org/cl/132080043
2014-08-24 17:26:18 -07:00

60 lines
1.4 KiB
Go

// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This file contains tests for the rangeloop checker.
package testdata
func RangeLoopTests() {
var s []int
for i, v := range s {
go func() {
println(i) // ERROR "range variable i captured by func literal"
println(v) // ERROR "range variable v captured by func literal"
}()
}
for i, v := range s {
defer func() {
println(i) // ERROR "range variable i captured by func literal"
println(v) // ERROR "range variable v captured by func literal"
}()
}
for i := range s {
go func() {
println(i) // ERROR "range variable i captured by func literal"
}()
}
for _, v := range s {
go func() {
println(v) // ERROR "range variable v captured by func literal"
}()
}
for i, v := range s {
go func() {
println(i, v)
}()
println("unfortunately, we don't catch the error above because of this statement")
}
for i, v := range s {
go func(i, v int) {
println(i, v)
}(i, v)
}
for i, v := range s {
i, v := i, v
go func() {
println(i, v)
}()
}
// If the key of the range statement is not an identifier
// the code should not panic (it used to).
var x [2]int
var f int
for x[0], f = range s {
go func() {
_ = f // ERROR "range variable f captured by func literal"
}()
}
}