-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Open
Labels
status: help wantedExtra attention is neededExtra attention is neededtype: enhancementNew feature or requestNew feature or request
Description
Description
Currently locked access to struct members inside a loop over an iterator is not detected correctly.
Example:
package main
import (
"strings"
"sync"
)
type Entries struct {
mu sync.Mutex
// +checklocks:mu
entries []string
}
func (e *Entries) Add(s string) {
e.mu.Lock()
defer e.mu.Unlock()
for item := range strings.SplitSeq(s, " ") {
e.entries = append(e.entries, item)
}
}
Error:
go vet -vettool=$HOME/go/bin/checklocks iter_seq_checklocks.go
# command-line-arguments
./iter_seq_checklocks.go:19:24: invalid field access, mu (&({freevar:e}.mu)) must be locked when accessing entries (locks: no locks held)
./iter_seq_checklocks.go:19:5: invalid field access, mu (&({freevar:e}.mu)) must be locked when accessing entries (locks: no locks held)
This works if the lock is acquired inside the loop.
func (e *Entries) Add(s string) {
for item := range strings.SplitSeq(s, " ") {
e.mu.Lock()
e.entries = append(e.entries, item)
e.mu.Unlock()
}
}
Is this feature related to a specific bug?
No response
Do you have a specific solution in mind?
No response
Metadata
Metadata
Assignees
Labels
status: help wantedExtra attention is neededExtra attention is neededtype: enhancementNew feature or requestNew feature or request