Skip to main content

SA0077: Enumeration type mismatch with CASE expression

Detects code positions where a CASE statement mixes enumeration values with non-enumeration types.

Justification: Mixing enumeration values and integer values in CASE constructs violates data type safety, reduces the readability of the code, and impacts maintainability. Enumeration values ensure semantic clarity. Using of raw integer values carries the risk of minor errors and makes the code more difficult to understand. Data type changes to enumerations can also destroy integer-based branches which may go unnoticed.

Importance: Low

Example 84. Example
{attribute 'qualified_only'}
{attribute 'strict'}
TYPE DUT_MyEnum :
(
	One := 1, 
	Two := 2, 
	Three := 3, 
	Four := 4
);
END_TYPE
PROGRAM PRG_Case
VAR
diVar : DINT;
xTemp : BOOL;

eMyEnum : DUT_MyEnum;
END_VAR
CASE diVar OF
    1 :
        xTemp := FALSE;
    DUT_MyEnum.Two : // SA0077
        xTemp := TRUE;
    ELSE
        xTemp := NOT xTemp;
END_CASE

CASE eMyEnum OF
    1 : // SA0077
        xTemp := FALSE;
    DUT_MyEnum.Two:
        xTemp := TRUE;
    ELSE
        xTemp := NOT xTemp;
END_CASE

Output in the Messages view:

  • sa_icon_message.png SA0077: Enum type mismatch with CASE expression