This includes only a couple of minor cmd/vet fixes for new(expr).

    export GOWORK=off
    cd src/cmd
    go get golang.org/x/tools@4df13e3
    go mod tidy
    go mod vendor

For #45624

Change-Id: Iafba4350d321d6cd1fcc91a062e2c150e3f4d553
Reviewed-on: https://go-review.googlesource.com/c/go/+/706735
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>
This commit is contained in:
Alan Donovan 2025-09-25 10:30:14 -04:00 committed by Gopher Robot
parent 6b32c613ca
commit 81a83bba21
5 changed files with 34 additions and 17 deletions

View file

@ -11,7 +11,7 @@ require (
golang.org/x/sys v0.36.0
golang.org/x/telemetry v0.0.0-20250908211612-aef8a434d053
golang.org/x/term v0.34.0
golang.org/x/tools v0.37.1-0.20250915202913-9fccddc465ef
golang.org/x/tools v0.37.1-0.20250924232827-4df13e317ce4
)
require (

View file

@ -22,7 +22,7 @@ golang.org/x/term v0.34.0 h1:O/2T7POpk0ZZ7MAzMeWFSg6S5IpWd/RXDlM9hgM3DR4=
golang.org/x/term v0.34.0/go.mod h1:5jC53AEywhIVebHgPVeg0mj8OD3VO9OzclacVrqpaAw=
golang.org/x/text v0.29.0 h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk=
golang.org/x/text v0.29.0/go.mod h1:7MhJOA9CD2qZyOKYazxdYMF85OwPdEr9jTtBpO7ydH4=
golang.org/x/tools v0.37.1-0.20250915202913-9fccddc465ef h1:ISPkUgvOYIt0oS7oVnwAPktCKBvgWkDlWWGMgX0veZM=
golang.org/x/tools v0.37.1-0.20250915202913-9fccddc465ef/go.mod h1:MBN5QPQtLMHVdvsbtarmTNukZDdgwdwlO5qGacAzF0w=
golang.org/x/tools v0.37.1-0.20250924232827-4df13e317ce4 h1:IcXDtHggZZo+GzNzvVRPyNFLnOc2/Z1gg3ZVIWF2uCU=
golang.org/x/tools v0.37.1-0.20250924232827-4df13e317ce4/go.mod h1:MBN5QPQtLMHVdvsbtarmTNukZDdgwdwlO5qGacAzF0w=
rsc.io/markdown v0.0.0-20240306144322-0bf8f97ee8ef h1:mqLYrXCXYEZOop9/Dbo6RPX11539nwiCNBb1icVPmw8=
rsc.io/markdown v0.0.0-20240306144322-0bf8f97ee8ef/go.mod h1:8xcPgWmwlZONN1D9bjxtHEjrUtSEa3fakVF8iaewYKQ=

View file

@ -157,7 +157,10 @@ func checkCopyLocksCallExpr(pass *analysis.Pass, ce *ast.CallExpr) {
}
if fun, ok := pass.TypesInfo.Uses[id].(*types.Builtin); ok {
switch fun.Name() {
case "new", "len", "cap", "Sizeof", "Offsetof", "Alignof":
case "len", "cap", "Sizeof", "Offsetof", "Alignof":
// The argument of this operation is used only
// for its type (e.g. len(array)), or the operation
// does not copy a lock (e.g. len(slice)).
return
}
}

View file

@ -193,18 +193,23 @@ func CheckReadable(pass *analysis.Pass, filename string) error {
return fmt.Errorf("Pass.ReadFile: %s is not among OtherFiles, IgnoredFiles, or names of Files", filename)
}
// AddImport checks whether this file already imports pkgpath and
// that import is in scope at pos. If so, it returns the name under
// which it was imported and a zero edit. Otherwise, it adds a new
// import of pkgpath, using a name derived from the preferred name,
// and returns the chosen name, a prefix to be concatenated with member
// to form a qualified name, and the edit for the new import.
// AddImport checks whether this file already imports pkgpath and that
// the import is in scope at pos. If so, it returns the name under
// which it was imported and no edits. Otherwise, it adds a new import
// of pkgpath, using a name derived from the preferred name, and
// returns the chosen name, a prefix to be concatenated with member to
// form a qualified name, and the edit for the new import.
//
// In the special case that pkgpath is dot-imported then member, the
// identifier for which the import is being added, is consulted. If
// member is not shadowed at pos, AddImport returns (".", "", nil).
// (AddImport accepts the caller's implicit claim that the imported
// package declares member.)
// The member argument indicates the name of the desired symbol within
// the imported package. This is needed in the case when the existing
// import is a dot import, because then it is possible that the
// desired symbol is shadowed by other declarations in the current
// package. If member is not shadowed at pos, AddImport returns (".",
// "", nil). (AddImport accepts the caller's implicit claim that the
// imported package declares member.)
//
// Use a preferredName of "_" to request a blank import;
// member is ignored in this case.
//
// It does not mutate its arguments.
func AddImport(info *types.Info, file *ast.File, preferredName, pkgpath, member string, pos token.Pos) (name, prefix string, newImport []analysis.TextEdit) {
@ -220,6 +225,10 @@ func AddImport(info *types.Info, file *ast.File, preferredName, pkgpath, member
pkgname := info.PkgNameOf(spec)
if pkgname != nil && pkgname.Imported().Path() == pkgpath {
name = pkgname.Name()
if preferredName == "_" {
// Request for blank import; any existing import will do.
return name, "", nil
}
if name == "." {
// The scope of ident must be the file scope.
if s, _ := scope.LookupParent(member, pos); s == info.Scopes[file] {
@ -232,8 +241,12 @@ func AddImport(info *types.Info, file *ast.File, preferredName, pkgpath, member
}
// We must add a new import.
// Ensure we have a fresh name.
newName := FreshName(scope, pos, preferredName)
newName := preferredName
if preferredName != "_" {
newName = FreshName(scope, pos, preferredName)
}
// Create a new import declaration either before the first existing
// declaration (which must exist), including its comments; or
@ -246,6 +259,7 @@ func AddImport(info *types.Info, file *ast.File, preferredName, pkgpath, member
if newName != preferredName || newName != pathpkg.Base(pkgpath) {
newText = fmt.Sprintf("%s %q", newName, pkgpath)
}
decl0 := file.Decls[0]
var before ast.Node = decl0
switch decl0 := decl0.(type) {

View file

@ -73,7 +73,7 @@ golang.org/x/text/internal/tag
golang.org/x/text/language
golang.org/x/text/transform
golang.org/x/text/unicode/norm
# golang.org/x/tools v0.37.1-0.20250915202913-9fccddc465ef
# golang.org/x/tools v0.37.1-0.20250924232827-4df13e317ce4
## explicit; go 1.24.0
golang.org/x/tools/cmd/bisect
golang.org/x/tools/cover