Skip to content

IDE0072: False positive when using switch expression and pattern matching with "or" to default case #58468

@seangwright

Description

@seangwright

Version Used:

> dotnet --version
6.0.101

Steps to Reproduce:

enum Greeting
{
    Hello,
    Goodbye
};

Greeting greeting = Greeting.Hello;

string message = greeting switch
{
    Greeting.Hello => "Hey!",
    Greeting.Goodbye or _ => "Not sure what to say 🤔"
};

Expected Behavior:

Does not trigger csharp(IDE0072) which states I need to "Populate switch".

Actual Behavior:

Triggers csharp(IDE0072).

Note

This is a common pattern for me because I don't want skip handling the specific case that has the same result as the default.

If I change the enum and remove Goodbye, I should get errors, but this analyzer error wants me to either duplicate the result value ("Not sure what to say 🤔") which means I have to make future changes in 2 places, or remove Greeting.Goodbye from the expression and have the default handle it, which removes an intentional reference to a specific value.

This is slightly related to this issue #50982

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions