dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.12k stars 4.04k forks source link

Lifted relational operators returning true should inform nullability tracking that neither operand can be null #56517

Open jnm2 opened 3 years ago

jnm2 commented 3 years ago

Version Used: 17.0.0-p4.0

These two CS8629 diagnostics should not be generated:

#nullable enable

using System;

class C
{
    void M(DateTime? a, DateTime? b)
    {
        if (a < b) // If this is true, neither variable can be null
        {
            // ⚠ CS8629 Nullable value type may be null.
            //  ↓
            _ = a.Value;
            //  ↓
            _ = b.Value;
        }
    }
}

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/expressions#lifted-operators:

For the relational operators < > <= >=, [...t]he lifted operator produces the value false if one or both operands are null.

RikkiGibson commented 3 years ago

I think this is a low impact fix since it only has the effect of changing variable states to not-null.