Skip to content

Fix UNT0042 false positives for setters and per-iteration meshes - #521

Merged
Sebastien Lebreton (sailro) merged 1 commit into
mainfrom
fix/unt0042-false-positives
Nov 30, 2026
Merged

Sebastien Lebreton (sailro) merged 1 commit into
mainfrom
fix/unt0042-false-positives

Conversation

@sailro

Copy link
Copy Markdown
Member

Fixes #520

Checklist

  • I have read the Contribution Guide ;
  • There is an approved issue describing the change when contributing a new analyzer or suppressor ; (not applicable: this fixes an existing analyzer)
  • I have added tests that prove my fix is effective or that my feature works ;
  • I have added necessary documentation (if appropriate) ;

Short description of what this resolves:

UNT0042 reported Mesh array properties used as assignment targets, such as mesh.vertices = vertices;, although only the getters allocate. Unity's reference source confirms it: getters go through GetAllocArrayFromChannel (native AllocExtractMeshComponentFromScript returns a new System.Array), while setters pass the array as a Span<byte> to SetMeshComponentFromSpanFromScript. 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 foreach variable), which cannot be cached before the loop; the code fix then hoisted the receiver out of its scope.

Changes proposed in this pull request:

  • Ignore setter-only accesses (simple, parenthesized and deconstruction assignments); ??= is still reported as it invokes the getter. The code fix no longer rewrites setters.
  • Only report when the receiver is loop-invariant, using data flow analysis: it must not read variables declared or written inside the loop.
  • Group occurrences by syntactically equivalent receivers, so meshes[0].vertices and meshes[i].vertices are no longer treated as the same access; the analyzer and the code fix now share the same occurrence finder.
  • Add tests and document the behavior in 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; ReceiverDeclaredInOuterLoop guards that accesses in an inner loop are still reported.

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
@sailro
Sebastien Lebreton (sailro) merged commit f14d3ff into main Nov 30, 2026
6 checks passed
@sailro
Sebastien Lebreton (sailro) deleted the fix/unt0042-false-positives branch November 30, 2026 19:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UNT0042 wrongly flags the setters too

2 participants