]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/vet: fix copylocks false positive on len(array) and cap(array).
authorAliaksandr Valialkin <valyala@gmail.com>
Mon, 19 Dec 2016 17:28:17 +0000 (19:28 +0200)
committerRob Pike <r@golang.org>
Sat, 24 Dec 2016 21:41:34 +0000 (21:41 +0000)
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>

src/cmd/vet/copylock.go
src/cmd/vet/testdata/copylock.go

index 31c1257a47017401a53811c7d874608bf85f0de2..f3ab8e2e055306d4bbe641559b662e711f3d5848 100644 (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 {
index 35ed766f1da7e26010bcd7d3285c534c5ebf8b83..3ce06f88d421af7253f2c8cafada9de519d9ff4a 100644 (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