Skip to content

Commit 0543cb4

Browse files
committed
add test for the cases
1 parent cb4d387 commit 0543cb4

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

exportloopref_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,13 @@ func TestDepPointer(t *testing.T) {
4646
testdata := analysistest.TestData()
4747
analysistest.Run(t, testdata, exportloopref.Analyzer, "deeppointer")
4848
}
49+
50+
func TestBreak(t *testing.T) {
51+
testdata := analysistest.TestData()
52+
analysistest.Run(t, testdata, exportloopref.Analyzer, "break")
53+
}
54+
55+
func TestReturn(t *testing.T) {
56+
testdata := analysistest.TestData()
57+
analysistest.Run(t, testdata, exportloopref.Analyzer, "return")
58+
}

testdata/src/break/break.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package main
2+
3+
func main() {
4+
var result *int
5+
haystack := []int{1, 2}
6+
for _, hay := range haystack {
7+
result = &hay // Broken just below, the loop does NOT set unexpected value to result
8+
break
9+
}
10+
}

testdata/src/return/return.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
func main() {
4+
result := sub()
5+
println(*result)
6+
}
7+
8+
func sub() (result *int) {
9+
haystack := []int{1, 2}
10+
for _, hay := range haystack {
11+
result = &hay // Returns just below, the loop does NOT set unexpected value to result
12+
return
13+
}
14+
return
15+
}

0 commit comments

Comments
 (0)