From: Aliaksandr Valialkin Date: Mon, 19 Dec 2016 17:28:17 +0000 (+0200) Subject: cmd/vet: fix copylocks false positive on len(array) and cap(array). X-Git-Tag: go1.8rc1~51 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=d160982a2ef9193384fb3c66939db0fb37a21ba3;p=gostls13.git 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 Run-TryBot: Rob Pike TryBot-Result: Gobot Gobot --- diff --git a/src/cmd/vet/copylock.go b/src/cmd/vet/copylock.go index 31c1257a47..f3ab8e2e05 100644 --- a/src/cmd/vet/copylock.go +++ b/src/cmd/vet/copylock.go @@ -93,9 +93,11 @@ func checkCopyLocksReturnStmt(f *File, rs *ast.ReturnStmt) { // checkCopyLocksCallExpr detects lock copy in the arguments to a function call func checkCopyLocksCallExpr(f *File, ce *ast.CallExpr) { - if id, ok := ce.Fun.(*ast.Ident); ok && id.Name == "new" && f.pkg.types[id].IsBuiltin() { - // Skip 'new(Type)' for built-in 'new' - return + if id, ok := ce.Fun.(*ast.Ident); ok && f.pkg.types[id].IsBuiltin() { + switch id.Name { + case "new", "len", "cap": + return + } } for _, x := range ce.Args { if path := lockPathRhs(f, x); path != nil { diff --git a/src/cmd/vet/testdata/copylock.go b/src/cmd/vet/testdata/copylock.go index 35ed766f1d..3ce06f88d4 100644 --- a/src/cmd/vet/testdata/copylock.go +++ b/src/cmd/vet/testdata/copylock.go @@ -88,6 +88,20 @@ func BadFunc() { fmuSlice := fmuA[:] // OK } +func LenAndCapOnLockArrays() { + var a [5]sync.Mutex + aLen := len(a) // OK + aCap := cap(a) // OK + + // override 'len' and 'cap' keywords + + len := func(interface{}) {} + len(a) // ERROR "function call copies lock value: sync.Mutex" + + cap := func(interface{}) {} + cap(a) // ERROR "function call copies lock value: sync.Mutex" +} + // SyncTypesCheck checks copying of sync.* types except sync.Mutex func SyncTypesCheck() { // sync.RWMutex copying