Sonar version: 9.9 (build 65466)
This is not strictly a false positive, but I think rule 1168 should not apply if the return type of the function is explicitly nullable (see nullable reference types, introduced in C# 8.0).
Oftentimes when I write code which returns a nullable collection I mean to treat the result as a single object for which being a collection is not the feature I am interested in. In any case I think the explicit nullable declaration should be enough of an opt-out.
Some concrete examples include files treated as units (byte[]
) or the several objects in the Open XML SDK which implement IEnumerable<OpenXmlElement>
.
byte[]? TryGetFile(string fileName)
{
return File.Exists(fileName)
? File.ReadAllBytes(fileName)
: null;
}