Skip to content

Non-null value warnings for class members set in InitializeAsync are not suppressed #3423

@as3249762

Description

@as3249762

The XUnit code below has a warning that _someClass doesn't have a value set, however the value is set in the InitializerAsync() method.

public class XUnitTests : IAsyncLifetime
{
    private SomeClass _someClass; // CS8618 warning

    public Task DisposeAsync()
    {
        return Task.CompletedTask;
    }

    public Task InitializeAsync()
    {
        _someClass = new() { SomeProperty = "Hello World" };
        return Task.CompletedTask;
    }

    [Fact]
    public void Test1()
    {
        var x = 2;
    }
}

NUnit has the NUnit3002 analyser that suppresses CS8618 warnings when the class member is set in the Setup() method, so I was wondering if XUnit could implement something similar?

NUnit code:

public class NUnitTests
{
    private SomeClass _someClass; // no warning

    [SetUp]
    public async Task Setup()
    {
        _someClass = new() { SomeProperty = "SomeValue" };
    }

    [Test]
    public void Test1()
    {
        Assert.Pass();
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Analyzershttps://github.com/xunit/xunit.analyzersFeatureA request for a new featurehelp wantedA community-provided PR would be welcomed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions