Search code examples
cenumsmisra

MISRA-C warning during initialization enum type


Following enum defined in my code

typedef enum e_gpio_pin
{
    GPIO_PIN_1 = 1,     /*!< pin 1 */
    GPIO_PIN_2,         /*!< pin 2 */
    GPIO_PIN_3,         /*!< pin 3 */
    GPIO_PIN_4,         /*!< pin 4 */
    GPIO_PIN_5,         /*!< pin 5 */
    GPIO_PIN_6,         /*!< pin 6 */
    GPIO_PIN_7,         /*!< pin 7 */
    GPIO_PIN_8,         /*!< pin 8 */
    GPIO_PIN_9,         /*!< pin 9 */
    GPIO_PIN_10,            /*!< pin 10 */
    GPIO_PIN_FIRST = GPIO_PIN_1,    /*!< first pin */
    GPIO_PIN_LAST = GPIO_PIN_10 /*!< last pin */
} T_GPIO_PIN;

Now below line have used to initialize the variable.

 static const T_GPIO_PIN ioPin = GPIO_PIN_9;

and i got below warning

An expression value of essential type 'Essentially Enum' is assigned to an object of essential type 'Essentially Enum' comment : MISRA 10.3 (C90-2012 req.)

is anyone have idea how can i resolve this warning?


Solution

  • Since the enumeration constant GPIO_PIN_9 belongs to T_GPIO_PIN ioPin they both have the same "essentially enum" type. This is explained in MISRA-C:2012 appendix D.5 and D.6. In MISRA terms, your enum is a named enum(*) of type enum<i> and its enumeration constants are of the same type.

    You would only get problems if you tried to assign an enum some values that are enumeration constants of another enum type. I suppose the tool might get confused because you both used an enum tag and a typedef, perhaps it thinks that enum e_gpio_pin and T_GPIO_PIN ioPin are distinct enum types.

    There is nothing wrong with your code as far as MISRA is concerned. Resolve the warning by posting a bug report to your tool vendor. A work-around for the tool bug might be to remove the enum tag.


    (*) See appendix D.5

    A named enum type is an enumeration which has a tag or which is used in the definition of any object, function or type;