Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFCCCC
langcpp

enum enum_type {
  E_A,
  E_B
};

int int_var = -1;
enum_type enum_var = static_cast<enum_type>(int_var);

if (enum_var < E_A) {
  // handle error
}

...

Code Block
bgColor#ccccff
langcpp

enum enum_type {
  E_A,
  E_B
};

int int_var = -1;

if (int_var < E_A || int_var > E_B) {
  // handle error
}

enum_type enum_var = static_cast<enum_type>(int_var);

...

Code Block
bgColor#ffcccc
langcpp

enum enum_type {
  E_A,
  E_B
};

int int_var = 5;

switch (static_cast<enum_type>(int_var)) {
  case E_A:
    // some action A
  case E_B:
    // some action B
  default:
    // handle error
}

...

Code Block
bgColor#ffcccc
langcpp

enum enum_type {
  E_A = 1,
  E_B,
  E_C,
  E_D,
  E_E,
  E_F,
  E_G
};

for(enum_type enum_var = E_A; enum_var <= E_G; enum_var = static_cast<enum_var>(enum_var+1)) {
  // some action
}

...

Code Block
bgColor#ccccff
langcpp

enum enum_type {
  E_A = 1,
  E_B,
  E_C,
  E_D,
  E_E,
  E_F,
  E_G
};

for(int i = E_A; i <= E_G; i = i+1) {
  // some action
}

...

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

INT36-CPP

high

probable

high

P6

L2

Automated Detection

Tool

Version

Checker

Description

 PRQA QA-C++

 
Include Page
PRQA QA-C++_v
PRQA QA-C++_v
3013 

 

Bibliography

[Becker 2009] Section 7.2, "Enumeration declarations"

...

INT35INT18-CPP. Evaluate integer expressions in a larger size before comparing or assigning to that size      04. Integers (INT)      05. Floating Point Arithmetic (FLP)