mirror of
https://github.com/golang/go
synced 2024-09-18 15:32:18 +00:00
cmd/vet: allow lock types inside built-in new()
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>
This commit is contained in:
parent
eb98e51563
commit
72a1b53b67
|
@ -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)
|
||||
|
|
14
src/cmd/vet/testdata/copylock.go
vendored
14
src/cmd/vet/testdata/copylock.go
vendored
|
@ -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"
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue