MISRAC2004-13.2_c
Synopsis
(Advisory) Tests of a value against zero should be made explicit, unless the operand is effectively boolean.
Enabled by default
No
Severity/Certainty
Low/Medium

Full description
Non-Boolean conditions were found in if statements. This check is identical to MISRAC++2008-5-0-13_c, MISRAC2012-Rule-14.4_c.
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)
{
int u8;
if (u8) {}
}
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
}