C# ref struct (Span and ReadOnlySpan) false positive as SonarLint understands it cannot be null

When using C# new type ref struct or readonly ref struct (both nullable) SonarQube warns a false-positive on if statements against null check, saying that they will always be false.

When working either directly with the new types or, specially and most importantly, through Span<T> and ReadOnlySpan<T> you should check for null as even those types being structs, they are references and a reference can be null.

Visual Studio version: 2019 (16.0.4).
SonarLint plugin version: 4.8.

Some sample code:

public void TestMethod(ReadOnlySpan<byte> span)
{
   if (span == null) { throw new ArgumentNullException(); }
}
1 Like

thanks a lot @ALMMa , I’ve opened https://github.com/SonarSource/sonar-dotnet/issues/2496