MISRAC2012-Rule-14.4_b
Synopsis
(Required) The controlling expression of an if statement and the controlling expression of an iteration-statement shall have essentially Boolean type
Enabled by default
Yes
Severity/Certainty
Medium/Medium

Full description
Non-Boolean termination conditions were found in for loops. This check is identical to MISRAC2004-13.2_b, MISRAC++2008-5-0-13_b.
Coding standards
- MISRA C:2004 13.2
(Advisory) Tests of a value against zero should be made explicit, unless the operand is effectively boolean.
- MISRA C:2012 Rule-14.4
(Required) The controlling expression of an if statement and the controlling expression of an iteration-statement shall have essentially Boolean type
- MISRA C++ 2008 5-0-13
(Required) The condition of an if-statement and the condition of an iteration-statement shall have type bool.
Code examples
The following code example fails the check and will give a warning:
void example(void)
{
for (int x = 10;x;--x) {}
}
The following code example passes the check and will not give a warning about this issue:
#include <stddef.h>
int * fn()
{
int * ptr;
return ptr;
}
int fn2()
{
return 5;
}
bool fn3()
{
return true;
}
void example(void)
{
for (fn(); fn3(); fn2()) // Compliant
{}
for (fn(); true; fn()) // Compliant
{
int *ptr = fn();
if ( NULL == ptr )
{
break;
}
}
for (int len = fn2(); len < 10; len++) // Compliant
;
}