Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IDE0072: False positive in switch statement with tuples #7469

Open
BieleckiLtd opened this issue Nov 8, 2024 · 2 comments
Open

IDE0072: False positive in switch statement with tuples #7469

BieleckiLtd opened this issue Nov 8, 2024 · 2 comments

Comments

@BieleckiLtd
Copy link

Analyzer

Diagnostic ID: IDE0072: Add missing cases to switch expression

Analyzer source

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

Version: SDK 8.0.303

Describe the bug

Image

The switch expression covers all possible cases, yet the analyzer requests a default case, which would be unreachable.

Steps To Reproduce

private bool isRed = true;
private bool isRound = false;
private string result => (isRed, isRound) switch
{
    (true, true) => "Red and round",
    (true, false) => "Red but not round",
    (false, true) => "Not red but round",
    (false, false) => "Not red and not round",
};
@Thomas-Shephard
Copy link

I believe this is issue should be under the Roslyn repository as the area that causes this diagnostic is PopulateSwitch.

Although from a cursory look, I don't think this is particularly trivial issue to fix as the issue is because switches are currently only analyzed in a more detailed manner for Booleans and Enums. Tuples can contain other tuples and each tuple may contain up to 8 types.

See the AnalyzeSwitch method

@BieleckiLtd
Copy link
Author

BieleckiLtd commented Nov 9, 2024

If I add default case, analyser detects unreachable code and raises CS0162 so it is somehow breaking down this tuple.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants