Repository navigation
Fix UNT0042 false positives for setters and per-iteration meshes - #521
Merged
Merged
Conversation
Mesh array property setters do not allocate (the array is passed as a Span to native code), so only getters are reported now. The code fix no longer rewrites setters to the cached local. Accesses on a mesh that changes with each iteration (foreach variable, locals declared or assigned in the loop, meshes[i]) are no longer reported, as they cannot be cached before the loop. Occurrences also require equivalent receivers, so meshes[0].vertices and meshes[i].vertices are no longer treated as the same access. Fixes #520 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: b3f0f070-0d78-4e38-9909-19a4d7af65a0
Jb Evain (jbevain)
approved these changes
Nov 30, 2026
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.
Fixes #520
Checklist
Short description of what this resolves:
UNT0042 reported
Mesharray properties used as assignment targets, such asmesh.vertices = vertices;, although only the getters allocate. Unity's reference source confirms it: getters go throughGetAllocArrayFromChannel(nativeAllocExtractMeshComponentFromScriptreturns a newSystem.Array), while setters pass the array as aSpan<byte>toSetMeshComponentFromSpanFromScript. The code fix also rewrote setters to assign the cached local, so the mesh was no longer updated.UNT0042 also reported accesses on a mesh that changes with each iteration (for example a
foreachvariable), which cannot be cached before the loop; the code fix then hoisted the receiver out of its scope.Changes proposed in this pull request:
??=is still reported as it invokes the getter. The code fix no longer rewrites setters.meshes[0].verticesandmeshes[i].verticesare no longer treated as the same access; the analyzer and the code fix now share the same occurrence finder.doc/UNT0042.md.Validation
dotnet build .\src\Microsoft.Unity.Analyzers.slnx -v q -nologo: succeeded (0 warnings, 0 errors).dotnet .\src\Microsoft.Unity.Analyzers.Tests\bin\Debug\net10.0\Microsoft.Unity.Analyzers.Tests.dll --filter-class Microsoft.Unity.Analyzers.Tests.MeshArrayPropertyInLoopTests: passed (26/26). 7 of the 8 new tests fail against the previous implementation;ReceiverDeclaredInOuterLoopguards that accesses in an inner loop are still reported.