Skip to content

Commit 4df13e3

Browse files
committed
x/tools: fix remaining places in preparation for new(expr)
+ tests For golang/go#45624 Change-Id: Idb5b0a78f9af9230abffe0282087ac50494d967f Reviewed-on: https://go-review.googlesource.com/c/tools/+/706255 Reviewed-by: Robert Findley <rfindley@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent ed00c08 commit 4df13e3

5 files changed

Lines changed: 42 additions & 6 deletions

File tree

go/analysis/passes/copylock/copylock.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,10 @@ func checkCopyLocksCallExpr(pass *analysis.Pass, ce *ast.CallExpr) {
157157
}
158158
if fun, ok := pass.TypesInfo.Uses[id].(*types.Builtin); ok {
159159
switch fun.Name() {
160-
case "new", "len", "cap", "Sizeof", "Offsetof", "Alignof":
160+
case "len", "cap", "Sizeof", "Offsetof", "Alignof":
161+
// The argument of this operation is used only
162+
// for its type (e.g. len(array)), or the operation
163+
// does not copy a lock (e.g. len(slice)).
161164
return
162165
}
163166
}

go/analysis/passes/copylock/copylock_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"golang.org/x/tools/go/analysis/analysistest"
1212
"golang.org/x/tools/go/analysis/passes/copylock"
13-
"golang.org/x/tools/internal/testenv"
1413
"golang.org/x/tools/internal/testfiles"
1514
)
1615

@@ -20,8 +19,6 @@ func Test(t *testing.T) {
2019
}
2120

2221
func TestVersions22(t *testing.T) {
23-
testenv.NeedsGo1Point(t, 22)
24-
2522
dir := testfiles.ExtractTxtarFileToTmp(t, filepath.Join(analysistest.TestData(), "src", "forstmt", "go22.txtar"))
2623
analysistest.Run(t, dir, copylock.Analyzer, "golang.org/fake/forstmt")
2724
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
//go:build go1.26
6+
7+
package a
8+
9+
import "sync"
10+
11+
func _(ptr *sync.Mutex) {
12+
_ = new(sync.Mutex)
13+
_ = new(*ptr) // want `call of new copies lock value: sync.Mutex`
14+
)

refactor/satisfy/find.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,10 @@ func (f *Finder) call(sig *types.Signature, args []ast.Expr) {
204204
func (f *Finder) builtin(obj *types.Builtin, sig *types.Signature, args []ast.Expr) {
205205
switch obj.Name() {
206206
case "make", "new":
207-
// skip the type operand
208-
for _, arg := range args[1:] {
207+
for i, arg := range args {
208+
if i == 0 && f.info.Types[arg].IsType() {
209+
continue // skip the type operand
210+
}
209211
f.expr(arg)
210212
}
211213

refactor/satisfy/find_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"sort"
1616
"testing"
1717

18+
"golang.org/x/tools/internal/testenv"
1819
"golang.org/x/tools/refactor/satisfy"
1920
)
2021

@@ -204,6 +205,25 @@ func _[P ~struct{F I}]() {
204205
}
205206
}
206207

208+
func TestNewExpr(t *testing.T) {
209+
testenv.NeedsGo1Point(t, 26)
210+
const src = `package p
211+
212+
type I interface{ f() }
213+
type C int
214+
func (C) f() {}
215+
216+
var _ I = new(C(123))
217+
`
218+
got := constraints(t, src)
219+
want := []string{
220+
"p.I <- *p.C",
221+
}
222+
if !reflect.DeepEqual(got, want) {
223+
t.Fatalf("found unexpected constraints: got %s, want %s", got, want)
224+
}
225+
}
226+
207227
func constraints(t *testing.T, src string) []string {
208228
// parse
209229
fset := token.NewFileSet()

0 commit comments

Comments
 (0)