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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
Commit-Queue: Alan Donovan <adonovan@google.com>
This commit is contained in:
Alan Donovan 2024-05-23 16:57:32 -04:00
Родитель fb52877ad2
Коммит 3629652b9d
4 изменённых файлов: 57 добавлений и 8 удалений

Просмотреть файл

@ -10,6 +10,7 @@ import (
"go/ast"
"go/printer"
"go/token"
"go/types"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
@ -34,15 +35,16 @@ func run(pass *analysis.Pass) (interface{}, error) {
(*ast.RangeStmt)(nil),
}
inspect.Preorder(nodeFilter, func(n ast.Node) {
var copy *ast.RangeStmt
if stmt, ok := n.(*ast.RangeStmt); ok {
x := *stmt
copy = &x
}
if copy == nil {
stmt := n.(*ast.RangeStmt)
// go1.23's range-over-func requires all vars, blank if necessary.
// TODO(adonovan): this may change in go1.24; see #65236.
if _, ok := pass.TypesInfo.TypeOf(stmt.X).Underlying().(*types.Signature); ok {
return
}
end := newlineIndex(pass.Fset, copy)
copy := *stmt
end := newlineIndex(pass.Fset, &copy)
// Range statements of the form: for i, _ := range x {}
var old ast.Expr
@ -63,7 +65,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
Pos: old.Pos(),
End: old.End(),
Message: "simplify range expression",
SuggestedFixes: suggestedFixes(pass.Fset, copy, end),
SuggestedFixes: suggestedFixes(pass.Fset, &copy, end),
})
})
return nil, nil

Просмотреть файл

@ -5,13 +5,18 @@
package simplifyrange_test
import (
"go/build"
"testing"
"golang.org/x/tools/go/analysis/analysistest"
"golang.org/x/tools/gopls/internal/analysis/simplifyrange"
"golang.org/x/tools/gopls/internal/util/slices"
)
func Test(t *testing.T) {
testdata := analysistest.TestData()
analysistest.RunWithSuggestedFixes(t, testdata, simplifyrange.Analyzer, "a")
if slices.Contains(build.Default.ReleaseTags, "go1.23") {
analysistest.RunWithSuggestedFixes(t, testdata, simplifyrange.Analyzer, "rangeoverfunc")
}
}

Просмотреть файл

@ -0,0 +1,21 @@
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package testdata
import "iter"
func _(seq1 iter.Seq[int], seq2 iter.Seq2[int, int]) {
for _ = range "" { // want "simplify range expression"
}
// silence
for _ = range seq1 {
}
for _, v := range seq2 {
_ = v
}
for _, _ = range seq2 {
}
}

Просмотреть файл

@ -0,0 +1,21 @@
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package testdata
import "iter"
func _(seq1 iter.Seq[int], seq2 iter.Seq2[int, int]) {
for range "" { // want "simplify range expression"
}
// silence
for _ = range seq1 {
}
for _, v := range seq2 {
_ = v
}
for _, _ = range seq2 {
}
}