dotnet/roslyn

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

Aperta

#56.517 aperta il 18 set 2021

 (1 commento) (0 reazioni) (0 assegnatari)C# (4257 fork)batch import
Area-CompilersBugFeature - Nullable Reference Typeshelp wanted

Metriche repository

Star
 (20.414 stelle)
Metriche merge PR
 (Merge medio 6g 17h) (256 PR mergiate in 30 g)

Descrizione

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.

Guida contributor