Skip to content

Commit 532589e

Browse files
committed
false negatives
1 parent 5595d7c commit 532589e

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

README.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ ref: https://github.com/kyoh86/exportloopref/blob/master/testdata/fixed/fixed.go
113113
## Sensing policy
114114

115115
I want to make exportloopref as accurately as possible.
116-
So some cases of lints will be ignored.
116+
So some cases of lints will be false-negative.
117117

118118
e.g.
119119

@@ -127,6 +127,48 @@ for _, p := []int{10, 11, 12, 13} {
127127
If you want to report all of lints (with some false-positives),
128128
you should use [looppointer](https://github.com/kyoh86/looppointer).
129129

130+
### Known false negatives
131+
132+
Case 1: pass the pointer to function to export.
133+
134+
Case 2: pass the pointer to local variable, and export it.
135+
136+
```go
137+
package main
138+
139+
type List []*int
140+
141+
func (l *List) AppendP(p *int) {
142+
*l = append(*l, p)
143+
}
144+
145+
func main() {
146+
var slice []*int
147+
list := List{}
148+
149+
println("loop expect exporting 10, 11, 12, 13")
150+
for _, v := range []int{10, 11, 12, 13} {
151+
list.AppendP(&v) // Case 1: wanted "exporting a pointer for the loop variable v", but cannot be found
152+
153+
p := &v // p is the local variable
154+
slice = append(slice, p) // Case 2: wanted "exporting a pointer for the loop variable v", but cannot be found
155+
}
156+
157+
println(`slice expecting "10, 11, 12, 13" but "13, 13, 13, 13"`)
158+
for _, p := range slice {
159+
printp(p)
160+
}
161+
println(`array expecting "10, 11, 12, 13" but "13, 13, 13, 13"`)
162+
for _, p := range ([]*int)(list) {
163+
printp(p)
164+
}
165+
}
166+
167+
func printp(p *int) {
168+
println(*p)
169+
}
170+
```
171+
130172
## Install
131173

132174
go:

exportloopref.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package exportloopref
22

33
import (
4+
"fmt"
45
"go/ast"
56
"go/token"
67
"go/types"
@@ -44,7 +45,8 @@ func run(pass *analysis.Pass) (interface{}, error) {
4445
inspect.WithStack(nodeFilter, func(n ast.Node, push bool, stack []ast.Node) bool {
4546
id, digg := search.Check(n, stack)
4647
if id != nil {
47-
pass.ReportRangef(id, "exporting a pointer for the loop variable %s", id.Name)
48+
msg := fmt.Sprintf("exporting a pointer for the loop variable %s", id.Name)
49+
pass.Report(analysis.Diagnostic{Pos: id.Pos(), End: id.End(), Message: msg, Category: "exportloopref"})
4850
}
4951
return digg
5052
})

testdata/src/false/false.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
type List []*int
4+
5+
func (l *List) AppendP(p *int) {
6+
*l = append(*l, p)
7+
}
8+
9+
func main() {
10+
var slice []*int
11+
list := List{}
12+
13+
println("loop expect exporting 10, 11, 12, 13")
14+
for _, v := range []int{10, 11, 12, 13} {
15+
list.AppendP(&v) // wanted "exporting a pointer for the loop variable v", but cannot be found
16+
17+
p := &v // p is the local variable
18+
slice = append(slice, p) // wanted "exporting a pointer for the loop variable v", but cannot be found
19+
}
20+
21+
println(`slice expecting "10, 11, 12, 13" but "13, 13, 13, 13"`)
22+
for _, p := range slice {
23+
printp(p)
24+
}
25+
println(`array expecting "10, 11, 12, 13" but "13, 13, 13, 13"`)
26+
for _, p := range ([]*int)(list) {
27+
printp(p)
28+
}
29+
}
30+
31+
func printp(p *int) {
32+
println(*p)
33+
}

0 commit comments

Comments
 (0)