MISRA.IF.NO_COMPOUNDThe 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-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.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] Exampleif ( 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.RationaleIf 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. Exampleif ( 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. |