Inspectopedia Help

Incorrect usage of the 'sync/atomic' package

Reports assignment statements of the form x = atomic.AddUint64(&x, 1).

Such operations are not atomic, and is a common misuse of the sync/atomic API. To make them atomic, one need to remove the assignment to use a direct call: atomic.AddUint64(&x, 1). In that case, the value of x will be updated atomically by address.

Example:

import ( "sync/atomic" ) type Counter uint64 func AtomicTests() { x := uint64(1) x = atomic.AddUint64(&x, 1) // ERROR "direct assignment to atomic value" _, x = 10, atomic.AddUint64(&x, 1) // ERROR "direct assignment to atomic value" x, _ = atomic.AddUint64(&x, 1), 10 // ERROR "direct assignment to atomic value" }

Inspection Details

By default bundled with:

GoLand 2024.1, Qodana for Go 2024.1,

Can be installed with plugin:

Go, 241.SNAPSHOT

Last modified: 29 April 2024