V2521. MISRA. Only the first member of enumerator list should be explicitly initialized, unless all members are explicitly initialized.


This diagnostic rule is based on the software development guidelines developed by MISRA (Motor Industry Software Reliability Association).

This diagnostic rule is relevant only to C++ programs.

The analyzer has detected an enumeration whose members are initialized in a mixed way (explicitly and implicitly). This may result in duplicating their values.

Here is an example of incorrect code:

enum Suits 
{ 
  SUITS_SPADES = 1,
  SUITS_CLUBS,
  SUITS_DIAMONDS,  
  SUITS_HEARTS,
  SUITS_UNKNOWN = 4 
};

The way the elements are initialized here results in assigning the same value to the members 'SUITS_HEARTS' and 'SUITS_UNKNOWN'. It is not clear if this was done on purpose or by mistake.

The warning is not issued in the following cases:

  • only the first enumeration element is initialized explicitly;
  • all enumeration elements are initialized implicitly;
  • all enumeration elements are initialized explicitly.

Bugs Found

Checked Projects
334
Collected Errors
12 668