Skip to content

Commit 034e59c

Browse files
adonovangopherbot
authored andcommitted
internal/analysis/analyzerutil: fix FileUsesGoVersion
This CL fixes a logic error in FileUsesGoVersion's bootstrap check (wrong operand order). Also, it excludes _test.go files from consideration of the bootstrap minimum version. This unbreaks TestVetStdlib; see CL 718503. Change-Id: I7101216567ecfb6ad01e420fa31be661aa302d57 Reviewed-on: https://go-review.googlesource.com/c/tools/+/720000 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Auto-Submit: Dmitri Shuralyov <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent 076bd80 commit 034e59c

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

internal/analysis/analyzerutil/version.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package analyzerutil
66

77
import (
88
"go/ast"
9+
"strings"
910

1011
"golang.org/x/tools/go/analysis"
1112
"golang.org/x/tools/internal/packagepath"
@@ -22,18 +23,20 @@ import (
2223
// operation is not free, yet is not a highly selective filter: the
2324
// fraction of files that pass most version checks is high and
2425
// increases over time.
25-
func FileUsesGoVersion(pass *analysis.Pass, file *ast.File, version string) bool {
26+
func FileUsesGoVersion(pass *analysis.Pass, file *ast.File, version string) (_res bool) {
27+
fileVersion := pass.TypesInfo.FileVersions[file]
28+
2629
// Standard packages that are part of toolchain bootstrapping
2730
// are not considered to use a version of Go later than the
2831
// current bootstrap toolchain version.
32+
// The bootstrap rule does not cover tests,
33+
// and some tests (e.g. debug/elf/file_test.go) rely on this.
2934
pkgpath := pass.Pkg.Path()
3035
if packagepath.IsStdPackage(pkgpath) &&
31-
stdlib.IsBootstrapPackage(pkgpath) &&
32-
versions.Before(version, stdlib.BootstrapVersion.String()) {
33-
return false // package must bootstrap
34-
}
35-
if versions.Before(pass.TypesInfo.FileVersions[file], version) {
36-
return false // file version is too old
36+
stdlib.IsBootstrapPackage(pkgpath) && // (excludes "*_test" external test packages)
37+
!strings.HasSuffix(pass.Fset.File(file.Pos()).Name(), "_test.go") { // (excludes all tests)
38+
fileVersion = stdlib.BootstrapVersion.String() // package must bootstrap
3739
}
38-
return true // ok
40+
41+
return !versions.Before(fileVersion, version)
3942
}

0 commit comments

Comments
 (0)