SergeyTeplyakov / ErrorProne.NET

Set of roslyn-based analyzers for catching common C# errors (inspired by Google's error-prone)
MIT License
869 stars 43 forks source link

Not seeing all diagnostics (Unity) #283

Open Selmar opened 3 months ago

Selmar commented 3 months ago

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 commented 2 months ago

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