| 1 |
This test verifies various behaviors of function extraction involving free control statements. |
| 2 |
|
| 3 |
-- go.mod -- |
| 4 |
module mod.test/extract |
| 5 |
|
| 6 |
go 1.18 |
| 7 |
|
| 8 |
-- freecontrol.go -- |
| 9 |
package extract |
| 10 |
|
| 11 |
//@codeaction(ifCondContinue, "refactor.extract.function", edit=freeControl1) |
| 12 |
//@codeaction(ifCondGotoLabel, "refactor.extract.function", edit=freeControl2) |
| 13 |
//@codeaction(ifCondGotoLabelWithLabel, "refactor.extract.function", edit=freeControl3) |
| 14 |
//@codeaction(multipleCtrl, "refactor.extract.function", edit=freeControl4) |
| 15 |
//@codeaction(multipleCtrlNotAllSelected, "refactor.extract.function", edit=freeControl5) |
| 16 |
//@codeaction(ctrlVarExists, "refactor.extract.function", edit=freeControl6) |
| 17 |
//@codeaction(twoReturns, "refactor.extract.function", edit=freeControl7) |
| 18 |
//@codeaction(forWithLabel, "refactor.extract.function", edit=freeControl8) |
| 19 |
|
| 20 |
func FuncContinue(cond bool) { |
| 21 |
for range "abc" { |
| 22 |
if cond { //@ loc(ifCondContinue, re`(?s)if.*println.0.`) |
| 23 |
continue |
| 24 |
} |
| 25 |
println(0) |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
func FuncGoTo(cond bool) { |
| 30 |
for range "abc" { |
| 31 |
if cond { //@ loc(ifCondGotoLabel, re`(?s)if.*println.1.`), loc(ifCondGotoLabelWithLabel, re`(?s)if.*goto.label1....`) |
| 32 |
goto label1 |
| 33 |
} |
| 34 |
label1: |
| 35 |
println(1) |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
func FuncMultipleCtrl(x int) { |
| 40 |
for range "abc" { |
| 41 |
if x < 10 { //@ loc(multipleCtrl, re`(?s)if.x.*return...next1....`), loc(multipleCtrlNotAllSelected, re`(?s)if.x.*break....`) |
| 42 |
continue |
| 43 |
} |
| 44 |
if x > 2 { |
| 45 |
break |
| 46 |
} |
| 47 |
if x == 1 { |
| 48 |
return //next1 |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
func FuncCtrlVarExists(x int) { |
| 54 |
ctrl := "abc" |
| 55 |
for range ctrl { |
| 56 |
if x < 10 { //@ loc(ctrlVarExists, re`(?s)if.x.*continue...next2....`) |
| 57 |
continue //next2 |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
func FuncTwoReturns(x int) int { |
| 63 |
outer: |
| 64 |
for range "abc" { |
| 65 |
if x < 10 { //@ loc(twoReturns, re`(?s)if.x.*return.1....`) |
| 66 |
return 0 |
| 67 |
} |
| 68 |
test := x - 4 |
| 69 |
if test > 2 { |
| 70 |
continue |
| 71 |
} |
| 72 |
if test == 10 { |
| 73 |
return 1 |
| 74 |
} |
| 75 |
|
| 76 |
for range "def" { //@ loc(forWithLabel, re`(?s)for.*outer.........`) |
| 77 |
if x < 2 { |
| 78 |
continue |
| 79 |
} |
| 80 |
if x > 10 { |
| 81 |
continue outer |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
return 0 |
| 86 |
} |
| 87 |
-- @freeControl1/freecontrol.go -- |
| 88 |
@@ -14 +14,3 @@ |
| 89 |
- if cond { //@ loc(ifCondContinue, re`(?s)if.*println.0.`) |
| 90 |
+ ctrl := newFunction(cond) |
| 91 |
+ switch ctrl { |
| 92 |
+ case 1: |
| 93 |
@@ -17 +19 @@ |
| 94 |
- println(0) |
| 95 |
@@ -21 +22,8 @@ |
| 96 |
+func newFunction(cond bool) int { |
| 97 |
+ if cond { //@ loc(ifCondContinue, re`(?s)if.*println.0.`) |
| 98 |
+ return 1 |
| 99 |
+ } |
| 100 |
+ println(0) |
| 101 |
+ return 0 |
| 102 |
+} |
| 103 |
+ |
| 104 |
-- @freeControl2/freecontrol.go -- |
| 105 |
@@ -23,5 +23 @@ |
| 106 |
- if cond { //@ loc(ifCondGotoLabel, re`(?s)if.*println.1.`), loc(ifCondGotoLabelWithLabel, re`(?s)if.*goto.label1....`) |
| 107 |
- goto label1 |
| 108 |
- } |
| 109 |
- label1: |
| 110 |
- println(1) |
| 111 |
+ newFunction(cond) |
| 112 |
@@ -31 +27,8 @@ |
| 113 |
+func newFunction(cond bool) { |
| 114 |
+ if cond { //@ loc(ifCondGotoLabel, re`(?s)if.*println.1.`), loc(ifCondGotoLabelWithLabel, re`(?s)if.*goto.label1....`) |
| 115 |
+ goto label1 |
| 116 |
+ } |
| 117 |
+label1: |
| 118 |
+ println(1) |
| 119 |
+} |
| 120 |
+ |
| 121 |
-- @freeControl3/freecontrol.go -- |
| 122 |
@@ -23 +23,3 @@ |
| 123 |
- if cond { //@ loc(ifCondGotoLabel, re`(?s)if.*println.1.`), loc(ifCondGotoLabelWithLabel, re`(?s)if.*goto.label1....`) |
| 124 |
+ ctrl := newFunction(cond) |
| 125 |
+ switch ctrl { |
| 126 |
+ case 1: |
| 127 |
@@ -31 +33,7 @@ |
| 128 |
+func newFunction(cond bool) int { |
| 129 |
+ if cond { //@ loc(ifCondGotoLabel, re`(?s)if.*println.1.`), loc(ifCondGotoLabelWithLabel, re`(?s)if.*goto.label1....`) |
| 130 |
+ return 1 |
| 131 |
+ } |
| 132 |
+ return 0 |
| 133 |
+} |
| 134 |
+ |
| 135 |
-- @freeControl4/freecontrol.go -- |
| 136 |
@@ -33,2 +33,3 @@ |
| 137 |
- if x < 10 { //@ loc(multipleCtrl, re`(?s)if.x.*return...next1....`), loc(multipleCtrlNotAllSelected, re`(?s)if.x.*break....`) |
| 138 |
- continue |
| 139 |
+ shouldReturn, ctrl := newFunction(x) |
| 140 |
+ if shouldReturn { |
| 141 |
+ return |
| 142 |
@@ -36 +37,4 @@ |
| 143 |
- if x > 2 { |
| 144 |
+ switch ctrl { |
| 145 |
+ case 1: |
| 146 |
+ continue |
| 147 |
+ case 2: |
| 148 |
@@ -39,3 +43 @@ |
| 149 |
- if x == 1 { |
| 150 |
- return //next1 |
| 151 |
- } |
| 152 |
@@ -45 +46,14 @@ |
| 153 |
+func newFunction(x int) (bool, int) { |
| 154 |
+ if x < 10 { //@ loc(multipleCtrl, re`(?s)if.x.*return...next1....`), loc(multipleCtrlNotAllSelected, re`(?s)if.x.*break....`) |
| 155 |
+ return false, 1 |
| 156 |
+ } |
| 157 |
+ if x > 2 { |
| 158 |
+ return false, 2 |
| 159 |
+ } |
| 160 |
+ if x == 1 { |
| 161 |
+ return true, //next1 |
| 162 |
+ 0 |
| 163 |
+ } |
| 164 |
+ return false, 0 |
| 165 |
+} |
| 166 |
+ |
| 167 |
-- @freeControl5/freecontrol.go -- |
| 168 |
@@ -33 +33,3 @@ |
| 169 |
- if x < 10 { //@ loc(multipleCtrl, re`(?s)if.x.*return...next1....`), loc(multipleCtrlNotAllSelected, re`(?s)if.x.*break....`) |
| 170 |
+ ctrl := newFunction(x) |
| 171 |
+ switch ctrl { |
| 172 |
+ case 1: |
| 173 |
@@ -35,2 +37 @@ |
| 174 |
- } |
| 175 |
- if x > 2 { |
| 176 |
+ case 2: |
| 177 |
@@ -45 +46,10 @@ |
| 178 |
+func newFunction(x int) int { |
| 179 |
+ if x < 10 { //@ loc(multipleCtrl, re`(?s)if.x.*return...next1....`), loc(multipleCtrlNotAllSelected, re`(?s)if.x.*break....`) |
| 180 |
+ return 1 |
| 181 |
+ } |
| 182 |
+ if x > 2 { |
| 183 |
+ return 2 |
| 184 |
+ } |
| 185 |
+ return 0 |
| 186 |
+} |
| 187 |
+ |
| 188 |
-- @freeControl6/freecontrol.go -- |
| 189 |
@@ -48,2 +48,4 @@ |
| 190 |
- if x < 10 { //@ loc(ctrlVarExists, re`(?s)if.x.*continue...next2....`) |
| 191 |
- continue //next2 |
| 192 |
+ ctrl1 := newFunction(x) |
| 193 |
+ switch ctrl1 { |
| 194 |
+ case 1: |
| 195 |
+ continue |
| 196 |
@@ -54 +56,7 @@ |
| 197 |
+func newFunction(x int) int { |
| 198 |
+ if x < 10 { //@ loc(ctrlVarExists, re`(?s)if.x.*continue...next2....`) |
| 199 |
+ return 1 //next2 |
| 200 |
+ } |
| 201 |
+ return 0 |
| 202 |
+} |
| 203 |
+ |
| 204 |
-- @freeControl7/freecontrol.go -- |
| 205 |
@@ -57,2 +57,3 @@ |
| 206 |
- if x < 10 { //@ loc(twoReturns, re`(?s)if.x.*return.1....`) |
| 207 |
- return 0 |
| 208 |
+ i, shouldReturn, ctrl := newFunction(x) |
| 209 |
+ if shouldReturn { |
| 210 |
+ return i |
| 211 |
@@ -60,2 +61,2 @@ |
| 212 |
- test := x - 4 |
| 213 |
- if test > 2 { |
| 214 |
+ switch ctrl { |
| 215 |
+ case 1: |
| 216 |
@@ -64,3 +65 @@ |
| 217 |
- if test == 10 { |
| 218 |
- return 1 |
| 219 |
- } |
| 220 |
@@ -79 +77,14 @@ |
| 221 |
+ |
| 222 |
+func newFunction(x int) (int, bool, int) { |
| 223 |
+ if x < 10 { //@ loc(twoReturns, re`(?s)if.x.*return.1....`) |
| 224 |
+ return 0, true, 0 |
| 225 |
+ } |
| 226 |
+ test := x - 4 |
| 227 |
+ if test > 2 { |
| 228 |
+ return 0, false, 1 |
| 229 |
+ } |
| 230 |
+ if test == 10 { |
| 231 |
+ return 1, true, 0 |
| 232 |
+ } |
| 233 |
+ return 0, false, 0 |
| 234 |
+} |
| 235 |
-- @freeControl8/freecontrol.go -- |
| 236 |
@@ -68,5 +68,3 @@ |
| 237 |
- for range "def" { //@ loc(forWithLabel, re`(?s)for.*outer.........`) |
| 238 |
- if x < 2 { |
| 239 |
- continue |
| 240 |
- } |
| 241 |
- if x > 10 { |
| 242 |
+ ctrl := newFunction(x) |
| 243 |
+ switch ctrl { |
| 244 |
+ case 1: |
| 245 |
@@ -74 +72 @@ |
| 246 |
- } |
| 247 |
@@ -79 +76,12 @@ |
| 248 |
+ |
| 249 |
+func newFunction(x int) int { |
| 250 |
+ for range "def" { //@ loc(forWithLabel, re`(?s)for.*outer.........`) |
| 251 |
+ if x < 2 { |
| 252 |
+ continue |
| 253 |
+ } |
| 254 |
+ if x > 10 { |
| 255 |
+ return 1 |
| 256 |
+ } |
| 257 |
+ } |
| 258 |
+ return 0 |
| 259 |
+} |