SA0058: Operations of enumeration variables

Function

Determines operations on variables of type enumeration. Assignments are permitted.

Reason

Enumerations should not be used as normal integer values. Alternatively, an alias data type can be defined or a subrange type can be used.

Exception

If an enumeration is marked with the attribute {attribute 'strict'}, the compiler reports such an operation.

If an enumeration is declared as a flag via the pragma attribute {attribute 'flags'}, no SA0058 error is issued for operations with AND, OR, NOT, XOR.

Importance

Medium

Sample 1:

Enumeration E_Color:

TYPE E_Color :
(
    eRed   := 1,
    eBlue  := 2,
    eGreen := 3
);
END_TYPE

MAIN program:

PROGRAM MAIN
VAR
    nVar   : INT;
    eColor : E_Color;
END_VAR
eColor := E_Color.eGreen;                           // no error
eColor := E_Color.eGreen + 1;                       // => SA0058
nVar   := E_Color.eBlue / 2;                        // => SA0058
nVar   := E_Color.eGreen + E_Color.eRed;            // => SA0058

Sample 2:

Enumeration E_State with attribute 'flags':

{attribute 'flags'}
TYPE E_State :
(
    eUnknown := 16#00000001,
    eStopped := 16#00000002,
    eRunning := 16#00000004
) DWORD;
END_TYPE

MAIN program:

PROGRAM MAIN
VAR
    nFlags : DWORD;
    nState : DWORD;
END_VAR
IF (nFlags AND E_State.eUnknown) <> DWORD#0 THEN    // no error
    nState := nState AND E_State.eUnknown;          // no error
 
ELSIF (nFlags OR E_State.eStopped) <> DWORD#0 THEN  // no error
    nState := nState OR E_State.eRunning;           // no error
END_IF