Skip to content

Unexpected nullability warnings for a pattern #82412

@AlekseyTs

Description

@AlekseyTs
        [Fact]
        public void Test()
        {
            var src2 = @"
#nullable enable

class S1
{
    public object? Value => throw null!;
}

class S2
{
    public object Value => throw null!;
}

class Program
{
    static void Test2(S1 s)
    {
        _ = s.Value;
        if (s is null or { Value: null })
        {
#line 1000
            _ = s switch { { Value: {} } => 1 };
        }
        else
        {
#line 2000
            _ = s switch { { Value: {} } => 1 };
        }
    } 

    static void Test4(S2 s)
    {
        _ = s.Value;
        if (s is null or { Value: null })
        {
#line 3000
            _ = s switch { { Value: {} } => 1 };
        }
        else
        {
#line 4000
            _ = s switch { { Value: {} } => 1 };
        }
    } 
}
";
            var comp2 = CreateCompilation(src2);

            comp2.VerifyDiagnostics(
                // (1000,19): warning CS8655: The switch expression does not handle some null inputs (it is not exhaustive). For example, the pattern 'null' is not covered.
                //             _ = s switch { { Value: {} } => 1 };
                Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustiveForNull, "switch").WithArguments("null").WithLocation(1000, 19),
                // (2000,19): warning CS8655: The switch expression does not handle some null inputs (it is not exhaustive). For example, the pattern 'null' is not covered.
                //             _ = s switch { { Value: {} } => 1 };
                Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustiveForNull, "switch").WithArguments("null").WithLocation(2000, 19),
                // (3000,19): warning CS8655: The switch expression does not handle some null inputs (it is not exhaustive). For example, the pattern 'null' is not covered.
                //             _ = s switch { { Value: {} } => 1 };
                Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustiveForNull, "switch").WithArguments("null").WithLocation(3000, 19),
                // (4000,19): warning CS8655: The switch expression does not handle some null inputs (it is not exhaustive). For example, the pattern 'null' is not covered.
                //             _ = s switch { { Value: {} } => 1 };
                Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustiveForNull, "switch").WithArguments("null").WithLocation(4000, 19)
                );
        }

I think warnings on lines 2000 and 4000 are unexpected.

Metadata

Metadata

Assignees

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions