MISRA.ENUM.INITNon-first enumerator is explicitly initialized, but not all elements are explicitly initialized. MISRA-C Rule 9.3 (required): In an enumerator list, the "=" construct shall not be used to explicitly initialise members other than the first, unless all items are explicitly initialised.If an enumerator list is given with no explicit initialisation of members, then C allocates a sequence of integers starting at 0 for the first element and increasing by 1 for each subsequent element. An explicit initialisation of the first element, as permitted by the above rule, forces the allocation of integers to start at the given value. When adopting this approach, it is essential to ensure that the initialisation value used is small enough that no subsequent value in the list will exceed the int storage used by enumeration constants. Explicit initialisation of all items in the list, which is also permissible, prevents the mixing of automatic and manual allocation, which is error prone. However, it is then the responsibility of the programmer to ensure that all values are in the required range, and that values are not unintentionally duplicated. Exampleenum colour { red=3, blue, green, yellow=5 }; /* non compliant */ /* green and yellow represent the same value - this is duplication */ enum colour { red=3, blue=4, green=5, yellow=5 }; /* compliant */ /* green and yellow represent the same value - this is duplication */ MISRA-C++ Rule 8-5-3 (required): In an enumerator list, the = construct shall not be used to explicitly initialize members other than the first, unless all items are explicitly initialized.RationaleIf an enumerator list is given with no explicit initialization of members, then C++ allocates a sequence of integers starting at zero for the first element and increasing by one for each subsequent element. An explicit initialization of the first element, as permitted by the above rule, forces the allocation of integers to start at the given value. When adopting this approach it is essential to ensure that the initialization value used is small enough that no subsequent value in the list will exceed the int storage used by enumeration constants. Explicit initialization of all items in the list, which is also permissible, prevents the mixing of automatic and manual allocation, which is error prone. However it is then the responsibility of the developer to ensure that all values are in the required range, and that values are not unintentionally duplicated. ExampleThe following example assigns the same value to the 'green' and 'yellow' enumeration constants. It is unclear to a reviewer if this was intentional or an error. enum colour { red=3, blue, green, yellow=5 }; // Non-compliant However, if all the items are explicitly initialized, then the duplicated values are acceptable as the duplication is readily detectable by anyone reviewing the code. enum colour { red=3, blue=4, green=5, yellow=5 }; // Compliant |