SteveDunn / Vogen

A semi-opinionated library which is a source generator and a code analyser. It Source generates Value Objects
Apache License 2.0
887 stars 46 forks source link

ReSharper warning with 5.x and #nullable enable #673

Closed Steve-OH closed 1 month ago

Steve-OH commented 1 month ago

Describe the bug

Using Vogen 5.x, I'm getting a warning:

CS8764 Nullability of return type doesn't match overridden member (possibly because of nullability attributes)

The warning arises only when the value object is a record and is derived from an abstract base record. It's caused by the generated ToString() override, but I don't understand exactly what's going on. It only occurs with records, not with classes.

It can be fixed by adding this in the base record:

public sealed override string? ToString() => base.ToString();

Visual Studio 2022 17.11.4 .NET 8 target ReSharper 2024.2.4 Vogen 5.0.2

Steps to reproduce

This code will produce the warning on build:

using Vogen;

internal abstract record Base<T>
{
}

[ValueObject]
internal partial record Descendant : Base<Descendant>
{
}

[ValueObject]
internal partial record Unrelated
{
}

Expected behaviour

It would be nice to be able to avoid the warning without having to add the dummy ToString() method. I have no clue how to accomplish that, however.

SteveDunn commented 1 month ago

Thank you for the bug report. Apologies for the inconvenience. ToString has now changed to be non-nullable. Previously, it was taking the nullability of the base method (assumed to be Object.ToString()), but this wasn't the case when deriving from a record, who implicitly defines it as non-nullable. You can go from nullable-in-the-base-type to non-nullable-in-the-derived-type, but not the other way round. Since the generated ToString is never null, it is not decorated with ?. Thanks again