Skip to content

Commit 31e3bb2

Browse files
adonovangopherbot
authored andcommitted
[gopls-release-branch.0.18] gopls/internal/analysis/modernize: fix rangeint bug
info.Defs[v] is nil if the loop variable is not declared (for i = 0 instead of for i := 0). + test Updates golang/go#71847 Change-Id: I28f82188e813f2d4f1ddc9335f0c13bd90c31ec1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/650815 Auto-Submit: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]> (cherry picked from commit 300465c) Reviewed-on: https://go-review.googlesource.com/c/tools/+/651095 Auto-Submit: Robert Findley <[email protected]> Reviewed-by: Alan Donovan <[email protected]> Commit-Queue: Alan Donovan <[email protected]>
1 parent 260fd3c commit 31e3bb2

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

gopls/internal/analysis/modernize/rangeint.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func rangeint(pass *analysis.Pass) {
7575
// Have: for i = 0; i < limit; i++ {}
7676

7777
// Find references to i within the loop body.
78-
v := info.Defs[index]
78+
v := info.ObjectOf(index)
7979
used := false
8080
for curId := range curLoop.Child(loop.Body).Preorder((*ast.Ident)(nil)) {
8181
id := curId.Node().(*ast.Ident)

gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go

+13
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ func _(i int, s struct{ i int }, slice []int) {
1212
for i := 0; i < len(slice); i++ { // want "for loop can be modernized using range over int"
1313
println(slice[i])
1414
}
15+
for i := 0; i < len(""); i++ { // want "for loop can be modernized using range over int"
16+
// NB: not simplified to range ""
17+
}
1518

1619
// nope
1720
for i := 0; i < 10; { // nope: missing increment
@@ -38,3 +41,13 @@ func _(i int, s struct{ i int }, slice []int) {
3841
}
3942

4043
func f() int { return 0 }
44+
45+
// Repro for part of #71847: ("for range n is invalid if the loop body contains i++"):
46+
func _(s string) {
47+
var i int // (this is necessary)
48+
for i = 0; i < len(s); i++ { // nope: loop body increments i
49+
if true {
50+
i++ // nope
51+
}
52+
}
53+
}

gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden

+13
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ func _(i int, s struct{ i int }, slice []int) {
1212
for i := range slice { // want "for loop can be modernized using range over int"
1313
println(slice[i])
1414
}
15+
for range len("") { // want "for loop can be modernized using range over int"
16+
// NB: not simplified to range ""
17+
}
1518

1619
// nope
1720
for i := 0; i < 10; { // nope: missing increment
@@ -38,3 +41,13 @@ func _(i int, s struct{ i int }, slice []int) {
3841
}
3942

4043
func f() int { return 0 }
44+
45+
// Repro for part of #71847: ("for range n is invalid if the loop body contains i++"):
46+
func _(s string) {
47+
var i int // (this is necessary)
48+
for i = 0; i < len(s); i++ { // nope: loop body increments i
49+
if true {
50+
i++ // nope
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)