From 0543cb4e4a67c0c3be7c010fdaeb61e5ee238d3a Mon Sep 17 00:00:00 2001 From: Kyoichiro Yamada Date: Sat, 31 Jul 2021 00:02:17 +0900 Subject: [PATCH] add test for the cases --- exportloopref_test.go | 10 ++++++++++ testdata/src/break/break.go | 10 ++++++++++ testdata/src/return/return.go | 15 +++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 testdata/src/break/break.go create mode 100644 testdata/src/return/return.go diff --git a/exportloopref_test.go b/exportloopref_test.go index 444a80c..48a1724 100644 --- a/exportloopref_test.go +++ b/exportloopref_test.go @@ -46,3 +46,13 @@ func TestDepPointer(t *testing.T) { testdata := analysistest.TestData() analysistest.Run(t, testdata, exportloopref.Analyzer, "deeppointer") } + +func TestBreak(t *testing.T) { + testdata := analysistest.TestData() + analysistest.Run(t, testdata, exportloopref.Analyzer, "break") +} + +func TestReturn(t *testing.T) { + testdata := analysistest.TestData() + analysistest.Run(t, testdata, exportloopref.Analyzer, "return") +} diff --git a/testdata/src/break/break.go b/testdata/src/break/break.go new file mode 100644 index 0000000..f4dc0d1 --- /dev/null +++ b/testdata/src/break/break.go @@ -0,0 +1,10 @@ +package main + +func main() { + var result *int + haystack := []int{1, 2} + for _, hay := range haystack { + result = &hay // Broken just below, the loop does NOT set unexpected value to result + break + } +} diff --git a/testdata/src/return/return.go b/testdata/src/return/return.go new file mode 100644 index 0000000..acf9a90 --- /dev/null +++ b/testdata/src/return/return.go @@ -0,0 +1,15 @@ +package main + +func main() { + result := sub() + println(*result) +} + +func sub() (result *int) { + haystack := []int{1, 2} + for _, hay := range haystack { + result = &hay // Returns just below, the loop does NOT set unexpected value to result + return + } + return +}