nunit/nunit.analyzers

Nullable warnings for fields after updating Visual Studio

Closed this issue · 2 comments

There is a C# analyzer CS8618 that warns for uninitialized non-nullable fields. Formerly, this warning could be resolved by initializing the field within a method marked by SetUpAttribute (which functionality I assume is implemented in this library).

After updating from Visual Studio v17.11.5 to v17.12.0, the behavior no longer works. Fields are triggering CS8618 despite being initialized within a SetUp method.

namespace TestProject1;

using NUnit.Framework;

public class Tests
{
    // This line triggers a warning!
    private object _instance;

    [SetUp]
    public void Setup()
    {
        _instance = new();
    }

    [Test]
    public void Test1()
    {
        Assert.That(_instance, Is.Not.Null);
    }
}
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <IsPackable>false</IsPackable>
    <IsTestProject>true</IsTestProject>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
    <PackageReference Include="NUnit" Version="4.2.2" />
    <PackageReference Include="NUnit.Analyzers" Version="4.4.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
  </ItemGroup>

</Project>

This looks to be an upstream problem, and should be fixed in a coming version of VS: dotnet/roslyn#75399