Main Content

MISRA C:2012 Rule 8.12

Within an enumerator list, the value of an implicitly-specified enumeration constant shall be unique

Description

Rule Definition

Within an enumerator list, the value of an implicitly-specified enumeration constant shall be unique.

Rationale

An implicitly specified enumeration constant has a value one greater than its predecessor. If the first enumeration constant is implicitly specified, then its value is 0. An explicitly specified enumeration constant has the specified value.

If implicitly and explicitly specified constants are mixed within an enumeration list, it is possible for your program to replicate values. Such replications can be unintentional and can cause unexpected behavior.

Polyspace Implementation

The rule checker flags an enumeration if it has an implicitly specified enumeration constant with the same value as another enumeration constant.

Additional Message in Report

The constant constant1 has same value as the constant constant2.

Troubleshooting

If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

enum color1 {red_1, blue_1, green_1};   /* Compliant */
enum color2 {red_2 = 1, blue_2 = 2, green_2 = 3};       /* Compliant */
enum color3 {red_3 = 1, blue_3, green_3};     /* Compliant */
enum color4 {red_4, blue_4, green_4 = 1};     /* Non Compliant */
enum color5 {red_5 = 2, blue_5, green_5 = 2};     /* Compliant */
enum color6 {red_6 = 2, blue_6, green_6 = 2, yellow_6};     /* Non Compliant */

Compliant situations:

  • color1: All constants are implicitly specified.

  • color2: All constants are explicitly specified.

  • color3: Though there is a mix of implicit and explicit specification, all constants have unique values.

  • color5: The implicitly specified constants have unique values.

Noncompliant situations:

  • color4: The implicitly specified constant blue_4 has the same value as green_4.

  • color6: The implicitly specified constant blue_6 has the same value as yellow_6.

Check Information

Group: Declarations and Definitions
Category: Required
AGC Category: Required

Version History

Introduced in R2014b