Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.STMT.COND.NOT_BOOLEAN

Condition of if or loop statement is not a boolean expression.

MISRA-C++ Rule 5-0-13 (required): The condition of an if-statement and the condition of an iteration-statement shall have type bool.

Rationale

If an expression with type other than bool is used in the condition of an if-statement or iteration-statement, then its result will be implicitly converted to bool. The condition expression shall contain an explicit test (yielding a result of type bool) in order to clarify the intentions of the developer.

Exception

A condition of the form type-specifier-seq declarator is not required to have type bool.

This exception is introduced because alternative mechanisms for achieving the same effect are cumbersome and error-prone.

Example

extern int32_t * fn ( );
extern int32_t fn2 ( );
extern bool fn3 ( );

while ( int32_t * p = fn ( ) ) // Compliant by exception
{
   // Code
}
// The following is a cumbersome but compliant example
do
{
   int32_t * p = fn ( );
   if ( NULL == p )
   {
      break;
   }
   // Code...
}
while ( true ); // Compliant

while ( int32_t length = fn2 ( ) ) // Compliant by exception
{
   // Code
}
while ( bool flag = fn3 ( ) )      // Compliant
{
   // Code
}
if ( int32_t * p = fn ( ) )        // Compliant by exception
if ( int32_t length = fn2 ( ) )    // Compliant by exception
if ( bool flag = fn3 ( ) )         // Compliant
if ( u8 )                          // Non-compliant
if ( u8 && ( bool_1 <= bool_2 ) )  // Non-compliant
for ( int32_t x = 10; x; --x )     // Non-compliant