Skip to content

[Feature]: Suppress nullable warning #5200

Description

@splendid25

Problem Statement

Hi,

Assert.That().IsNotNull() don't suppress nullable warnings for some code after the Assert.
The following code gives a warning CS8629 "Nullable value type may be null." when the Id is guaranteed to be not null after the Assert.

Proposed Solution

[Test]
[Arguments(1)]
public async Task CheckNull(int? id)
{
    var value = new
    {
        Id = id
    };

    await Assert.That(value.Id)
                .IsNotNull();

    await Assert.That(value.Id.Value)
                .EqualTo(0);
}

Alternatives Considered

For example with an extra if ... is not null, the compiler can suppress the warning :

[Test]
[Arguments(1)]
public async Task CheckNull(int? id)
{
    var value = new
    {
        Id = id
    };

    await Assert.That(value.Id)
                .IsNotNull();

    if (value.Id is not null)
    {
        await Assert.That(value.Id.Value)
                    .EqualTo(0);
    }
}

Feature Category

Other

How important is this feature to you?

Nice to have - would improve my experience

Additional Context

No response

Contribution

  • I'm willing to submit a pull request for this feature

Metadata

Metadata

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions