Skip to content

New rule: Case statements should be exhaustive #98

@fourls

Description

@fourls

Prerequisites

  • This rule has not already been suggested.
  • This should be a new rule, not an improvement to an existing rule.
  • This rule would be generally useful, not specific to my code or setup.

Suggested rule title

Case statements should be exhaustive

Rule description

Enums are commonly used in case statements to conditionally perform code. This rule would pick up cases where the options within the case are not exhaustive, i.e. some values are missing and a default is not provided.

For example, the case below could be fixed by adding an meBaz or else option.

type
  TMyEnum = (meFoo, meBar, meBaz);

procedure DoThing(EnumValue: TMyEnum);
begin
  case EnumValue of // Noncompliant - missing meBaz
    meFoo: WriteLn('Foo');
    meBar: WriteLn('Bar');
  end;
end;

Rationale

In cases where control flow is being alternated by enum values, a valid path should exist for all possible values. This is generally accepted as good practice.

In addition, any new values that are added to an enum should be handled in all existing alternations to prevent unexpected behaviour. Many programming languages enforce exhaustiveness of switch/case statements at compile time because of this - this rule helps enforce this for Delphi.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew feature or requestruleImprovements or additions to rules

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions