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