Inspectopedia Help

Locks mistakenly passed by value

Reports locks that are mistakenly passed by values.

Accidentally copying a value containing a lock may cause both copies to work incorrectly. Generally, such values should be referred to through a pointer. A lock here means a type implementing sync.Locker, such as sync.Mutex or sync.WaitGroup.

Example:

type SafeInt struct { m sync.Mutex i int } func (s SafeInt) Inc() { // mutex is copied s.m.Lock() s.i++ s.m.Unlock() }

After the Add pointer quick-fix is applied:

type SafeInt struct { m sync.Mutex i int } func (s *SafeInt) Inc() { s.m.Lock() s.i++ s.m.Unlock() }

Inspection Details

Available in:

GoLand 2023.3

Plugin:

Go, 233.SNAPSHOT

Last modified: 13 July 2023