| 1 | // Copyright 2025 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 analyzerutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/ |
| 6 | |
| 7 | import ( |
| 8 | "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 9 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 10 | |
| 11 | "golang.org/x/tools/go/analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/" |
| 12 | "golang.org/x/tools/internal/packagepathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagepath`/" |
| 13 | "golang.org/x/tools/internal/stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/" |
| 14 | "golang.org/x/tools/internal/versionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/versions`/" |
| 15 | ) |
| 16 | |
| 17 | // FileUsesGoVersion reports whether the specified file may use features of the |
| 18 | // specified version of Go (e.g. "go1.24"). |
| 19 | // |
| 20 | // It returns false when version information is not available, |
| 21 | // such as for parsed files that are ignored by the type checker. |
| 22 | // |
| 23 | // Tip: we recommend using this check "late", just before calling |
| 24 | // pass.Report, rather than "early" (when entering each ast.File, or |
| 25 | // each candidate node of interest, during the traversal), because the |
| 26 | // operation is not free, yet is not a highly selective filter: the |
| 27 | // fraction of files that pass most version checks is high and |
| 28 | // increases over time. |
| 29 | func FileUsesGoVersionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/FileUsesGoVersion().(pass *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#, file *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#, version string) (_res bool) { |
| 30 | fileVersion, ok := pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo..FileVersionsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#FileVersions.[file] |
| 31 | if !ok { |
| 32 | return false // be conservative in the absence of information (e.g. IgnoredFiles) |
| 33 | } |
| 34 | |
| 35 | // Standard packages that are part of toolchain bootstrapping |
| 36 | // are not considered to use a version of Go later than the |
| 37 | // current bootstrap toolchain version. |
| 38 | // The bootstrap rule does not cover tests, |
| 39 | // and some tests (e.g. debug/elf/file_test.go) rely on this. |
| 40 | pkgpath := pass.Pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Pkg..Pathscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Path().() |
| 41 | if packagepathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagepath`/.IsStdPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagepath`/IsStdPackage().(pkgpath) && |
| 42 | stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.IsBootstrapPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/IsBootstrapPackage().(pkgpath) && // (excludes "*_test" external test packages) |
| 43 | !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(pass.Fsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Fset..Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#File().(file.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Pos().()).Namescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Name().(), "_test.go") { // (excludes all tests) |
| 44 | fileVersion = stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.BootstrapVersionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/BootstrapVersion..Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Version#String().() // package must bootstrap |
| 45 | } |
| 46 | |
| 47 | return !versionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/versions`/.Beforescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/versions`/Before().(fileVersion, version) |
| 48 | } |