1
0
mirror of https://github.com/golang/go synced 2024-11-27 04:11:22 -07:00

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 <r@golang.org>
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Aliaksandr Valialkin 2016-12-19 19:28:17 +02:00 committed by Rob Pike
parent a3f4cc0669
commit d160982a2e
2 changed files with 19 additions and 3 deletions

View File

@ -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 {

View File

@ -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