Exhaustion 1.0.0

dotnet add package Exhaustion --version 1.0.0
                    
NuGet\Install-Package Exhaustion -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Exhaustion" Version="1.0.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Exhaustion" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Exhaustion">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Exhaustion --version 1.0.0
                    
#r "nuget: Exhaustion, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Exhaustion@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Exhaustion&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Exhaustion&version=1.0.0
                    
Install as a Cake Tool

Exhaustion Analyzer

A Roslyn analyzer that enforces exhaustive pattern matching for closed type hierarchies in C#.

What it does

This analyzer ensures that switch expressions and statements are exhaustive when matching against closed type hierarchies. A closed hierarchy is a type with:

  • An abstract or record type with private/protected constructors
  • At least one derived type nested within it

Features

  • Detects missing cases in pattern matching
  • Reports warnings when a discard pattern (_) is used instead of explicit matching
  • Detects redundant default arms when all types are already matched
  • Detects redundant default arms on sealed types (leaf nodes in hierarchies)
  • Supports nested closed hierarchies
  • Works with both switch expressions and switch statements
  • Handles generic types and type parameters

Installation

Install via NuGet:

dotnet add package Exhaustion

Diagnostic ID

EXHAUSTION001: Switch expression/statement must be exhaustive for closed type hierarchies

Examples

Missing Cases

public abstract record Result<TSuccess, TError>
{
    private Result() { }

    public sealed record Ok(TSuccess Value) : Result<TSuccess, TError>;
    public sealed record Error(TError Value) : Result<TSuccess, TError>;
}

// ❌ Error: missing Error case
var message = result switch
{
    Result<int, string>.Ok(var value) => $"Success: {value}"
    // Missing: Result<int, string>.Error case
};

Redundant Default Arms

// ❌ Error: redundant default arm - all cases already matched
var message = result switch
{
    Result<int, string>.Ok(var value) => $"Success: {value}",
    Result<int, string>.Error(var error) => $"Error: {error}",
    _ => throw new InvalidOperationException() // Redundant!
};

// ✅ Correct: no default arm needed
var message = result switch
{
    Result<int, string>.Ok(var value) => $"Success: {value}",
    Result<int, string>.Error(var error) => $"Error: {error}"
};

Sealed Types

public abstract record HttpError<TError>
{
    private HttpError() { }

    public sealed record ExceptionError(Exception Exception) : HttpError<TError>;
    public sealed record ErrorResponseError(TError Body, int StatusCode) : HttpError<TError>;
}

// If httpError is already known to be ErrorResponseError:
var httpError = (HttpError<string>.ErrorResponseError)!result;

// ❌ Error: redundant pattern matching on sealed type
var (body, statusCode) = httpError switch
{
    HttpError<string>.ErrorResponseError(var b, var sc) => (b, sc),
    _ => throw new InvalidOperationException() // Redundant!
};

// ✅ Correct: direct deconstruction
var (body, statusCode) = httpError;
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Exhaustion:

Package Downloads
RestClient.Net

The safest way to make REST calls in C#. Functional HTTP client library with Result types, exhaustiveness checking, and zero exceptions.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 782 10/13/2025