Commit 9a84e527 authored by Wei Congrui's avatar Wei Congrui Committed by Robert Griesemer

cmd/vet: fix copylocks false positive on unsafe.Sizeof(mutex)

Fixes #21800

Change-Id: I6c61d3543f28e9951b2a219b3c7298077b38f29e
Reviewed-on: https://go-review.googlesource.com/66210
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarRobert Griesemer <gri@golang.org>
parent 2c1d2e06
......@@ -93,9 +93,16 @@ 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 && f.pkg.types[id].IsBuiltin() {
switch id.Name {
case "new", "len", "cap":
var id *ast.Ident
switch fun := ce.Fun.(type) {
case *ast.Ident:
id = fun
case *ast.SelectorExpr:
id = fun.Sel
}
if fun, ok := f.pkg.uses[id].(*types.Builtin); ok {
switch fun.Name() {
case "new", "len", "cap", "Sizeof":
return
}
}
......
......@@ -3,6 +3,9 @@ package testdata
import (
"sync"
"sync/atomic"
"unsafe"
. "unsafe"
unsafe1 "unsafe"
)
func OkFunc() {
......@@ -102,6 +105,17 @@ func LenAndCapOnLockArrays() {
cap(a) // ERROR "call of cap copies lock value: sync.Mutex"
}
func SizeofMutex() {
var mu sync.Mutex
unsafe.Sizeof(mu) // OK
unsafe1.Sizeof(mu) // OK
Sizeof(mu) // OK
unsafe := struct{ Sizeof func(interface{}) }{}
unsafe.Sizeof(mu) // ERROR "call of unsafe.Sizeof copies lock value: sync.Mutex"
Sizeof := func(interface{}) {}
Sizeof(mu) // ERROR "call of Sizeof copies lock value: sync.Mutex"
}
// SyncTypesCheck checks copying of sync.* types except sync.Mutex
func SyncTypesCheck() {
// 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