Debian Codegraph @

go/analysis/passes/modernize/testdata/src/waitgroupgo/waitgroup.go

raw
1 package waitgroup
2
3 import (
4 "fmt"
5 "sync"
6 )
7
8 // supported case for pattern 1.
9 func _() {
10 var wg sync.WaitGroup
11 wg.Add(1)
12 go func() { // want "Goroutine creation can be simplified using WaitGroup.Go"
13 defer wg.Done()
14 fmt.Println()
15 }()
16
17 wg.Add(1)
18 go func() { // want "Goroutine creation can be simplified using WaitGroup.Go"
19 defer wg.Done()
20 }()
21
22 for range 10 {
23 wg.Add(1)
24 go func() { // want "Goroutine creation can be simplified using WaitGroup.Go"
25 defer wg.Done()
26 fmt.Println()
27 }()
28 }
29 }
30
31 // supported case for pattern 2.
32 func _() {
33 var wg sync.WaitGroup
34 wg.Add(1)
35 go func() { // want "Goroutine creation can be simplified using WaitGroup.Go"
36 fmt.Println()
37 wg.Done()
38 }()
39
40 wg.Add(1)
41 go func() { // want "Goroutine creation can be simplified using WaitGroup.Go"
42 wg.Done()
43 }()
44
45 for range 10 {
46 wg.Add(1)
47 go func() { // want "Goroutine creation can be simplified using WaitGroup.Go"
48 fmt.Println()
49 wg.Done()
50 }()
51 }
52 }
53
54 // this function puts some wrong usages but waitgroupgo modernizer will still offer fixes.
55 func _() {
56 var wg sync.WaitGroup
57 wg.Add(1)
58 go func() { // want "Goroutine creation can be simplified using WaitGroup.Go"
59 defer wg.Done()
60 defer wg.Done()
61 fmt.Println()
62 }()
63
64 wg.Add(1)
65 go func() { // want "Goroutine creation can be simplified using WaitGroup.Go"
66 defer wg.Done()
67 fmt.Println()
68 wg.Done()
69 }()
70
71 wg.Add(1)
72 go func() { // want "Goroutine creation can be simplified using WaitGroup.Go"
73 fmt.Println()
74 wg.Done()
75 wg.Done()
76 }()
77 }
78
79 // this function puts the unsupported cases of pattern 1.
80 func _() {
81 var wg sync.WaitGroup
82 wg.Add(1)
83 go func() {}()
84
85 wg.Add(1)
86 go func(i int) {
87 defer wg.Done()
88 fmt.Println(i)
89 }(1)
90
91 wg.Add(1)
92 go func() {
93 fmt.Println()
94 defer wg.Done()
95 }()
96
97 wg.Add(1)
98 go func() { // noop: no wg.Done call inside function body.
99 fmt.Println()
100 }()
101
102 go func() { // noop: no Add call before this go stmt.
103 defer wg.Done()
104 fmt.Println()
105 }()
106
107 wg.Add(2) // noop: only support Add(1).
108 go func() {
109 defer wg.Done()
110 }()
111
112 var wg1 sync.WaitGroup
113 wg1.Add(1) // noop: Add and Done should be the same object.
114 go func() {
115 defer wg.Done()
116 fmt.Println()
117 }()
118
119 wg.Add(1) // noop: Add and Done should be the same object.
120 go func() {
121 defer wg1.Done()
122 fmt.Println()
123 }()
124
125 wg.Add(1) // noop: function literal has return values, wg.Go requires func().
126 go func() int {
127 defer wg.Done()
128 return 0
129 }()
130 }
131
132 // this function puts the unsupported cases of pattern 2.
133 func _() {
134 var wg sync.WaitGroup
135 wg.Add(1)
136 go func() {
137 wg.Done()
138 fmt.Println()
139 }()
140
141 go func() { // noop: no Add call before this go stmt.
142 fmt.Println()
143 wg.Done()
144 }()
145
146 var wg1 sync.WaitGroup
147 wg1.Add(1) // noop: Add and Done should be the same object.
148 go func() {
149 fmt.Println()
150 wg.Done()
151 }()
152
153 wg.Add(1) // noop: Add and Done should be the same object.
154 go func() {
155 fmt.Println()
156 wg1.Done()
157 }()
158
159 wg.Add(1) // noop: function literal has return values, wg.Go requires func().
160 go func() int {
161 fmt.Println()
162 wg.Done()
163 return 0
164 }()
165 }
166
167 type Server struct {
168 wg sync.WaitGroup
169 }
170
171 type ServerContainer struct {
172 serv Server
173 }
174
175 func _() {
176 var s Server
177 s.wg.Add(1)
178 go func() { // want "Goroutine creation can be simplified using WaitGroup.Go"
179 print()
180 s.wg.Done()
181 }()
182
183 var sc ServerContainer
184 sc.serv.wg.Add(1)
185 go func() { // want "Goroutine creation can be simplified using WaitGroup.Go"
186 print()
187 sc.serv.wg.Done()
188 }()
189
190 var wg sync.WaitGroup
191 arr := [1]*sync.WaitGroup{&wg}
192 arr[0].Add(1)
193 go func() { // want "Goroutine creation can be simplified using WaitGroup.Go"
194 print()
195 arr[0].Done()
196 }()
197 }