Skip to content

bug: Sequential enum GetFormattedLength lacks bounds check for invalid enum values #50

@DrBarnabus

Description

@DrBarnabus

Description

The generated CustomEnumHelper for sequential enums uses array indexing in GetFormattedLength without validating the enum value:

public static int GetFormattedLength(CustomEnum value) => Lengths[(int)value];

An out-of-range enum value will throw a raw IndexOutOfRangeException rather than a descriptive ArgumentOutOfRangeException.

In contrast, the switch-based helper generated for non-sequential enums throws a descriptive ArgumentOutOfRangeException for invalid values. The TryFormat method in the same sequential helper does include a bounds check ((uint)value >= Names.Length), so GetFormattedLength is inconsistent with its sibling.

Reproduction

Visible in the snapshot baseline:
SourceGeneratorSnapshotTests.BasicCompositePrimaryKey_SourceOutput#UnitTests.BasicPrimaryKey.g.verified.cs (lines ~266-274)

Expected behaviour

GetFormattedLength should validate the enum value and throw ArgumentOutOfRangeException for invalid values, consistent with the non-sequential enum helper and the TryFormat guard in the same class.

Discovered via

Snapshot testing added in #48 — this is a pre-existing generator issue, not a regression.

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