Skip to content

Commit 3629652

Browse files
committed
gopls/internal/analysis/simplifyrange: suppress on range-over-func
go1.23's range-over-func currently requires all the vars be declared, blank if necessary. That may change, but for now, suppress the checker. Fixes golang/go#67239 Updates golang/go#65236 Change-Id: I3e783fcfcb6a6f01f3acf62428cd9accbeb160c1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/588056 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Alan Donovan <[email protected]> Commit-Queue: Alan Donovan <[email protected]>
1 parent fb52877 commit 3629652

File tree

4 files changed

+57
-8
lines changed

4 files changed

+57
-8
lines changed

gopls/internal/analysis/simplifyrange/simplifyrange.go

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

1415
"golang.org/x/tools/go/analysis"
1516
"golang.org/x/tools/go/analysis/passes/inspect"
@@ -34,15 +35,16 @@ func run(pass *analysis.Pass) (interface{}, error) {
3435
(*ast.RangeStmt)(nil),
3536
}
3637
inspect.Preorder(nodeFilter, func(n ast.Node) {
37-
var copy *ast.RangeStmt
38-
if stmt, ok := n.(*ast.RangeStmt); ok {
39-
x := *stmt
40-
copy = &x
41-
}
42-
if copy == nil {
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 {
4343
return
4444
}
45-
end := newlineIndex(pass.Fset, copy)
45+
46+
copy := *stmt
47+
end := newlineIndex(pass.Fset, &copy)
4648

4749
// Range statements of the form: for i, _ := range x {}
4850
var old ast.Expr
@@ -63,7 +65,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
6365
Pos: old.Pos(),
6466
End: old.End(),
6567
Message: "simplify range expression",
66-
SuggestedFixes: suggestedFixes(pass.Fset, copy, end),
68+
SuggestedFixes: suggestedFixes(pass.Fset, &copy, end),
6769
})
6870
})
6971
return nil, nil

gopls/internal/analysis/simplifyrange/simplifyrange_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
55
package simplifyrange_test
66

77
import (
8+
"go/build"
89
"testing"
910

1011
"golang.org/x/tools/go/analysis/analysistest"
1112
"golang.org/x/tools/gopls/internal/analysis/simplifyrange"
13+
"golang.org/x/tools/gopls/internal/util/slices"
1214
)
1315

1416
func Test(t *testing.T) {
1517
testdata := analysistest.TestData()
1618
analysistest.RunWithSuggestedFixes(t, testdata, simplifyrange.Analyzer, "a")
19+
if slices.Contains(build.Default.ReleaseTags, "go1.23") {
20+
analysistest.RunWithSuggestedFixes(t, testdata, simplifyrange.Analyzer, "rangeoverfunc")
21+
}
1722
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2024 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package testdata
6+
7+
import "iter"
8+
9+
func _(seq1 iter.Seq[int], seq2 iter.Seq2[int, int]) {
10+
for _ = range "" { // want "simplify range expression"
11+
}
12+
13+
// silence
14+
for _ = range seq1 {
15+
}
16+
for _, v := range seq2 {
17+
_ = v
18+
}
19+
for _, _ = range seq2 {
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2024 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package testdata
6+
7+
import "iter"
8+
9+
func _(seq1 iter.Seq[int], seq2 iter.Seq2[int, int]) {
10+
for range "" { // want "simplify range expression"
11+
}
12+
13+
// silence
14+
for _ = range seq1 {
15+
}
16+
for _, v := range seq2 {
17+
_ = v
18+
}
19+
for _, _ = range seq2 {
20+
}
21+
}

0 commit comments

Comments
 (0)