Commit d160982a authored by Aliaksandr Valialkin's avatar Aliaksandr Valialkin Committed by Rob Pike

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/34630Reviewed-by: default avatarRob Pike <r@golang.org>
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent a3f4cc06
...@@ -93,9 +93,11 @@ func checkCopyLocksReturnStmt(f *File, rs *ast.ReturnStmt) { ...@@ -93,9 +93,11 @@ func checkCopyLocksReturnStmt(f *File, rs *ast.ReturnStmt) {
// checkCopyLocksCallExpr detects lock copy in the arguments to a function call // checkCopyLocksCallExpr detects lock copy in the arguments to a function call
func checkCopyLocksCallExpr(f *File, ce *ast.CallExpr) { func checkCopyLocksCallExpr(f *File, ce *ast.CallExpr) {
if id, ok := ce.Fun.(*ast.Ident); ok && id.Name == "new" && f.pkg.types[id].IsBuiltin() { if id, ok := ce.Fun.(*ast.Ident); ok && f.pkg.types[id].IsBuiltin() {
// Skip 'new(Type)' for built-in 'new' switch id.Name {
return case "new", "len", "cap":
return
}
} }
for _, x := range ce.Args { for _, x := range ce.Args {
if path := lockPathRhs(f, x); path != nil { if path := lockPathRhs(f, x); path != nil {
......
...@@ -88,6 +88,20 @@ func BadFunc() { ...@@ -88,6 +88,20 @@ func BadFunc() {
fmuSlice := fmuA[:] // OK 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 // SyncTypesCheck checks copying of sync.* types except sync.Mutex
func SyncTypesCheck() { func SyncTypesCheck() {
// sync.RWMutex copying // sync.RWMutex copying
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment