Rule 10.5 cast of enum to boolean

Moderators: misra-c, david ward

Post Reply
misra-c
Posts: 566
Joined: Thu Jan 05, 2006 1:11 pm

Rule 10.5 cast of enum to boolean

Post by misra-c » Thu Oct 09, 2014 9:36 am

Code: Select all

#include <stdbool.h>

typedef enum{ NOT_OK, OK, MAYBE } assessment;

void foo (assessment a)
{
  bool result;
  result = (bool) NOT_OK;    /* [1] compliant with MISRA C:2012 Rule 10.5? */
  /*. . . */
  result = (bool) a;         /* [2] not compliant with MISRA C:2012 Rule 10.5? */
  /*. . . */
}
The exception to Rule 10.5 permits a cast of an integer constant expression with the value 0 or 1 to a type which is defined as essentially Boolean. However the rule does not permit the cast of objects with essentially enum type to an essentially Boolean type.

Am I correct to say that [1] is compliant with rule 10.5, but [2] is not compliant?
---
Posted by and on behalf of
the MISRA C Working Group

misra-c
Posts: 566
Joined: Thu Jan 05, 2006 1:11 pm

Re: Rule 10.5 cast of enum to boolean

Post by misra-c » Thu Oct 09, 2014 9:37 am

After considering this situation, the MISRA working group has decided that the wording of the exception should be modified in a later Technical Corrigendum. The exception should only apply to expressions that are of essentially signed or essentially unsigned type. NOT_OK is an “essentially enum” type and would not be covered by the re-worded exception.

Therefore both [1] and [2] should be considered as not compliant with rule 10.5
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “8.10 The essential type model”