fix(manualmutexunlock): distinguish struct instances sharing a mutex field - #41383
Merged
Conversation
…uct instances sharing a mutex field Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix manualmutexunlock false negative issue
fix(manualmutexunlock): distinguish struct instances sharing a mutex field
Jun 25, 2026
pelikhan
marked this pull request as ready for review
June 25, 2026 05:52
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a correctness issue in the manualmutexunlock analyzer where mutex state tracked for struct field selectors (e.g. a.mu) could collide across different struct instances because the key was derived from the shared field declaration object. The update introduces an instance-aware key so violations are reported reliably.
Changes:
- Replace the mutex tracking map key from
types.Objectto a compositemutexKey{base, field}to distinguisha.muvsb.mu. - Update receiver/call extraction helpers to return
(mutexKey, bool)and wire through all call sites. - Add new
testdatacases covering two-guard scenarios to prevent regressions.
Show a summary per file
| File | Description |
|---|---|
| pkg/linters/manualmutexunlock/manualmutexunlock.go | Switch tracking to an instance-aware key and update receiver resolution helpers. |
| pkg/linters/manualmutexunlock/testdata/src/manualmutexunlock/manualmutexunlock.go | Add regression tests ensuring separate struct instances don’t clobber each other’s mutex state. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 2/2 changed files
- Comments generated: 1
Comment on lines
+202
to
217
| fieldObj := sel.Obj() | ||
| // When the base is a plain identifier (the common case: `a.mu`), | ||
| // build a composite key (base var, field) so that distinct | ||
| // instances of the same struct type are tracked independently. | ||
| baseIdent, ok := r.X.(*ast.Ident) | ||
| if !ok { | ||
| // Fall back for non-ident base expressions (e.g. `getGuard().mu`): | ||
| // use the field object alone as the key, matching prior behaviour. | ||
| return mutexKey{base: fieldObj}, true | ||
| } | ||
| baseObj := pass.TypesInfo.ObjectOf(baseIdent) | ||
| if baseObj == nil { | ||
| return mutexKey{base: fieldObj}, true | ||
| } | ||
| return mutexKey{base: baseObj, field: fieldObj}, true | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
manualmutexunlocktracked mutex state inmap[types.Object]*mutexVarState, keyed viapass.TypesInfo.Selections[r].Obj()for field selectors. That returns the field declaration — one*types.Varshared across all instances of the struct — soa.muandb.mucollide on the same key and clobber each other's state. A deferred unlock onb.musilently masks a manual-unlock violation ona.mu.Changes
mutexKeycomposite type{base, field types.Object}replaces baretypes.Objectas the map key.mu) →{base: ObjectOf(mu), field: nil}— unchanged semanticsa.mu) →{base: ObjectOf(a), field: fieldObj}— instance-distinctgetGuard().mu) →{base: fieldObj, field: nil}— fallback, prior behaviour preservedgetMutexReceiverObj→getMutexReceiverKeyreturns(mutexKey, bool)instead oftypes.Object.getLockCallObj/getUnlockCallObj→getLockCallKey/getUnlockCallKeyupdated accordingly; all call sites ininspectMutexNodeupdated.BadTwoGuardsManualFirst/BadTwoGuardsManualSecondassert the violation is flagged regardless of statement order;GoodTwoGuardsBothDeferredconfirms no false positive when both are deferred.