Skip to content

Commit ab04c19

Browse files
adonovangopherbot
authored andcommitted
gopls/internal/analysis/modernize: improve rangeint transformation
for i := 0; i < len(slice); i++ {} is currently reduced to for i := range len(slice) {} but this CL delivers the better style of: for i := range slice {} Fixes golang/go#71725 Change-Id: Idb025315047c3be992267f6c1783757798e0c840 Reviewed-on: https://go-review.googlesource.com/c/tools/+/649356 Reviewed-by: Jonathan Amsterdam <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Alan Donovan <[email protected]>
1 parent ddd4bde commit ab04c19

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

gopls/internal/analysis/modernize/rangeint.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import (
88
"fmt"
99
"go/ast"
1010
"go/token"
11+
"go/types"
1112

1213
"golang.org/x/tools/go/analysis"
1314
"golang.org/x/tools/go/analysis/passes/inspect"
1415
"golang.org/x/tools/go/ast/inspector"
16+
"golang.org/x/tools/go/types/typeutil"
1517
"golang.org/x/tools/internal/analysisinternal"
1618
"golang.org/x/tools/internal/astutil/cursor"
1719
"golang.org/x/tools/internal/astutil/edge"
@@ -98,6 +100,14 @@ func rangeint(pass *analysis.Pass) {
98100
})
99101
}
100102

103+
// If limit is len(slice),
104+
// simplify "range len(slice)" to "range slice".
105+
if call, ok := limit.(*ast.CallExpr); ok &&
106+
typeutil.Callee(info, call) == builtinLen &&
107+
is[*types.Slice](info.TypeOf(call.Args[0]).Underlying()) {
108+
limit = call.Args[0]
109+
}
110+
101111
pass.Report(analysis.Diagnostic{
102112
Pos: init.Pos(),
103113
End: inc.End(),

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package rangeint
22

3-
func _(i int, s struct{ i int }) {
3+
func _(i int, s struct{ i int }, slice []int) {
44
for i := 0; i < 10; i++ { // want "for loop can be modernized using range over int"
55
println(i)
66
}
@@ -9,6 +9,9 @@ func _(i int, s struct{ i int }) {
99
for i := 0; i < 10; i++ { // want "for loop can be modernized using range over int"
1010
// i unused within loop
1111
}
12+
for i := 0; i < len(slice); i++ { // want "for loop can be modernized using range over int"
13+
println(slice[i])
14+
}
1215

1316
// nope
1417
for i := 0; i < 10; { // nope: missing increment

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package rangeint
22

3-
func _(i int, s struct{ i int }) {
3+
func _(i int, s struct{ i int }, slice []int) {
44
for i := range 10 { // want "for loop can be modernized using range over int"
55
println(i)
66
}
@@ -9,6 +9,9 @@ func _(i int, s struct{ i int }) {
99
for range 10 { // want "for loop can be modernized using range over int"
1010
// i unused within loop
1111
}
12+
for i := range slice { // want "for loop can be modernized using range over int"
13+
println(slice[i])
14+
}
1215

1316
// nope
1417
for i := 0; i < 10; { // nope: missing increment

0 commit comments

Comments
 (0)