MISRA.STMT.NO_COMPOUNDThe body of switch, while, do/while or for 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-statementCategory: Required Analysis: Decidable, Single Translation Unit Applies to: C90, C99 AmplificationThe body of an iteration-statement (while, do ...while or for) or a selection-statement (if, else, switch) shall be a compound-statement. RationaleIt 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. ExceptionAn if statement immediately following an else need not be contained within a compound-statement. ExampleThe 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.8 (required): The statement forming the body of a switch, while, do "¦ while or for statement shall be a compound statement.The statement that forms the body of a switch statement or a while, do "¦ while or for loop, shall be a compound statement (enclosed within braces), even if that compound statement contains a single statement. Examplefor (i = 0; i < N_ELEMENTS; ++i) { buffer[i] = 0; /* Even a single statement must be in braces */ } while ( new_data_available ) process_data (); /* Incorrectly not enclosed in braces */ service_watchdog (); /* Added later but, despite the appearance (from the indent) it is actually not part of the body of the while statement, and is executed only after the loop has terminated */ 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-3-1 (required): The statement forming the body of a switch, while, do ... while or for statement shall be a compound statementRationaleIf the bodies of these statements 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 a switch statement or a while, do ... while or for loop shall be a compound statement (enclosed within braces) ensures that these errors cannot arise. Examplefor ( i = 0; i < N_ELEMENTS; ++i ) { // Compliant buffer [ i ] = 0; // Even a single statement must // be in braces } for ( i = 0; i < N_ELEMENTS; ++i ); // Non-compliant // Accidental single null statement { buffer [ i ] = 0; } while ( new_data_available ) // Non-compliant process_data ( ); // Incorrectly not enclosed in braces service_watchdog ( ); // Added later but, despite the appearance // (from the indent) it is actually not // part of the body of the while statement, // and is executed only after the loop has // terminated 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. |