1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
# exportloopref
An analyzer that finds exporting pointers for loop variables.

Pin them all!
[](https://pkg.go.dev/kyoh86/exportloopref)
[](https://goreportcard.com/report/github.com/kyoh86/exportloopref)
[](https://codecov.io/gh/kyoh86/exportloopref)
[](https://github.com/kyoh86/exportloopref/releases)
## What's this?
Sample problem code from: https://github.com/kyoh86/exportloopref/blob/main/testdata/src/simple/simple.go
```go
package main
func main() {
var intArray [4]*int
var intSlice []*int
var intRef *int
var intStr struct{ x *int }
println("loop expecting 10, 11, 12, 13")
for i, p := range []int{10, 11, 12, 13} {
printp(&p) // not a diagnostic
intSlice = append(intSlice, &p) // want "exporting a pointer for the loop variable p"
intArray[i] = &p // want "exporting a pointer for the loop variable p"
if i%2 == 0 {
intRef = &p // want "exporting a pointer for the loop variable p"
intStr.x = &p // want "exporting a pointer for the loop variable p"
}
var vStr struct{ x *int }
var vArray [4]*int
var v *int
if i%2 == 0 {
v = &p // not a diagnostic (x is local variable)
vArray[1] = &p // not a diagnostic (x is local variable)
vStr.x = &p
}
_ = v
}
println(`slice expecting "10, 11, 12, 13" but "13, 13, 13, 13"`)
for _, p := range intSlice {
printp(p)
}
println(`array expecting "10, 11, 12, 13" but "13, 13, 13, 13"`)
for _, p := range intArray {
printp(p)
}
println(`captured value expecting "12" but "13"`)
printp(intRef)
}
func printp(p *int) {
println(*p)
}
```
In Go, the `p` variable in the above loops is actually a single variable.
So in many case (like the above), using it makes for us annoying bugs.
You can find them with `exportloopref`, and fix it.
```go
package main
func main() {
var intArray [4]*int
var intSlice []*int
var intRef *int
var intStr struct{ x *int }
println("loop expecting 10, 11, 12, 13")
for i, p := range []int{10, 11, 12, 13} {
p := p // FIX variable into the local variable
printp(&p)
intSlice = append(intSlice, &p)
intArray[i] = &p
if i%2 == 0 {
intRef = &p
intStr.x = &p
}
var vStr struct{ x *int }
var vArray [4]*int
var v *int
if i%2 == 0 {
v = &p
vArray[1] = &p
vStr.x = &p
}
_ = v
}
println(`slice expecting "10, 11, 12, 13"`)
for _, p := range intSlice {
printp(p)
}
println(`array expecting "10, 11, 12, 13"`)
for _, p := range intArray {
printp(p)
}
println(`captured value expecting "12"`)
printp(intRef)
}
func printp(p *int) {
println(*p)
}
```
ref: https://github.com/kyoh86/exportloopref/blob/main/testdata/src/fixed/fixed.go
## Sensing policy
I want to make exportloopref as accurately as possible.
So some cases of lints will be false-negative.
e.g.
```go
var s Foo
for _, p := range []int{10, 11, 12, 13} {
s.Bar(&p) // If s stores the pointer, it will be bug.
}
```
If you want to report all of lints (with some false-positives),
you should use [looppointer](https://github.com/kyoh86/looppointer).
### Known false negatives
Case 1: pass the pointer to function to export.
Case 2: pass the pointer to local variable, and export it.
```go
package main
type List []*int
func (l *List) AppendP(p *int) {
*l = append(*l, p)
}
func main() {
var slice []*int
list := List{}
println("loop expect exporting 10, 11, 12, 13")
for _, v := range []int{10, 11, 12, 13} {
list.AppendP(&v) // Case 1: wanted "exporting a pointer for the loop variable v", but cannot be found
p := &v // p is the local variable
slice = append(slice, p) // Case 2: wanted "exporting a pointer for the loop variable v", but cannot be found
}
println(`slice expecting "10, 11, 12, 13" but "13, 13, 13, 13"`)
for _, p := range slice {
printp(p)
}
println(`array expecting "10, 11, 12, 13" but "13, 13, 13, 13"`)
for _, p := range ([]*int)(list) {
printp(p)
}
}
func printp(p *int) {
println(*p)
}
```
## Install
go:
```console
$ go get github.com/kyoh86/exportloopref/cmd/exportloopref
```
[homebrew](https://brew.sh/):
```console
$ brew install kyoh86/tap/exportloopref
```
[gordon](https://github.com/kyoh86/gordon):
```console
$ gordon install kyoh86/exportloopref
```
## Usage
```
exportloopref [-flag] [package]
```
### Flags
| Flag | Description |
| --- | --- |
| -V | print version and exit |
| -all | no effect (deprecated) |
| -c int | display offending line with this many lines of context (default -1) |
| -cpuprofile string | write CPU profile to this file |
| -debug string | debug flags, any subset of "fpstv" |
| -fix | apply all suggested fixes |
| -flags | print analyzer flags in JSON |
| -json | emit JSON output |
| -memprofile string | write memory profile to this file |
| -source | no effect (deprecated) |
| -tags string | no effect (deprecated) |
| -trace string | write trace log to this file |
| -v | no effect (deprecated) |
# LICENSE
[](http://www.opensource.org/licenses/MIT)
This is distributed under the [MIT License](http://www.opensource.org/licenses/MIT).
|