cmd/vet: add stdversion analyzer

+ release note, test

Fixes #46136

Change-Id: Ib157c5343dde379f8d6f67bdfa64b3f6a6432bab
Reviewed-on: https://go-review.googlesource.com/c/go/+/582556
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
changes/56/582556/3
Alan Donovan 2024-04-30 16:21:20 -04:00
parent 344075d936
commit 6defff5005
6 changed files with 56 additions and 0 deletions

View File

@ -8,5 +8,19 @@ Distributions that install the `go` command to a location other than
`$GOROOT/bin/go` should install a symlink instead of relocating
or copying the `go` binary.
### Vet {#vet}
The `go vet` subcommand now includes the
[stdversion](https://beta.pkg.go.dev/golang.org/x/tools/go/analysis/passes/stdversion)
analyzer, which flags references to symbols that are too new for the version
of Go in effect in the referring file. (The effective version is determined
by the `go` directive in the file's enclosing `go.mod` file, and
by any [`//go:build` constraints](https://pkg.go.dev/cmd/go#hdr-Build_constraints)
in the file.)
For example, it will report a diagnostic for a reference to the
`reflect.TypeFor` function (introduced in go1.22) from a file in a
module whose go.mod file specifies `go 1.21`.
### Cgo {#cgo}

View File

@ -67,6 +67,7 @@ var passAnalyzersToVet = map[string]bool{
"sigchanyzer": true,
"slog": true,
"stdmethods": true,
"stdversion": true,
"stringintconv": true,
"structtag": true,
"testinggoroutine": true,

View File

@ -32,6 +32,7 @@ import (
"golang.org/x/tools/go/analysis/passes/sigchanyzer"
"golang.org/x/tools/go/analysis/passes/slog"
"golang.org/x/tools/go/analysis/passes/stdmethods"
"golang.org/x/tools/go/analysis/passes/stdversion"
"golang.org/x/tools/go/analysis/passes/stringintconv"
"golang.org/x/tools/go/analysis/passes/structtag"
"golang.org/x/tools/go/analysis/passes/testinggoroutine"
@ -70,6 +71,7 @@ func main() {
sigchanyzer.Analyzer,
slog.Analyzer,
stdmethods.Analyzer,
stdversion.Analyzer,
stringintconv.Analyzer,
structtag.Analyzer,
tests.Analyzer,

View File

@ -0,0 +1,3 @@
module stdversion
go 1.21

View File

@ -0,0 +1,5 @@
package stdversion
import "reflect"
var _ = reflect.TypeFor[int]() // ERROR "reflect.TypeFor requires go1.22 or later \(module is go1.21\)"

View File

@ -152,6 +152,37 @@ func TestVet(t *testing.T) {
t.Log("vet stderr:\n", cmd.Stderr)
}
})
// The stdversion analyzer requires a lower-than-tip go
// version in its go.mod file for it to report anything.
// So again we use a testdata go.mod file to "downgrade".
t.Run("stdversion", func(t *testing.T) {
cmd := testenv.Command(t, testenv.GoToolPath(t), "vet", "-vettool="+vetPath(t), ".")
cmd.Env = append(os.Environ(), "GOWORK=off")
cmd.Dir = "testdata/stdversion"
cmd.Stderr = new(strings.Builder) // all vet output goes to stderr
cmd.Run()
stderr := cmd.Stderr.(fmt.Stringer).String()
filename := filepath.FromSlash("testdata/stdversion/stdversion.go")
// Unlike the tests above, which runs vet in cmd/vet/, this one
// runs it in subdirectory, so the "full names" in the output
// are in fact short "./rangeloop.go".
// But we can't just pass "./rangeloop.go" as the "full name"
// argument to errorCheck as it does double duty as both a
// string that appears in the output, and as file name
// openable relative to the test directory, containing text
// expectations.
//
// So, we munge the file.
stderr = strings.ReplaceAll(stderr, filepath.FromSlash("./stdversion.go"), filename)
if err := errorCheck(stderr, false, filename, filepath.Base(filename)); err != nil {
t.Errorf("error check failed: %s", err)
t.Log("vet stderr:\n", cmd.Stderr)
}
})
}
func cgoEnabled(t *testing.T) bool {