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

Suppress switch expression warning #27

Open
domn1995 opened this issue Jun 27, 2022 · 3 comments
Open

Suppress switch expression warning #27

domn1995 opened this issue Jun 27, 2022 · 3 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@domn1995
Copy link
Owner

domn1995 commented Jun 27, 2022

As a dunet user, when using switch expression with dunet-generated unions, I don't want to be warned about the switch expression not being exhaustive when it is provably so.

For example, the following should not emit a warning:

using static Option<int>;

Option<int> option = new Some(1);

var output = option switch
{
    Some some => some.Value.ToString(),
    None => "",
};

[Union]
public partial record Option<T>
{
    partial record Some(T Value);
    partial record None();
}
@domn1995 domn1995 added the help wanted Extra attention is needed label Jun 27, 2022
@domn1995 domn1995 changed the title Supress switch expression warning Suppress switch expression warning Jul 2, 2022
@domn1995 domn1995 pinned this issue Jul 8, 2022
@domn1995
Copy link
Owner Author

domn1995 commented Jul 21, 2022

What about creating a marker exception that tells dunet to run its own analyzer on this switch expression? That analyzer can do the proper exhaustiveness check with the knowledge that dunet unions are closed types. Something like:

Shape shape = new Shape.Circle(3.14);

var area = shape switch
{
   Circle circle => 3.14 * circle.Radius * circle.Radius,
   Rectangle rect => rect.Length * rect.Width,
   _ => Unreachable(), // Issues warning that there's no case for `Triangle`.
};

[Union]
partial record Shape
{
   partial record Circle(double Radius);
   partial record Rectangle(double Length, double Width);
   partial record Triangle(double Base, double Height);
}

@domn1995 domn1995 added the enhancement New feature or request label Aug 24, 2022
@domn1995
Copy link
Owner Author

domn1995 commented Aug 24, 2022

Stumbled upon https://github.com/shuebner/ClosedTypeHierarchyDiagnosticSuppressor while creeping on the progress of dotnet/csharplang#113

Worth looking if that solution might jive with this library.

@abatur-ms
Copy link

Also worth mentioning https://github.com/WalkerCodeRanger/ExhaustiveMatching

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants