Skip to main content
Source: Al0103ClosedTypeHierarchySwitchAnalyzer.cs

Description

When switching over a sealed type hierarchy (abstract class or interface with all implementations in the same assembly), the switch should cover all derived types to catch missing cases at compile time.

Bad Code

public abstract record Shape;
public record Circle : Shape;
public record Square : Shape;

string Describe(Shape s) => s switch
{
    Circle c => "circle",
    _ => "unknown" // Misses Square
};

Good Code

string Describe(Shape s) => s switch
{
    Circle c => "circle",
    Square sq => "square",
    _ => throw new UnreachableException()
};

Properties

  • Category: Design
  • Severity: Warning
  • Enabled by default: True
  • Code fix available: True

Configuration

dotnet_diagnostic.AL0103.severity = warning