SA0077: Enumeration type mismatch with CASE expression

Function

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

Reason

Mixing enumeration values and integer values in CASE constructs violates data type security, reduces the readability of the code and impairs maintainability. Enumeration values ensure semantic clarity. Using raw integer values carries the risk of subtle errors and makes the code more difficult to understand. Data type changes to enumerations can also destroy integer-based branches without being noticed.

Importance

Low

Sample:

Enumeration E_Sample:

TYPE E_Sample :
(
    eNull,
    eOne,
    eTwo
) DWORD;
END_TYPE

MAIN program:

PROGRAM MAIN
VAR
    nDINT   : DINT;
    eSample : E_Sample;
    bVar    : BOOL;
END_VAR
nDINT := nDINT + DINT#1;
 
CASE nDINT OF
    DINT#1:
         bVar := FALSE;
 
    E_Sample.eTwo:               // => SA0077
         bVar := TRUE;
 
ELSE
    bVar := NOT bVar;
END_CASE
 
CASE eSample OF
    DINT#1:                      // => SA0077
         bVar := FALSE;
 
    E_Sample.eTwo:
         bVar := TRUE;
 
ELSE
    bVar := NOT bVar;
END_CASE