-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go/analysis/passes/copylock: find locks via type parameters
Add support for finding incorrect usage of locks via type parameters. It would probably be fine not to do this, since using locks explicitly via type parameters should be exceedingly rare. However, it was straightforward to add, and is consistent with other analyzers. Updates golang/go#48704 Change-Id: I329a2fa9f11c6bbb491d49afde7fabce8299cbdf Reviewed-on: https://go-review.googlesource.com/c/tools/+/360234 Trust: Robert Findley <[email protected]> Run-TryBot: Robert Findley <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Tim King <[email protected]>
- Loading branch information
Showing
3 changed files
with
86 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
go/analysis/passes/copylock/testdata/src/typeparams/typeparams.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright 2021 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package typeparams | ||
|
||
import "sync" | ||
|
||
func OkFunc1[Struct ~*struct{ mu sync.Mutex }](s Struct) { | ||
} | ||
|
||
func BadFunc1[Struct ~struct{ mu sync.Mutex }](s Struct) { // want `passes lock by value: .*Struct contains ~struct{mu sync.Mutex}` | ||
} | ||
|
||
func OkFunc2[MutexPtr *sync.Mutex](m MutexPtr) { | ||
var x *MutexPtr | ||
p := x | ||
var y MutexPtr | ||
p = &y | ||
*p = *x | ||
|
||
var mus []MutexPtr | ||
|
||
for _, _ = range mus { | ||
} | ||
} | ||
|
||
func BadFunc2[Mutex sync.Mutex](m Mutex) { // want `passes lock by value: .*Mutex contains sync.Mutex` | ||
var x *Mutex | ||
p := x | ||
var y Mutex | ||
p = &y | ||
*p = *x // want `assignment copies lock value to \*p: .*Mutex contains sync.Mutex` | ||
|
||
var mus []Mutex | ||
|
||
for _, _ = range mus { | ||
} | ||
} | ||
|
||
func ApproximationError[Mutex interface { | ||
~sync.Mutex | ||
M() | ||
}](m Mutex) { // want `passes lock by value: .*Mutex contains ~sync.Mutex` | ||
} |