JasonBock / Rocks

A mocking library based on the Compiler APIs (Roslyn + Mocks)
MIT License
248 stars 20 forks source link

Types With `[MemberNotNullWhen]` Causes Issue With Make Build Type #297

Closed JasonBock closed 9 months ago

JasonBock commented 9 months ago

To Reproduce

var code =
    """
    using Rocks;
    using System.Diagnostics.CodeAnalysis;

    [assembly: RockMake<INotNull>]

    public interface INotNull
    {
        [MemberNotNullWhen(false, nameof(Error))]
        bool IsSuccess { get; }

        string Error { get; }
    }
    """;

Expected behavior The mock is made.

Actual behavior An error occurs:

error CS8775: Member 'Error' must have a non-null value when exiting with 'false'.

Additional context This occurs because the attribute says that another member should not return null when the marked member returns false. However, because of the way a make is made, default! is always returned, and this messes with the expectation of the attribute.

This is just a diagnostic warning, so the easiest thing to do would be to wrap every make with a #pragma disable/restore pair with CS8775, so it'll never show up.

This was found on Refit.IApiResponse