ErrorProne.NET icon indicating copy to clipboard operation
ErrorProne.NET copied to clipboard

Not seeing all diagnostics (Unity)

Open Selmar opened this issue 1 year ago • 1 comments

I don't see all expected warnings after adding the analyzers to a Unity project. For example, in the below sample code, I only get EPS01 A struct 'NonReadOnlyStruct' can be made readonly.

Perhaps I missed a step?

public struct NonReadOnlyStruct
{
	public readonly long PublicField;
	public long PublicProperty { get; }
	public void PublicMethod() { }

	private static readonly NonReadOnlyStruct _ros;
	public static void Samples(in NonReadOnlyStruct nrs)
	{
		// Ok. Public field access causes no hidden copies
		var x = nrs.PublicField;
		// Ok. No hidden copies.
		x = _ros.PublicField;

		// Hidden copy: Property access on 'in'-parameter
		x = nrs.PublicProperty;

		// Hidden copy: Method call on readonly field
		_ros.PublicMethod();

		ref readonly var local = ref nrs;

		// Hidden copy: method call on ref readonly local
		local.PublicMethod();

		// Hidden copy: method call on ref readonly return
		Local().PublicMethod();

		ref readonly NonReadOnlyStruct Local() => ref _ros;
	}
}

Selmar avatar Mar 31 '24 16:03 Selmar

It works fine in a separate project. Surely something related to Unity and perhaps out of the scope of this project?

Selmar avatar Apr 07 '24 10:04 Selmar