| 1 |
package rangeint |
| 2 |
|
| 3 |
import ( |
| 4 |
"os" |
| 5 |
os1 "os" |
| 6 |
"rangeint/a" |
| 7 |
) |
| 8 |
|
| 9 |
func _(i int, s struct{ i int }, slice []int) { |
| 10 |
for i := 0; i < 10; i++ { // want "for loop can be modernized using range over int" |
| 11 |
println(i) |
| 12 |
} |
| 13 |
for j := int(0); j < 10; j++ { // want "for loop can be modernized using range over int" |
| 14 |
println(j) |
| 15 |
} |
| 16 |
for j := int8(0); j < 10; j++ { // want "for loop can be modernized using range over int" |
| 17 |
println(j) |
| 18 |
} |
| 19 |
for j := int16(0); j < 10; j++ { // want "for loop can be modernized using range over int" |
| 20 |
println(j) |
| 21 |
} |
| 22 |
for j := int32(0); j < 10; j++ { // want "for loop can be modernized using range over int" |
| 23 |
println(j) |
| 24 |
} |
| 25 |
for j := int64(0); j < 10; j++ { // want "for loop can be modernized using range over int" |
| 26 |
println(j) |
| 27 |
} |
| 28 |
for j := uint8(0); j < 10; j++ { // want "for loop can be modernized using range over int" |
| 29 |
println(j) |
| 30 |
} |
| 31 |
for j := uint16(0); j < 10; j++ { // want "for loop can be modernized using range over int" |
| 32 |
println(j) |
| 33 |
} |
| 34 |
for j := uint32(0); j < 10; j++ { // want "for loop can be modernized using range over int" |
| 35 |
println(j) |
| 36 |
} |
| 37 |
for j := uint64(0); j < 10; j++ { // want "for loop can be modernized using range over int" |
| 38 |
println(j) |
| 39 |
} |
| 40 |
for j := int8(0.); j < 10; j++ { // want "for loop can be modernized using range over int" |
| 41 |
println(j) |
| 42 |
} |
| 43 |
for j := int8(.0); j < 10; j++ { // want "for loop can be modernized using range over int" |
| 44 |
println(j) |
| 45 |
} |
| 46 |
for j := os.FileMode(0); j < 10; j++ { // want "for loop can be modernized using range over int" |
| 47 |
println(j) |
| 48 |
} |
| 49 |
|
| 50 |
{ |
| 51 |
var i int |
| 52 |
for i = 0; i < 10; i++ { // want "for loop can be modernized using range over int" |
| 53 |
} |
| 54 |
// NB: no uses of i after loop. |
| 55 |
} |
| 56 |
for i := 0; i < 10; i++ { // want "for loop can be modernized using range over int" |
| 57 |
// i unused within loop |
| 58 |
} |
| 59 |
for i := 0; i < len(slice); i++ { // want "for loop can be modernized using range over int" |
| 60 |
println(slice[i]) |
| 61 |
} |
| 62 |
for i := 0; i < len(""); i++ { // want "for loop can be modernized using range over int" |
| 63 |
// NB: not simplified to range "" |
| 64 |
} |
| 65 |
|
| 66 |
// nope |
| 67 |
for j := .0; j < 10; j++ { // nope: j is a float type |
| 68 |
println(j) |
| 69 |
} |
| 70 |
for j := float64(0); j < 10; j++ { // nope: j is a float type |
| 71 |
println(j) |
| 72 |
} |
| 73 |
for i := 0; i < 10; { // nope: missing increment |
| 74 |
} |
| 75 |
for i := 0; i < 10; i-- { // nope: negative increment |
| 76 |
} |
| 77 |
for i := 0; ; i++ { // nope: missing comparison |
| 78 |
} |
| 79 |
for i := 0; i <= 10; i++ { // nope: wrong comparison |
| 80 |
} |
| 81 |
for ; i < 10; i++ { // nope: missing init |
| 82 |
} |
| 83 |
for s.i = 0; s.i < 10; s.i++ { // nope: not an ident |
| 84 |
} |
| 85 |
for i := 0; i < 10; i++ { // nope: takes address of i |
| 86 |
println(&i) |
| 87 |
} |
| 88 |
for i := 0; i < 10; i++ { // nope: increments i |
| 89 |
i++ |
| 90 |
} |
| 91 |
for i := 0; i < 10; i++ { // nope: assigns i |
| 92 |
i = 8 |
| 93 |
} |
| 94 |
|
| 95 |
for i := 0; i < 10; i += 1 { // want "for loop can be modernized using range over int" |
| 96 |
println(i) |
| 97 |
} |
| 98 |
for i := 0; i < 10; s.i += 1 { // nope: modifies another variable |
| 99 |
println(i) |
| 100 |
} |
| 101 |
for i := 0; i < 10; i -= 1 { // nope: decrements i |
| 102 |
println(i) |
| 103 |
} |
| 104 |
for i := 0; i < 10; i += 2 { // nope: range only increments by 1 |
| 105 |
println(i) |
| 106 |
} |
| 107 |
|
| 108 |
// The limit expression must be loop invariant; |
| 109 |
// see https://github.com/golang/go/issues/72917 |
| 110 |
for i := 0; i < f(); i++ { // nope |
| 111 |
} |
| 112 |
{ |
| 113 |
var s struct{ limit int } |
| 114 |
for i := 0; i < s.limit; i++ { // nope: limit is not a const or local var |
| 115 |
} |
| 116 |
} |
| 117 |
{ |
| 118 |
const k = 10 |
| 119 |
for i := 0; i < k; i++ { // want "for loop can be modernized using range over int" |
| 120 |
} |
| 121 |
} |
| 122 |
{ |
| 123 |
var limit = 10 |
| 124 |
for i := 0; i < limit; i++ { // want "for loop can be modernized using range over int" |
| 125 |
} |
| 126 |
} |
| 127 |
{ |
| 128 |
var limit = 10 |
| 129 |
for i := 0; i < limit; i++ { // nope: limit is address-taken |
| 130 |
} |
| 131 |
print(&limit) |
| 132 |
} |
| 133 |
{ |
| 134 |
limit := 10 |
| 135 |
limit++ |
| 136 |
for i := 0; i < limit; i++ { // nope: limit is assigned other than by its declaration |
| 137 |
} |
| 138 |
} |
| 139 |
for i := 0; i < Global; i++ { // nope: limit is an exported global var; may be updated elsewhere |
| 140 |
} |
| 141 |
for i := 0; i < len(table); i++ { // want "for loop can be modernized using range over int" |
| 142 |
} |
| 143 |
{ |
| 144 |
s := []string{} |
| 145 |
for i := 0; i < len(s); i++ { // nope: limit is not loop-invariant |
| 146 |
s = s[1:] |
| 147 |
} |
| 148 |
} |
| 149 |
for i := 0; i < len(slice); i++ { // nope: i is incremented within loop |
| 150 |
i += 1 |
| 151 |
} |
| 152 |
for Global = 0; Global < 10; Global++ { // nope: loop index is a global variable. |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
var Global int |
| 157 |
|
| 158 |
var table = []string{"hello", "world"} |
| 159 |
|
| 160 |
func f() int { return 0 } |
| 161 |
|
| 162 |
// Repro for part of #71847: ("for range n is invalid if the loop body contains i++"): |
| 163 |
func _(s string) { |
| 164 |
var i int // (this is necessary) |
| 165 |
for i = 0; i < len(s); i++ { // nope: loop body increments i |
| 166 |
if true { |
| 167 |
i++ // nope |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
// Repro for #71952: for and range loops have different final values |
| 173 |
// on i (n and n-1, respectively) so we can't offer the fix if i is |
| 174 |
// used after the loop. |
| 175 |
func nopePostconditionDiffers() { |
| 176 |
i := 0 |
| 177 |
for i = 0; i < 5; i++ { |
| 178 |
println(i) |
| 179 |
} |
| 180 |
println(i) // must print 5, not 4 |
| 181 |
} |
| 182 |
|
| 183 |
// Non-integer untyped constants need to be explicitly converted to int. |
| 184 |
func issue71847d() { |
| 185 |
const limit = 1e3 // float |
| 186 |
for i := 0; i < limit; i++ { // want "for loop can be modernized using range over int" |
| 187 |
} |
| 188 |
for i := int(0); i < limit; i++ { // want "for loop can be modernized using range over int" |
| 189 |
} |
| 190 |
for i := uint(0); i < limit; i++ { // want "for loop can be modernized using range over int" |
| 191 |
} |
| 192 |
|
| 193 |
const limit2 = 1 + 0i // complex |
| 194 |
for i := 0; i < limit2; i++ { // want "for loop can be modernized using range over int" |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
func issue72726() { |
| 199 |
var n, kd int |
| 200 |
for i := 0; i < n; i++ { // want "for loop can be modernized using range over int" |
| 201 |
// nope: j will be invisible once it's refactored to 'for j := range min(n-j, kd+1)' |
| 202 |
for j := 0; j < min(n-j, kd+1); j++ { // nope |
| 203 |
_, _ = i, j |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
for i := 0; i < i; i++ { // nope |
| 208 |
} |
| 209 |
|
| 210 |
var i int |
| 211 |
for i = 0; i < i/2; i++ { // nope |
| 212 |
} |
| 213 |
|
| 214 |
var arr []int |
| 215 |
for i = 0; i < arr[i]; i++ { // nope |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
func todo() { |
| 220 |
for j := os1.FileMode(0); j < 10; j++ { // want "for loop can be modernized using range over int" |
| 221 |
println(j) |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
type T uint |
| 226 |
type TAlias = uint |
| 227 |
|
| 228 |
func Fn(a int) T { |
| 229 |
return T(a) |
| 230 |
} |
| 231 |
|
| 232 |
func issue73037() { |
| 233 |
var q T |
| 234 |
for a := T(0); a < q; a++ { // want "for loop can be modernized using range over int" |
| 235 |
println(a) |
| 236 |
} |
| 237 |
for a := Fn(0); a < q; a++ { |
| 238 |
println(a) |
| 239 |
} |
| 240 |
var qa TAlias |
| 241 |
for a := TAlias(0); a < qa; a++ { // want "for loop can be modernized using range over int" |
| 242 |
println(a) |
| 243 |
} |
| 244 |
for a := T(0); a < 10; a++ { // want "for loop can be modernized using range over int" |
| 245 |
for b := T(0); b < 10; b++ { // want "for loop can be modernized using range over int" |
| 246 |
println(a, b) |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
func issue75289() { |
| 252 |
// A use of i within a defer may be textually before the loop but runs |
| 253 |
// after, so it should cause the loop to be rejected as a candidate |
| 254 |
// to avoid it observing a different final value of i. |
| 255 |
{ |
| 256 |
var i int |
| 257 |
defer func() { println(i) }() |
| 258 |
for i = 0; i < 10; i++ { // nope: i is accessed after the loop (via defer) |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
// A use of i within a defer within the loop is also a dealbreaker. |
| 263 |
{ |
| 264 |
var i int |
| 265 |
for i = 0; i < 10; i++ { // nope: i is accessed after the loop (via defer) |
| 266 |
defer func() { println(i) }() |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
// This (outer) defer is irrelevant. |
| 271 |
defer func() { |
| 272 |
var i int |
| 273 |
for i = 0; i < 10; i++ { // want "for loop can be modernized using range over int" |
| 274 |
} |
| 275 |
}() |
| 276 |
} |
| 277 |
|
| 278 |
// See go.dev/issue/76880. |
| 279 |
func _() (i int) { |
| 280 |
for i = 0; i < 3; i++ { // nope: i is implicitly accessed after the loop |
| 281 |
} |
| 282 |
return |
| 283 |
} |
| 284 |
|
| 285 |
func issue74687() { |
| 286 |
for i := a.ID(0); i < 10; i++ { // want "for loop can be modernized using range over int" |
| 287 |
println(i) |
| 288 |
} |
| 289 |
|
| 290 |
for i := a.ID(0); i < a.ID(13); i++ { // want "for loop can be modernized using range over int" |
| 291 |
println(i) |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
func issue76470() { |
| 296 |
var i, j int |
| 297 |
UsedAfter: |
| 298 |
for i = 0; i < 10; i++ { // nope: i is accessed after the loop |
| 299 |
break UsedAfter |
| 300 |
} |
| 301 |
if i == 9 { |
| 302 |
panic("Modernizer changes behavior") |
| 303 |
} |
| 304 |
|
| 305 |
NotUsedAfter: |
| 306 |
for j = 0; j < 10; j++ { // want "for loop can be modernized using range over int" |
| 307 |
break NotUsedAfter |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
// Don't allow rewriting of an outer loop when its inner loop modifies the outer |
| 312 |
// loop variable. |
| 313 |
func issue77034() { |
| 314 |
for i := 0; i < 5; i++ { |
| 315 |
for i = range 10 { |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
func issue77034_define_inner() { |
| 321 |
for i := 0; i < 5; i++ { // want "for loop can be modernized using range over int" |
| 322 |
for i := range 10 { // inner "i" doesn't modify outer "i" |
| 323 |
println(i) |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
type C int32 |
| 329 |
|
| 330 |
// Don't add an unneeded type cast if the limit's default type is an integer, and |
| 331 |
// the init value is assigned outside the loop, because the compiler can infer the type. |
| 332 |
func issue77891() { |
| 333 |
const limit_float = 1e6 |
| 334 |
var i int |
| 335 |
for i = 0; i < limit_float; i++ { // want "for loop can be modernized using range over int" |
| 336 |
println(i) |
| 337 |
} |
| 338 |
|
| 339 |
var j int64 |
| 340 |
for j = 0; j < 20; j++ { // want "for loop can be modernized using range over int" |
| 341 |
println(j) |
| 342 |
} |
| 343 |
|
| 344 |
var c C |
| 345 |
for c = 0; c < 10; c++ { // want "for loop can be modernized using range over int" |
| 346 |
println(c) |
| 347 |
} |
| 348 |
|
| 349 |
var ptr uintptr |
| 350 |
for ptr = 0; ptr < 100; ptr++ { // want "for loop can be modernized using range over int" |
| 351 |
println(ptr) |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
// Regression tests for golang/go#78571: the loop index cannot be a type |
| 356 |
// parameter constrained by multiple distinct integer types, nor a type |
| 357 |
// parameter constrained by a non-integer type. |
| 358 |
func issue78571[I ~int | ~uint](limit I) { |
| 359 |
var i I |
| 360 |
for i = 0; i < limit; i++ { // nope: int and uint have different underlying types. |
| 361 |
println(i) |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
type MyInt int |
| 366 |
|
| 367 |
func issue78571_sameunderlying[T int | MyInt](limit T) { |
| 368 |
var i T |
| 369 |
for i = 0; i < limit; i++ { // want "for loop can be modernized using range over int" |
| 370 |
println(i) |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
func issue78571_single[T ~int](limit T) { |
| 375 |
var i T |
| 376 |
for i = 0; i < limit; i++ { // want "for loop can be modernized using range over int" |
| 377 |
println(i) |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
func issue78571_float[T ~float64](limit T) { |
| 382 |
var i T |
| 383 |
for i = 0; i < limit; i++ { // nope: can't range over a float |
| 384 |
println(i) |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
func issue78571_floats[T float32 | float64](limit T) { |
| 389 |
var i T |
| 390 |
for i = 0; i < limit; i++ { // nope: can't range over a float |
| 391 |
println(i) |
| 392 |
} |
| 393 |
} |