When using a case statement to alternate between different values of an enumeration,
all values should be handled. This could be done explicitly by including all values in the
case arms, or implicitly by adding a default branch.
An exhaustive case statement makes it clear that all behaviour is intentionally
defined for all values, and guards against accidental omissions - for example, forgetting to
update the case statement when a new value is added to the enumeration.
Note that this rule currently ignores any case statement with a subrange
expression due to analysis constraints.
Add the missing enumeration values to the case:
type
TBeverageKind = (bvCold, bvFrozen, bvHot, bvRoomTemp);
procedure PrepareBeverage(Kind: TBeverageKind);
begin
case Kind of
bvCold, bvFrozen:
Refrigerate;
bvHot:
Microwave;
end;
end;
type
TBeverageKind = (bvCold, bvFrozen, bvHot, bvRoomTemp);
procedure PrepareBeverage(Kind: TBeverageKind);
begin
case Kind of
bvCold, bvFrozen:
Refrigerate;
bvHot:
Microwave;
bvRoomTemp:
// No action required
end;
end;
Alternatively, add an else block to implicitly handle all remaining values:
type
TBeverageKind = (bvCold, bvFrozen, bvHot, bvRoomTemp);
procedure PrepareBeverage(Kind: TBeverageKind);
begin
case Kind of
bvCold, bvFrozen:
Refrigerate;
bvHot:
Microwave;
end;
end;
type
TBeverageKind = (bvCold, bvFrozen, bvHot, bvRoomTemp);
procedure PrepareBeverage(Kind: TBeverageKind);
begin
case Kind of
bvCold, bvFrozen:
Refrigerate;
bvHot:
Microwave;
else
// No action required
end;
end;