MISRA.IF.NO_ELSEMISRA C 2012 Rule 15.7: All if ... else if constructs shall be terminated with an else statementA chain of if/else-if statements is not terminated with else or is terminated with an empty else clause. Category: Required Analysis: Decidable, Single Translation Unit Applies to: C90, C99 AmplificationA final else statement shall always be provided whenever an if statement is followed by a sequence of one or more else if constructs. The else statement shall contain at least either one side effect or a comment. RationaleTerminating a sequence of if ... else if constructs with an else statement is defensive programming and complements the requirement for a default clause in a switch statement (see Rule 16.5). The else statement is required to have a side effect or a comment to ensure that a positive indication is given of the desired behaviour, aiding the code review process. Note: a final else statement is not required for a simple if statement. ExampleThe following example is non-compliant as there is no explicit indication that no action is to be taken by the terminating else. if ( flag_1 ) { action_1 ( ); } else if ( flag_2 ) { action_2 ( ); } /* Non-compliant */ The following shows a compliant terminating else. else { ; /* No action required - ; is optional */ } See alsoRule 16.5 MISRA-C 2004 Rule 14.10 (required): All if "¦ else if constructs shall be terminated with an else clause.This rule applies whenever an if statement is followed by one or more else if statements; the final else if shall be followed by an else statement. In the case of a simple if statement then the else statement need not be included. The requirement for a final else statement is defensive programming. The else statement shall either take appropriate action or contain a suitable comment as to why no action is taken. This is consistent with the requirement to have a final default clause in a switch statement (15.3). For example this code is a simple if statement: if ( x < 0 ) { log_error(3); x = 0; } /* else not needed */ whereas the following code demonstrates an if, else if construct if ( x < 0 ) { log_error(3); x = 0; } else if ( y < 0 ) { x = 3; } else /* this else clause is required, even if the */ { /* programmer expects this will never be reached */ /* no change in value of x */ } MISRA-C++ 2008 Rule 6-4-2 (required): All if "¦ else if constructs shall be terminated with an else clause.RationaleWhen an if statement is followed by one or more else if statements then the final else if shall be followed by an else statement. In the case of a simple if statement the else statement need not be included. The final else statement, which should either take appropriate action or contain a suitable comment as to why no action is taken, is defensive programming. ExampleFor example this code is a simple if statement: if ( x < 0 ) { log_error( 3 ); x = 0; } // else not needed Whereas the following code demonstrates an if, else if construct if ( x < 0 ) { log_error ( 3 ); x = 0; } else if ( y < 0 ) { x = 3; } else // this else clause is required, even if the { // developer expects this will never be reached // No change in value of x } |