A lock is released right after it is taken, protecting nothing

tx0/go:empty-critical-sectionrecommendedwarningbug, concurrency

What it finds

mu.Lock(); mu.Unlock(): nothing happens while the lock is held. Usually the work that the lock should protect was moved away, or defer was forgotten before Unlock. Waiting until other holders are done is a deliberate use, rare enough to mark with a suppression.

The two calls must be adjacent statements of one block, without arguments, on the same receiver written the same way, and Lock must be followed by Unlock or RLock by RUnlock.

From the rule's source, empty_critical_section.rs.

Example from the rule's tests
package p import "sync" var mu sync.RWMutex func f(m map[string]int) {	mu.Lock()	mu.Unlock()tx0 reports: mu.Unlock right after locking leaves nothing protected	mu.RLock()	mu.RUnlock()tx0 reports: mu.RUnlock right after locking leaves nothing protected	mu.Lock()	m["a"] = 1	mu.Unlock()	mu.Lock()	defer mu.Unlock()}
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 from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/go:empty-critical-section" = "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:empty-critical-section] reason="..."

tx0 explain tx0/go:empty-critical-section shows the same in the terminal. Found a false positive? Report it.