Commit 003a68bc authored by Ian Lance Taylor's avatar Ian Lance Taylor

cmd/vet: remove copylock warning about result types and calls

Don't issue a copylock warning about a result type; the function may
return a composite literal with a zero value, which is OK.

Don't issue a copylock warning about a function call on the RHS, or an
indirection of a function call; the function may return a composite
literal with a zero value, which is OK.

Updates #16227.

Change-Id: I94f0e066bbfbca5d4f8ba96106210083e36694a2
Reviewed-on: https://go-review.googlesource.com/24711Reviewed-by: default avatarJosh Bleecher Snyder <josharian@gmail.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: default avatarRob Pike <r@golang.org>
parent 878e002b
...@@ -125,14 +125,10 @@ func checkCopyLocksFunc(f *File, name string, recv *ast.FieldList, typ *ast.Func ...@@ -125,14 +125,10 @@ func checkCopyLocksFunc(f *File, name string, recv *ast.FieldList, typ *ast.Func
} }
} }
if typ.Results != nil { // Don't check typ.Results. If T has a Lock field it's OK to write
for _, field := range typ.Results.List { // return T{}
expr := field.Type // because that is returning the zero value. Leave result checking
if path := lockPath(f.pkg.typesPkg, f.pkg.types[expr].Type); path != nil { // to the return statement.
f.Badf(expr.Pos(), "%s returns lock by value: %v", name, path)
}
}
}
} }
// checkCopyLocksRange checks whether a range statement // checkCopyLocksRange checks whether a range statement
...@@ -194,6 +190,16 @@ func lockPathRhs(f *File, x ast.Expr) typePath { ...@@ -194,6 +190,16 @@ func lockPathRhs(f *File, x ast.Expr) typePath {
if _, ok := x.(*ast.CompositeLit); ok { if _, ok := x.(*ast.CompositeLit); ok {
return nil return nil
} }
if _, ok := x.(*ast.CallExpr); ok {
// A call may return a zero value.
return nil
}
if star, ok := x.(*ast.StarExpr); ok {
if _, ok := star.X.(*ast.CallExpr); ok {
// A call may return a pointer to a zero value.
return nil
}
}
return lockPath(f.pkg.typesPkg, f.pkg.types[x].Type) return lockPath(f.pkg.typesPkg, f.pkg.types[x].Type)
} }
......
...@@ -12,7 +12,7 @@ import "sync" ...@@ -12,7 +12,7 @@ import "sync"
func OkFunc(*sync.Mutex) {} func OkFunc(*sync.Mutex) {}
func BadFunc(sync.Mutex) {} // ERROR "BadFunc passes lock by value: sync.Mutex" func BadFunc(sync.Mutex) {} // ERROR "BadFunc passes lock by value: sync.Mutex"
func OkRet() *sync.Mutex {} func OkRet() *sync.Mutex {}
func BadRet() sync.Mutex {} // ERROR "BadRet returns lock by value: sync.Mutex" func BadRet() sync.Mutex {} // Don't warn about results
var ( var (
OkClosure = func(*sync.Mutex) {} OkClosure = func(*sync.Mutex) {}
...@@ -28,7 +28,7 @@ func (EmbeddedRWMutex) BadMeth() {} // ERROR "BadMeth passes lock by value: test ...@@ -28,7 +28,7 @@ func (EmbeddedRWMutex) BadMeth() {} // ERROR "BadMeth passes lock by value: test
func OkFunc(e *EmbeddedRWMutex) {} func OkFunc(e *EmbeddedRWMutex) {}
func BadFunc(EmbeddedRWMutex) {} // ERROR "BadFunc passes lock by value: testdata.EmbeddedRWMutex" func BadFunc(EmbeddedRWMutex) {} // ERROR "BadFunc passes lock by value: testdata.EmbeddedRWMutex"
func OkRet() *EmbeddedRWMutex {} func OkRet() *EmbeddedRWMutex {}
func BadRet() EmbeddedRWMutex {} // ERROR "BadRet returns lock by value: testdata.EmbeddedRWMutex" func BadRet() EmbeddedRWMutex {} // Don't warn about results
type FieldMutex struct { type FieldMutex struct {
s sync.Mutex s sync.Mutex
...@@ -107,6 +107,14 @@ func ReturnViaInterface(x int) (int, interface{}) { ...@@ -107,6 +107,14 @@ func ReturnViaInterface(x int) (int, interface{}) {
} }
} }
// Some cases that we don't warn about.
func AcceptedCases() {
x := EmbeddedRwMutex{} // composite literal on RHS is OK (#16227)
x = BadRet() // function call on RHS is OK (#16227)
x = *OKRet() // indirection of function call on RHS is OK (#16227)
}
// TODO: Unfortunate cases // TODO: Unfortunate cases
// Non-ideal error message: // Non-ideal error message:
......
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