MISRA Discussion Forums
Rule 14.4 and the explanation is not enough to understand why - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4)
+--- Forum: MISRA C:2012 and MISRA C:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=21)
+---- Forum: 8.14 Control statement expressions (https://forum.misra.org.uk/forumdisplay.php?fid=169)
+---- Thread: Rule 14.4 and the explanation is not enough to understand why (/showthread.php?tid=1344)



Rule 14.4 and the explanation is not enough to understand why - xiangke - 06-06-2017

MISRA C 2012 guideline says:
Strong typing requires the controlling expression of an if statement or iteration-statement to have
essentially Boolean type.

For example:
typedef unsigned short tudtKalEvents;
#define udtSTOP_EVENT (tudtKalEvents)0x0001;
Code:
if(udtEvents & (tudtKalEvents) udtSTOP_EVENT)
  {
        vidStop();
  }
I think the code is ok, I use the & statement as the conditional expression. As we know, the value is not equal to zero, the condition shall be judged to be true,
why We must use the boolean type?


Re: Rule 14.4 and the explanation is not enough to understand why - dg1980 - 08-06-2017

Just to be clear, you don't need to use the boolean type, but you need to form a boolean expression, e.g.
Code:
if ((udtEvents & (tudtKalEvents)udtSTOP_EVENT) != 0u)
{
  vidStop();
}
It´s all about avoiding implicit conversion as much as possible.


Re: Rule 14.4 and the explanation is not enough to understand why - misra-c - 14-06-2017

MISRA-C introduces a strong typing model to help users avoid unexpected issues which occur in the C typing model.
The rule prevents the accidental use of integer expression when a boolean expression was intended.

To be compliant with MISRA rule 14.4 you should write your test as a boolean expression:
Code:
if((udtEvents & udtSTOP_EVENT) != 0U  )