There appears to be a regression with the SonarAnalyzer.CSharp c# nuget update - after upgrading from 8.44.0.52574 to 8.45.0.54064, we received many false positive errors on rule S2259:
foo(46, 32): [S2259] ‘context’ is null on at least one execution path.
code example:
public ITrackable Context(Dictionary<string, string> context)
{
if (context?.Any() == null) return this;
foreach (var (k, v) in context)
{
Properties[$"c_{k}"] = v;
}
return this;
}
In all of the false positives, the c# code is checked with a conditional ? prior to assessing the value, so there shouldn’t be an additional null check required like the case above for the “context” variable in the foreach statement.
Make sure to read this post before raising a thread here:
In this version of SonarAnalyzer.CSharp, S2259 was migrated to a new engine that was able to finally analyze methods that use newer syntax (from C# 9 and C# 10).
understood - unfortunately it’s a regression in that we can’t upgrade the nuget until this is addressed - I think the issue you’ve linked to isn’t the same problem, I believe the regression here is not interpreting the “?” condition when evaluating null on the execution paths.
thx @Pavel_Mikula - you’re right that in that case we could just do if (context == null), but looks like nullability in general is having issues - here is a different example:
if (statuses?.Count > 0)
{
// Sonar complains below about statuses is null on one execution path
var filter = (statuses.Count == 1);
// Other code....
}