Main Content

MISRA C:2012 Rule 15.6

The body of an iteration-statement or a selection-statement shall be a compound statement

Description

Rule Definition

The body of an iteration-statement or a selection-statement shall be a compound- statement.

Rationale

If the block of code associated with an iteration or selection statement is not contained in braces, you can make mistakes about the association. For example:

  • You can wrongly associate a line of code with an iteration or selection statement because of its indentation.

  • You can accidentally place a semicolon following the iteration or selection statement. Because of the semicolon, the line following the statement is no longer associated with the statement even though you intended otherwise.

This checker enforces the practice of adding braces following a selection or iteration statement even for a single line in the body. Later, when more lines are added, the developer adding them does not need to note the absence of braces and include them.

Polyspace Implementation

The checker flags for loops where the first token following a for statement is not a left brace, for instance:

for (i=init_val; i > 0; i--)
   if (arr[i] < 0)
      arr[i] = 0;
Similar checks are performed for if, else if, else, switch, for and do..while statements.

The second line of the message on the Result Details pane indicates which statement is violating the rule. For instance, in the preceding example, there are two violations. The second line of the message points to the for loop for one violation and the if condition for another.

Additional Message in Report

  • The else keyword shall be followed by either a compound statement, or another if statement.

  • An if (expression) construct shall be followed by a compound statement.

  • The statement forming the body of a while statement shall be a compound statement.

  • The statement forming the body of a do ... while statement shall be a compound statement.

  • The statement forming the body of a for statement shall be a compound statement.

  • The statement forming the body of a switch statement shall be a compound statement.

Troubleshooting

If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

int data_available = 1;
void f1(void) {
    while(data_available)                 /* Non-compliant */
        process_data();

    while(data_available) {               /* Compliant */
        process_data();
    }
}

In this example, the second while block is enclosed in braces and does not violate the rule.

#include<stdbool.h>
void f1(bool flag_1, bool flag_2) {
    if(flag_1)                            /* Non-compliant */
        if(flag_2)                        /* Non-compliant */
            action_1();
    else                                  /* Non-compliant */
            action_2();
}

In this example, the rule is violated because the if or else blocks are not enclosed in braces. Unless indented as above, it is easy to associate the else statement with the inner if.

Correction — Place Selection Statement Block in Braces

One possible correction is to enclose each block associated with an if or else statement in braces.

#include<stdbool.h>
void f1(bool flag_1, bool flag_2) {
    if(flag_1) {                          /* Compliant */
        if(flag_2) {                        /* Compliant */
            action_1();
        }
    }
    else {                                /* Compliant */
        action_2();
    }
}

#include<stdbool.h>
void f1(bool flag_1) {
    while(flag_1);                        /* Non-compliant */
    {
        flag_1 = action_1();
    }
}

In this example, the rule is violated even though the while statement is followed by a block in braces. The semicolon following the while statement causes the block to dissociated from the while statement.

The rule helps detect such spurious semicolons.

Check Information

Group: Control Flow
Category: Required
AGC Category: Required

Version History

Introduced in R2014b