ZLinq icon indicating copy to clipboard operation
ZLinq copied to clipboard

Unity + ZLinq + ErrorProne.NET.Structs cause Warnings about hidden copies

Open RichardWepner opened this issue 6 months ago • 2 comments

In a Unity project of mine I'm using ErrorProne.NET to get additional warnings for Problems that can easily be missed, as well as ZLinq. ErrorProne.NET has a specialization for struct related problems.

I suspect that this indicates a general problem also present outside of Unity, but I didn't test this.

Project setup:

  • create a Unity project (currently in use on my side: Unity 2021.3.18)
  • Add NuGetForUnity
  • Install ZLinq through NuGetForUnity
  • Install ErrorProne.NET through NuGetForUnity
    • ErrorProne.NET.CoreAnalyzer
    • ErrorProne.NET.Structs
    • ErrorProne.Unity.Analyzers

(Installing the Unity specific ZLinq package via Git URL doesn't change this.)

Whenever a ZLinq method like Select, Where, and so on is called, this causes ErrorProne to produce a Warning. Example: Expression 'Select' causes a hidden copy of a struct 'ValueEnumerable'(EPS06).

As far as I can tell, this might be caused by the this parameter being passed in by-value i.e. with neither ref nor in. I was creating a simple extension method for ValueEnumerator. The following version caused the same warning:

public static void Foo<TEnumerator, TValue>(this ValueEnumerable<TEnumerator, TValue> list) where TEnumerator : struct, IValueEnumerator<TValue>

The following didn't cause the warning:

public static void Foo<TEnumerator, TValue>(this in ValueEnumerable<TEnumerator, TValue> list) where TEnumerator : struct, IValueEnumerator<TValue>

(A ref in this situation would limit how the methods could be called, and only one version of the methods can be supplied.)

Even though the amount of data copied might not be big, I guess it might still be worth it to adjust this. This would only be a breaking change if someone was calling the methods directly instead of through a ValueEnumerator.

If however the performance impact of an in parameter (i.e. read indirections) would be bigger than copying of the data (which I doubt), suppression for this warning using a Roslyn Analyzer would be useful to anyone using ErrorProne and ZLinq.

RichardWepner avatar Jun 08 '25 11:06 RichardWepner

I tried adding ref and in modifiers many times during adjustments, but in the end, I settled on removing all of them. There must have been some issue with them. The problem with ref is obvious, but I can't remember what the issue was with in...

neuecc avatar Jun 09 '25 01:06 neuecc

I stumbled over the problem with ref myself (and had to adjust the issue text here). It would be interesting to know what problems you encountered with in, though. Are there some extension methods that modify the received ValueEnumerable?

In the meantime I'll take a look how to suppress the warnings without suppressing this warning entirely.

RichardWepner avatar Jun 09 '25 20:06 RichardWepner

  * ErrorProne.Unity.Analyzers

I cannot find this package, where were you able to get it?

Have you figured out a clean way of disabling this warning only for ZLINQ? I'm interested in using ErrorProne.Net with Unity, would love to hear if you have any insights.

wallstop avatar Jun 22 '25 00:06 wallstop