]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/vet: allow lock types inside built-in new()
authorAliaksandr Valialkin <valyala@gmail.com>
Mon, 28 Mar 2016 18:08:15 +0000 (21:08 +0300)
committerRob Pike <r@golang.org>
Wed, 30 Mar 2016 00:16:48 +0000 (00:16 +0000)
Updates #14839
Fixes #14994

Change-Id: I9bb51bad19105a17c80d690c5486e5dd007ac84a
Reviewed-on: https://go-review.googlesource.com/21222
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
src/cmd/vet/copylock.go
src/cmd/vet/testdata/copylock.go

index ac676f04b1f6eec2c798607ce939d52bdffa8c0e..35b120c5589dfe3a75a45f7ec0cd2e6f9e593a94 100644 (file)
@@ -91,8 +91,12 @@ func checkCopyLocksReturnStmt(f *File, rs *ast.ReturnStmt) {
        }
 }
 
-// checkCopyLocksCallExpr detects lock copy in function call
+// 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
+       }
        for _, x := range ce.Args {
                if path := lockPathRhs(f, x); path != nil {
                        f.Badf(x.Pos(), "function call copies lock value: %v", path)
index 2b8cec142029b10f807faf39fbf03c4c216230dc..cf56802cdbb249a05d01b9457015c12e62b687bb 100644 (file)
@@ -17,11 +17,17 @@ func OkFunc() {
        }
 
        yy := []Tlock{
-               sync.Tlock{},
-               sync.Tlock{
+               Tlock{},
+               Tlock{
                        once: sync.Once{},
                },
        }
+
+       nl := new(sync.Mutex)
+       mx := make([]sync.Mutex, 10)
+       xx := struct{ L *sync.Mutex }{
+               L: new(sync.Mutex),
+       }
 }
 
 type Tlock struct {
@@ -55,4 +61,8 @@ func BadFunc() {
                t,   // ERROR "literal copies lock value from t: testdata.Tlock contains sync.Once contains sync.Mutex"
                *tp, // ERROR "literal copies lock value from \*tp: testdata.Tlock contains sync.Once contains sync.Mutex"
        }
+
+       // override 'new' keyword
+       new := func(interface{}) {}
+       new(t) // ERROR "function call copies lock value: testdata.Tlock contains sync.Once contains sync.Mutex"
 }