dotnet / roslyn-analyzers

MIT License
1.55k stars 460 forks source link

CA1508: False positive with nullable in switch expression default case #7337

Open AnthonyMastrean opened 1 week ago

AnthonyMastrean commented 1 week ago

Analyzer

Diagnostic ID: CA1508: Avoid dead conditional code

Analyzer source

SDK: Built-in CA analyzers in .NET 5 SDK or later

Version: SDK 5.0.100

Describe the bug

The analyzer warns on the below code despite no condition that would indicate value is not null.

Steps To Reproduce

static class Ca1508FalsePositive
{
    static void Example(string? value)
    {
        _ = value switch
        {
            _ => value?.Trim(),
        };
    }
}

Expected behavior

No warning!

Actual behavior

Warning!

'value' is never 'null'. Remove or refactor the condition(s) to avoid dead code.

Additional context

Might be related to #6893