MISRAC++2008-5-0-13_a
In this section:
Synopsis
(Required) The condition of an if-statement and the condition of an iteration-statement shall have type bool.
Enabled by default
Yes
Severity/Certainty
Low/Medium

Full description
Non-Boolean termination conditions were found in do ... while statements. This check is identical to MISRAC2004-13.2_a, MISRAC2012-Rule-14.4_a.
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
Code examples
The following code example fails the check and will give a warning:
typedef int int32_t;
int32_t func();
void example(void)
{
do {
} while (func());
}
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)
{
while (int *ptr = fn() ) // Compliant by exception
{}
do
{
int *ptr = fn();
if ( NULL == ptr )
{
break;
}
}
while (true); // Compliant
while (int len = fn2() ) // Compliant by exception
{}
if (int *p = fn()) {} // Compliant by exception
if (int len = fn2() ) {} // Compliant by exception
if (bool flag = fn3()) {} // Compliant
}