Skip to main content

SA0058: Operations on enumeration variables

Detects operations on variables of the enumeration data type Assignments are permitted.

Justification: Enumerations should not be used as ordinary integer values. Alternatively, an alias data type could be defined or a subrange type could be used.

Importance: Medium

Exception: If an enumeration is tagged with the pragma {attribute 'strict'}, then the compiler already reports this kind of operation.

If an enumeration is declared as a flag by the pragma {attribute 'flags'}, then an error is not issued for AND, OR, NOT, or oder XOR operations.

Example 68. Example
TYPE My_Enum :
(
    red := 1, blue := 2, green := 3, black := 4
);
END_TYPE
PROGRAM PLC_PRG
VAR
    iTemp1 : INT;
    abc : My_Enum;
END_VAR
iTemp1 := iTemp1 + INT#1;
abc := My_Enum.red;    // OK
iTemp1 := My_Enum.black / My_Enum.blue;    //  SA0058
iTemp1 := My_Enum.green / My_Enum.red;    //  SA0058

Output in the Messages view:

  • sa_icon_message.png SA0058: Operations on enumeration variables



Example 69. Example with a pragma {attribute 'flags'}
{attribute 'flags'}  // declaring the enumeration as a "flag"
TYPE Flags :
(
    Unknown := 16#00000001,
    Stopped := 16#00000002,
    Running := 16#00000004
) DWORD;
END_TYPE
PROGRAM PLC_PRG
VAR
 iTemp1 : INT;
 abc : Flags;
        batate : BYTE;
        dwFlags : DWORD;
        dwState : DWORD;
END_VAR
// OK for the following
IF (dwFlags AND Flags.Unknown) <> DWORD#0 THEN
        dwState := dwState AND Flags.Unknown;
    ELSIF (dwFlags OR Flags.Stopped) <> DWORD#0 THEN
        dwState := dwState OR Flags.Running;
END_IF