mirror of
https://github.com/golang/go
synced 2024-11-18 07:24:45 -07:00
01f8cd246d
They will be deleted from their current homes once this has landed. Changes made to import paths to make the code compile, and to find errchk in the right place in cmd/vet's Makefile. TODO in a later CL: tidy up vet. R=golang-dev, gri CC=golang-dev https://golang.org/cl/9495043
62 lines
1.4 KiB
Go
62 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.
|
|
|
|
// +build vet_test
|
|
|
|
package main
|
|
|
|
func RangeLoopTests() {
|
|
var s []int
|
|
for i, v := range s {
|
|
go func() {
|
|
println(i) // ERROR "range variable i enclosed by function"
|
|
println(v) // ERROR "range variable v enclosed by function"
|
|
}()
|
|
}
|
|
for i, v := range s {
|
|
defer func() {
|
|
println(i) // ERROR "range variable i enclosed by function"
|
|
println(v) // ERROR "range variable v enclosed by function"
|
|
}()
|
|
}
|
|
for i := range s {
|
|
go func() {
|
|
println(i) // ERROR "range variable i enclosed by function"
|
|
}()
|
|
}
|
|
for _, v := range s {
|
|
go func() {
|
|
println(v) // ERROR "range variable v enclosed by function"
|
|
}()
|
|
}
|
|
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 enclosed by function"
|
|
}()
|
|
}
|
|
}
|