Skip to content

Commit d160982

Browse files
valyalarobpike
authored andcommitted
cmd/vet: fix copylocks false positive on len(array) and cap(array).
This is a follow-up for https://golang.org/cl/24340. Updates #14664. Fixes #18374. Change-Id: I2831556a9014d30ec70d5f91943d18c33db5b390 Reviewed-on: https://go-review.googlesource.com/34630 Reviewed-by: Rob Pike <[email protected]> Run-TryBot: Rob Pike <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent a3f4cc0 commit d160982

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/cmd/vet/copylock.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,11 @@ func checkCopyLocksReturnStmt(f *File, rs *ast.ReturnStmt) {
9393

9494
// checkCopyLocksCallExpr detects lock copy in the arguments to a function call
9595
func checkCopyLocksCallExpr(f *File, ce *ast.CallExpr) {
96-
if id, ok := ce.Fun.(*ast.Ident); ok && id.Name == "new" && f.pkg.types[id].IsBuiltin() {
97-
// Skip 'new(Type)' for built-in 'new'
98-
return
96+
if id, ok := ce.Fun.(*ast.Ident); ok && f.pkg.types[id].IsBuiltin() {
97+
switch id.Name {
98+
case "new", "len", "cap":
99+
return
100+
}
99101
}
100102
for _, x := range ce.Args {
101103
if path := lockPathRhs(f, x); path != nil {

src/cmd/vet/testdata/copylock.go

+14
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@ func BadFunc() {
8888
fmuSlice := fmuA[:] // OK
8989
}
9090

91+
func LenAndCapOnLockArrays() {
92+
var a [5]sync.Mutex
93+
aLen := len(a) // OK
94+
aCap := cap(a) // OK
95+
96+
// override 'len' and 'cap' keywords
97+
98+
len := func(interface{}) {}
99+
len(a) // ERROR "function call copies lock value: sync.Mutex"
100+
101+
cap := func(interface{}) {}
102+
cap(a) // ERROR "function call copies lock value: sync.Mutex"
103+
}
104+
91105
// SyncTypesCheck checks copying of sync.* types except sync.Mutex
92106
func SyncTypesCheck() {
93107
// sync.RWMutex copying

0 commit comments

Comments
 (0)