domn1995/dunet

Suppress switch expression warning

Opened this issue · 3 comments

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();
}

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);
}

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.