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.IF.NO_COMPOUND

The body of if/else statement is not a compound statement.

MISRA C 2012 Rule 15.6: The body of an iteration-statement or a selection-statement shall be a compound-statement

Category: Required

Analysis: Decidable, Single Translation Unit

Applies to: C90, C99

Amplification

The body of an iteration-statement (while, do … while or for) or a selection-statement (if, else, switch) shall be a compound-statement.

Rationale

It is possible for a developer to mistakenly believe that a sequence of statements forms the body of an iteration-statement or selection-statement by virtue of their indentation. The accidental inclusion of a semi-colon after the controlling expression is a particular danger, leading to a null control statement. Using a compound-statement clearly defines which statements actually form the body.

Additionally, it is possible that indentation may lead a developer to associate an else statement with the wrong if.

Exception

An if statement immediately following an else need not be contained within a compound-statement.

Example

The layout for the compound-statement and its enclosing braces are style issues which are not addressed by this document; the style used in the following examples is not mandatory.

Maintenance to the following

while ( data_available )
process_data ( ); /* Non-compliant */

could accidentally give

while ( data_available )
process_data ( ); /* Non-compliant */
service_watchdog ( );

where service_watchdog() should have been added to the loop body. The use of a compound-statement significantly reduces the chance of this happening.

The next example appears to show that action_2() is the else statement to the first if.

if ( flag_1 )
    if ( flag_2 )        /* Non-compliant */
        action_1 ( );    /* Non-compliant */
else
    action_2 ( );        /* Non-compliant */

when the actual behaviour is

if ( flag_1 )
{
    if ( flag_2 )
    {
        action_1 ( );
    }
    else
    {
        action_2 ( );
    }
}

The use of compound-statements ensures that if and else associations are clearly defined.

The exception allows the use of else if, as shown below:

if ( flag_1 )
{
    action_1 ( );
}
else if ( flag_2 )    /* Compliant by exception */
{
    action_2 ( );
}
else
{
    ;
}

The following example shows how a spurious semi-colon could lead to an error

while ( flag );    /* Non-compliant */
{
    flag = fn ( );
}

The following example shows the compliant method of writing a loop with an empty body:

while ( !data_available )
{
}

MISRA-C 2004 Rule 14.9 (required): An if (expression) construct shall be followed by a compound statement. The else keyword shall be followed by either a compound statement, or another if statement.

[Koenig 24]

Example

if ( test1 )
{
   x = 1;           /* Even a single statement must be in braces      */
}
else if ( test2 )   /* No need for braces in else if                  */
{
   x = 0;           /* Single statement must be in braces             */
}
else
      x = 3;        /* This was (incorrectly) not enclosed in braces  */
      y = 2;        /* This line was added later but, despite the
                       appearance (from the indent), it is actually
                       not part of the else, and is executed
                       unconditionally                                */

Note that the layout for compound statements and their enclosing braces should be determined from the style guidelines. The above is just an example.

MISRA-C++ 2008 Rule 6-4-1 (required): An if ( condition ) construct shall be followed by a compound statement. The else keyword shall be followed by either a compound statement, or another if statement.

Rationale

If the bodies of these constructs are not compound statements, then errors can occur if a developer fails to add the required braces when attempting to change a single statement body to a multistatement body.

Requiring that the body of these constructs shall be a compound statement (enclosed within braces) ensures that these errors cannot arise.

Example

if ( test1 );        // Non-compliant - accidental single null statement
{
   x = 1;
}
if ( test1 )
{
   x = 1;           // Compliant - a single statement must be in braces
}
else if ( test2 )   // Compliant - no need for braces between else and if
{
   x = 0;           // Compliant — a single statement must be in braces
}
else                // Non-compliant
   x = 3;           // This was (incorrectly) not enclosed in braces
   y = 2;           // This line was added later but, despite the
                    // appearance (from the indent) it is actually not
                    // part of the else, and is executed unconditionally

Note that this example assumes a particular style for the layout of compound statements and their enclosing braces. This style is not mandated, but a style should be defined within the style guide for the project.