Skip to content

Commit 7de8d0a

Browse files
committed
gopls/internal/analysis/simplifyrange: reenable on range-over-func
This reverts the functional change of commit 3629652 (CL 588056) that caused range-over-func to differ from other range operands; but we keep some tests and tidy up the logic. It was decided at the 11th hour to permit redundant blanks in range-over-func statements in the go1.23 spec; see golang/go#65236 (comment). Fixes golang/go#67239 Updates golang/go#65236 Change-Id: Ib3c1c535a1107a05f18732e07d7c8844bbac4d1e Reviewed-on: https://go-review.googlesource.com/c/tools/+/594555 Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent c9675c2 commit 7de8d0a

File tree

4 files changed

+15
-22
lines changed

4 files changed

+15
-22
lines changed

gopls/internal/analysis/simplifyrange/simplifyrange.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"go/ast"
1111
"go/printer"
1212
"go/token"
13-
"go/types"
1413

1514
"golang.org/x/tools/go/analysis"
1615
"golang.org/x/tools/go/analysis/passes/inspect"
@@ -35,16 +34,12 @@ func run(pass *analysis.Pass) (interface{}, error) {
3534
(*ast.RangeStmt)(nil),
3635
}
3736
inspect.Preorder(nodeFilter, func(n ast.Node) {
38-
stmt := n.(*ast.RangeStmt)
39-
40-
// go1.23's range-over-func requires all vars, blank if necessary.
41-
// TODO(adonovan): this may change in go1.24; see #65236.
42-
if _, ok := pass.TypesInfo.TypeOf(stmt.X).Underlying().(*types.Signature); ok {
43-
return
37+
var copy *ast.RangeStmt // shallow-copy the AST before modifying
38+
{
39+
x := *n.(*ast.RangeStmt)
40+
copy = &x
4441
}
45-
46-
copy := *stmt
47-
end := newlineIndex(pass.Fset, &copy)
42+
end := newlineIndex(pass.Fset, copy)
4843

4944
// Range statements of the form: for i, _ := range x {}
5045
var old ast.Expr
@@ -65,7 +60,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
6560
Pos: old.Pos(),
6661
End: old.End(),
6762
Message: "simplify range expression",
68-
SuggestedFixes: suggestedFixes(pass.Fset, &copy, end),
63+
SuggestedFixes: suggestedFixes(pass.Fset, copy, end),
6964
})
7065
})
7166
return nil, nil

gopls/internal/analysis/simplifyrange/simplifyrange_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
func Test(t *testing.T) {
1717
testdata := analysistest.TestData()
1818
analysistest.RunWithSuggestedFixes(t, testdata, simplifyrange.Analyzer, "a")
19-
if slices.Contains(build.Default.ReleaseTags, "go1.23") {
19+
if slices.Contains(build.Default.ReleaseTags, "go1.23") { // uses iter.Seq
2020
analysistest.RunWithSuggestedFixes(t, testdata, simplifyrange.Analyzer, "rangeoverfunc")
2121
}
2222
}

gopls/internal/analysis/simplifyrange/testdata/src/rangeoverfunc/rangeoverfunc.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ package testdata
77
import "iter"
88

99
func _(seq1 iter.Seq[int], seq2 iter.Seq2[int, int]) {
10+
// range-over-func is (once again) consistent with other types (#65236)
1011
for _ = range "" { // want "simplify range expression"
1112
}
12-
13-
// silence
14-
for _ = range seq1 {
13+
for _ = range seq1 { // want `simplify range expression`
1514
}
16-
for _, v := range seq2 {
15+
for _, v := range seq2 { // silence
1716
_ = v
1817
}
19-
for _, _ = range seq2 {
18+
for _, _ = range seq2 { // want `simplify range expression`
2019
}
2120
}

gopls/internal/analysis/simplifyrange/testdata/src/rangeoverfunc/rangeoverfunc.go.golden

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ package testdata
77
import "iter"
88

99
func _(seq1 iter.Seq[int], seq2 iter.Seq2[int, int]) {
10+
// range-over-func is (once again) consistent with other types (#65236)
1011
for range "" { // want "simplify range expression"
1112
}
12-
13-
// silence
14-
for _ = range seq1 {
13+
for range seq1 { // want `simplify range expression`
1514
}
16-
for _, v := range seq2 {
15+
for _, v := range seq2 { // silence
1716
_ = v
1817
}
19-
for _, _ = range seq2 {
18+
for range seq2 { // want `simplify range expression`
2019
}
2120
}

0 commit comments

Comments
 (0)