Skip to content

Keyless smart enum Switch/Map default arm interpolates this, rendering the type name and tripping default-ToString analyzers #25

Closed

Description

@bsamiee

Version: Thinktecture.Runtime.Extensions 10.5.0

What happens

SmartEnumCodeGenerator emits the same unreachable arm in every Switch and Map overload:

default:
   throw new global::System.InvalidOperationException($"Unknown item '{this}'.");

A keyed smart enum gets a generated ToString() that returns the key, so the message names the item. A keyless [SmartEnum] gets no generated ToString() (see the Should_generate_keyless_class_file and Should_generate_sealed_keyless_enum_file verified outputs), so {this} calls object.ToString() and the message names the type, such as Unknown item 'MyApp.SheetStep'., never the item.

Analyzers that flag interpolation of a type without a ToString override and that report in generated code fail the build on this arm. ErrorProne.NET EPC20 ("A default ToString implementation is used for type ...") configures GeneratedCodeAnalysisFlags.Analyze | ReportDiagnostics and reports on every Switch/Map overload of every keyless smart enum, which becomes an error under TreatWarningsAsErrors. An .editorconfig severity row cannot reach these files, since the compiler applies per-path severities to source files only; the consumer's remaining options are a global analyzer config or a hand-written ToString per keyless enum.

Repro

[SmartEnum]
public sealed partial class SheetStep {
    public static readonly SheetStep Halving = new();
    public static readonly SheetStep Doubling = new();
}

With ErrorProne.NET.CoreAnalyzers 0.9.0-beta.4 referenced, the build reports EPC20 at each $"Unknown item '{this}'." in SheetStep.SmartEnum.g.cs.

Suggestion

For keyless smart enums, the generator already knows each item's name (it emits them in the KeylessSmartEnumItemMetadata identifier switch) and the index (GetItemIndex()). The arm can render one of those, for example $"Unknown item at index {index}." as the metadata switch already does, in place of interpolating this. Keyed smart enums can keep {this}, since their generated ToString() returns the key.

Activity

  1. PawelGerr commented on Oct 8, 2026

    @PawelGerr
    Owner

    Thx, a new version 10.5.1 has been released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions