A value that contains a lock is copied
What it finds
A sync.Mutex, sync.WaitGroup or another type of sync that must not be copied, copied by value: as a parameter or receiver, as the value of a range loop, or by an assignment. The copy has its own state, so locking it or waiting on it does nothing to the original.
A type contains such a value when it is one, or when it is declared in the file as a struct with such a field, an array of such values, or a type defined as one, so a value whose type is declared in another file is never reported. A copy by assignment is reported when the value is a variable whose type is written in its declaration or given by a composite literal, or *p for a pointer p declared so; sync and the types are resolved through the file.
The rule is in the recommended tier until its precision has been measured: the code it was checked on contains no copied locks at all.
From the rule's source, copied_lock.rs.
package p import ( "sync" s "sync") type Counter struct { mu sync.Mutex n int} type Nested struct { inner [2]Counter} type Guarded[T any] struct { sync.RWMutex value T} type Shared struct { mu *sync.Mutex wg []sync.WaitGroup} type Once s.Once func (c Counter) Value() int {tx0 reports: the receiver c copies Counter, which contains a sync.Mutex return c.n} func (c *Counter) Inc() { c.mu.Lock() defer c.mu.Unlock() c.n++} func (g Guarded[T]) Get() T {tx0 reports: the receiver g copies Guarded[T], which contains a sync.RWMutex return g.value} func (Shared) Fine() {} func wait(wg sync.WaitGroup) {tx0 reports: the parameter wg copies a sync.WaitGroup _ = func(mu s.Mutex) {}tx0 reports: the parameter mu copies a sync.Mutex _ = func(p *sync.Mutex, rest ...sync.Mutex) {} _ = func(sync.Mutex) {}tx0 reports: this parameter copies a sync.Mutex} func also(once Once) {}tx0 reports: the parameter once copies Once, which contains a sync.Once func nested(n Nested) {}tx0 reports: the parameter n copies Nested, which contains a sync.Mutex func copies(counters []Counter, byName map[string]Counter, pointers []*Counter, c *Counter) { for _, counter := range counters {tx0 reports: this range variable copies Counter, which contains a sync.Mutex _ = counter.n } for i := range counters { _ = counters[i].n } for name, counter := range byName {tx0 reports: this range variable copies Counter _, _ = name, counter } for _, counter := range pointers { _ = counter } var mu sync.Mutex other := mutx0 reports: this assignment copies a sync.Mutex _ = mu snapshot := *ctx0 reports: this assignment copies Counter, which contains a sync.Mutex fresh := Counter{} copied := freshtx0 reports: this assignment copies Counter var again Counter = freshtx0 reports: this assignment copies Counter again = *ctx0 reports: this assignment copies Counter alias := c shared := Shared{} sharedCopy := shared _, _, _, _, _, _ = other, snapshot, copied, again, alias, sharedCopy} func shadowed() { type Counter struct{ n int } var c Counter d := c _ = d} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/go:copied-lock" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/go:copied-lock] reason="..."tx0 explain tx0/go:copied-lock shows the same in the terminal. Found a false positive? Report it.