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.


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

By default bundled with:

GoLand 2024.1, Qodana for Go 2024.1,

Can be installed with plugin:


Last modified: 29 April 2024