The result of an atomic add is assigned back to the variable it updated, which is not atomic

tx0/go:atomic-result-assigneddefaultwarningbug, concurrency

What it finds

n = atomic.AddInt64(&n, 1): the add is atomic, but the assignment that follows is an ordinary write of n, which races with other goroutines that update it and can undo their adds. The call alone already updated n.

The assignment must have one target and the call one of the Add functions of sync/atomic, whose first argument takes the address of the same variable, written the same way.

From the rule's source, atomic_result_assigned.rs.

Example from the rule's tests
package p import "sync/atomic" type stats struct{ hits int64 } func f(s *stats, n int64) int64 {	n = atomic.AddInt64(&n, 1)tx0 reports: assigning the result of atomic.AddInt64 to n is not atomic	s.hits = atomic.AddInt64(&s.hits, 1)tx0 reports: assigning the result of atomic.AddInt64 to s.hits is not atomic	atomic.AddInt64(&s.hits, 1)	total := atomic.AddInt64(&s.hits, 1)	return total}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/go:atomic-result-assigned" = "off"

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:atomic-result-assigned] reason="..."

tx0 explain tx0/go:atomic-result-assigned shows the same in the terminal. Found a false positive? Report it.