sonar-dotnet
sonar-dotnet copied to clipboard
S2259 FP: return value of ToList() is not null
Description
S2259 raises an issue telling that the return value of the ToList() is is null on at least one execution path.
Repro steps
public static long Test()
{
long sum = 0;
long[] array = new[] { 1L, 2L, 3L };
List<long> test = array.ToList();
if (test?.Count == 0)
{
// Do something
}
foreach (long number in test) // Reported Bug: 'test' is null on at least one execution path.
{
sum += number;
}
return sum;
}
Expected behavior
S2259 should not raise an issue.
Actual behavior
S2259 raises an issue for this case.
Known workarounds
Related information
- C#/VB.NET Plugins version: 8.27
@pavel-mikula-sonarsource I wouldn't solve this issue. It is a rabbit hole (there are literally millions of APIs in the runtime that never return null). The root cause here is the nonsensical test?.Count == 0
check, which implies that test can be null. If the developer thinks so once, he should not change his mind midway.
This should be low hanging to fix. And we'll have at least some support for Linq APIs also in #3290 so I'd keep it