ExhaustiveMatching icon indicating copy to clipboard operation
ExhaustiveMatching copied to clipboard

Add support for exhaustive matching on records

Open abiratur opened this issue 2 years ago • 1 comments

The analyzer does not handle record classes. for example :

    [Closed(typeof(UserNotFound))]
    public abstract record Response
    {
        public record UserNotFound : Response;

        public record Success : Response;
    }

    class Controller
    {
        public int Get(Response response)
        {
            return response switch
            {
                Response.UserNotFound _ => -1,
                _ => throw ExhaustiveMatch.Failed(response)
            };
        }
    }

The analyzer doesn't detect that the "Success" class is needed to be added to the Closed attribute. The analyzer doesn't detect that the "Success" clause needs to be handled in the Get() method

Thanks for this library, really appreciate the effort !

abiratur avatar Mar 27 '22 09:03 abiratur

If you don't provide all branchs in the ClosedAttribute, how should the analyzer know that Success is the only other subclass of the record. See my issue #44 on how the analyzer can detect the situation even for records without the attribute if the constructor is private. I have also provided a pull request and will try to use the modified version of the analyzer already in some of our projects.

csharper2010 avatar Apr 05 '22 07:04 csharper2010