1
0
mirror of https://github.com/golang/go synced 2024-11-11 18:51:37 -07:00

cmd/vet: include function name or value in copylock message

Given
	var t struct{ lock sync.Mutex }
	var fntab []func(t)
	f(a(), b(&t), c(), fntab[0](t))

Before:
	function call copies lock value: struct{lock sync.Mutex} contains sync.Mutex

After:
	call of fntab[0] copies lock value: struct{lock sync.Mutex} contains sync.Mutex

This will make diagnosis easier when there are multiple function calls per line.

Change-Id: I9881713c5671b847b84a0df0115f57e7cba17d72
Reviewed-on: https://go-review.googlesource.com/34730
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Rob Pike 2016-12-25 08:51:21 +11:00
parent 161cd34f78
commit d698e614a2
3 changed files with 8 additions and 6 deletions

View File

@ -101,7 +101,7 @@ func checkCopyLocksCallExpr(f *File, ce *ast.CallExpr) {
}
for _, x := range ce.Args {
if path := lockPathRhs(f, x); path != nil {
f.Badf(x.Pos(), "function call copies lock value: %v", path)
f.Badf(x.Pos(), "call of %s copies lock value: %v", f.gofmt(ce.Fun), path)
}
}
}

View File

@ -67,7 +67,7 @@ func BadFunc() {
// override 'new' keyword
new := func(interface{}) {}
new(t) // ERROR "function call copies lock value: testdata.Tlock contains sync.Once contains sync.Mutex"
new(t) // ERROR "call of new copies lock value: testdata.Tlock contains sync.Once contains sync.Mutex"
// copy of array of locks
var muA [5]sync.Mutex
@ -96,10 +96,10 @@ func LenAndCapOnLockArrays() {
// override 'len' and 'cap' keywords
len := func(interface{}) {}
len(a) // ERROR "function call copies lock value: sync.Mutex"
len(a) // ERROR "call of len copies lock value: sync.Mutex"
cap := func(interface{}) {}
cap(a) // ERROR "function call copies lock value: sync.Mutex"
cap(a) // ERROR "call of cap copies lock value: sync.Mutex"
}
// SyncTypesCheck checks copying of sync.* types except sync.Mutex

View File

@ -86,8 +86,10 @@ func FuncCallInterfaceArg(f func(a int, b interface{})) {
f(1, "foo")
f(2, &t)
f(3, &sync.Mutex{})
f(4, m) // ERROR "function call copies lock value: sync.Mutex"
f(5, t) // ERROR "function call copies lock value: struct{lock sync.Mutex} contains sync.Mutex"
f(4, m) // ERROR "call of f copies lock value: sync.Mutex"
f(5, t) // ERROR "call of f copies lock value: struct{lock sync.Mutex} contains sync.Mutex"
var fntab []func(t)
fntab[0](t) // ERROR "call of fntab.0. copies lock value: struct{lock sync.Mutex} contains sync.Mutex"
}
// Returning lock via interface value