Fix MA0158 code fix when the field is assigned in another document - #1478
Merged
Merged
Conversation
The code fix changes the type of the field from object to System.Threading.Lock and rewrites the `new object()` assignments to `new()`, but it only looked for assignments in the document declaring the field. When the field was assigned in another document, such as another part of a partial class or a derived class, the assignment was left unchanged and the fixed code failed with CS0029. The fix now finds the references of the field with SymbolFinder and rewrites the assignments in every document of the solution.
…thcutt-e87459 # Conflicts: # tests/Meziantou.Analyzer.Test/Rules/UseSystemThreadingLockInsteadOfObjectAnalyzerTests.cs
This was referenced Sep 12, 2026
This was referenced Sep 28, 2026
Merged
Open
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.
Problem
The MA0158 code fix changes the type of a field from
objecttoSystem.Threading.Lock, then rewrites thenew object()assignments of that field tonew(). It only looked for those assignments in the document declaring the field, as it iteratededitor.OriginalRoot.DescendantNodes().OfType<AssignmentExpressionSyntax>().When the field was assigned in another document, the assignment was left unchanged and the fixed code failed with
CS0029. Two cases:partialclass whose constructor in another file does_lock = new object();private protectedfield assigned in a derived class in another fileChanges
The code fix is now solution wide:
Solutionand uses aSolutionEditor. The references of the field are found withSymbolFinder.FindReferencesAsync, so the assignments are rewritten in every document of the solution._lockorthis._lock) and the right side creates a plainobject. These are the same conditions as before, just no longer limited to one document.RegisterCodeFixesAsyncis passed to the fix, so the fix no longer re-validates anything and can no longer return an unchanged document, per the "validate before registering" guidance ofAGENTS.md.Two regression tests with multiple documents cover the two cases above, the second one using
this._lockto also cover a member access.Notes for the reviewer
dotnet run --project src/DocumentationGeneratorreports no change.docs/Rules/MA0158.mddoes not document how far the code fix reaches, so it needed no update.new object()(for example_lock = GetObject();), the code fix still changes the type and leaves that assignment failing to compile.