sonar-dotnet
sonar-dotnet copied to clipboard
S2259 False Negative when using target-typed new in C#
Description
S2259 (variable is null on at least one execution path) sometimes fails to trigger when the source file includes usage of the C#9 target-typed new feature.
Repro steps
Using .NET 5:
using System;
namespace Bug
{
public static class Bug
{
static void Main()
{
// This is fine
// Random rng = new Random()
// But this causes an issue
Random rng = new();
int[] results = rng.Next(0, 2) == 1 ? null : Array.Empty<int>();
Console.WriteLine(results.Length); // S2259 false negative
}
}
}
Expected behavior
S2259 should trigger, as results may be null.
Actual behavior
S2259 fails to trigger.
Known workarounds
Using an explicit contructor (new Random() instead of new() ) does not trigger the issue.
Related information
- C#/VB.NET Plugins version: I was unable to find this information in the SonarQube UI.
- SonarQube 9.0.1.46107
- Windows 10
- Microsoft Visual Studio Enterprise 2019 Version 16.10.4
dotnet --version:5.0.302dotnet sonarscanner /?:SonarScanner for MSBuild 5.2.2
Hi @gloaming,
Thanks for reporting this. I can confirm the FN; it is a known issue.
The problem appears because we don't support yet the new C# 9 syntax elements in our symbolic execution rules.
I will close the current issue since the problem is documented on our side and we have a ticket to fix it.