1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // This file contains tests for the loopclosure checker.
9 import "golang.org/x/sync/errgroup"
15 println(i) // want "loop variable i captured by func literal"
16 println(v) // want "loop variable v captured by func literal"
21 println(i) // want "loop variable i captured by func literal"
22 println(v) // want "loop variable v captured by func literal"
27 println(i) // want "loop variable i captured by func literal"
32 println(v) // want "loop variable v captured by func literal"
39 println("unfortunately, we don't catch the error above because of this statement")
52 // If the key of the range statement is not an identifier
53 // the code should not panic (it used to).
56 for x[0], f = range s {
58 _ = f // want "loop variable f captured by func literal"
67 _ = map[int]int{v: 1} // want "loop variable v captured by func literal"
72 for i := 0; i < 10; i++ {
74 print(i) // want "loop variable i captured by func literal"
77 for i, j := 0, 1; i < 100; i, j = j, i+j {
79 print(j) // want "loop variable j captured by func literal"
87 for p := head; p != nil; p = p.cdr {
89 print(p.car) // want "loop variable p captured by func literal"
94 // Group is used to test that loopclosure does not match on any type named "Group".
95 // The checker only matches on methods "(*...errgroup.Group).Go".
98 func (g *Group) Go(func() error) {}
102 // errgroup.Group.Go() invokes Go routines
103 g := new(errgroup.Group)
104 for i, v := range s {
106 print(i) // want "loop variable i captured by func literal"
107 print(v) // want "loop variable v captured by func literal"
111 // Do not match other Group.Go cases
113 for i, v := range s {