Nullability check fail when testing both equals null and not equals null.
Version Used: Visual Studio 16.6.2
Steps to Reproduce:
To any project with nullable reference types enabled, add following function:
private static string GetText(string? code)
{
if ((code == null) || ((code != null) && (code.Length < 5)))
{
return "Invalid value"; // null or too short.
}
return code;
}
I can easily reformat null and length checks to either different if statements, or just use "code?.Length < 5", but issue is that 2 conditions with or-operator doesn't detect nullability check, when just first condition detects it correctly.
Expected Behavior: No errors or warnings about possible null references.
Actual Behavior: "CS8603 Possible null reference return" on "return code;" line.
The problem here is that the "pure null" check on the right-hand-side of the || reintroduces null into the domain of code. The reasoning is that if the programmer is checking for it, it must be possible. The correct fix is to rewrite the source as
private static string GetText(string? code)
{
if (code == null || code.Length < 5)
{
return "Invalid value"; // null or too short.
}
return code;
}