Skip to main content

IAR Embedded Workbench for RL78 5.20

MISRAC++2008-5-0-13_c

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

lowmedium.png
Full description

Non-boolean conditions were found in if statements. This check is identical to MISRAC2004-13.2_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

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
}