Vogen icon indicating copy to clipboard operation
Vogen copied to clipboard

Possible null reference argument in call to IEqualityComparer

Open DorianGreen opened this issue 11 months ago • 3 comments

Describe the bug

I am getting the following analyzer warning:

Possible null reference argument for parameter 'y' in 'bool IEqualityComparer<EntityName>.Equals(EntityName x, EntityName y)'.

The generated code looks like this:

public global::System.Boolean Equals(EntityName? other, global::System.Collections.Generic.IEqualityComparer<EntityName> comparer)
{
    return comparer.Equals(this, other);
}

Steps to reproduce

using Vogen v6.0.0

create a value object:

[ValueObject<string>]
public sealed partial class EntityName
{
    private static Validation Validate(string input) => !string.IsNullOrWhiteSpace(input) && input.Length <= 50
        ? Validation.Ok
        : Validation.Invalid("Customer IDs must be greater than 0.");
}

run build with analyzers

Expected behaviour

I would expect a pre-null check before calling the comparer

DorianGreen avatar Jan 28 '25 14:01 DorianGreen

Thank you for the feedback. I'll take a look at this issue soon.

SteveDunn avatar Feb 03 '25 09:02 SteveDunn

Hi @DorianGreen - have you got a small repo project as I'm unable to reproduce this. The generated code looks correct : IEqualityComparer has a method signature of bool Equals(T? x, T? y); in my test project.

Maybe it's a mismatch of nullability scopes? What's your project configured for? And what's the nullability of the scope where you value object is declared?

SteveDunn avatar Feb 05 '25 19:02 SteveDunn

Hi @DorianGreen - just looking through issues now that I've got a bit of spare time. Have you got a small repro project that I can use?

SteveDunn avatar May 21 '25 17:05 SteveDunn

This was fixed and is now in 8.0.1 - thanks for the report!

SteveDunn avatar Aug 13 '25 04:08 SteveDunn