MISRA.RETURN.NOT_LASTReturn is not the last statement in a function. MISRA C 2012 Rule 15.5: A function should have a single point of exit at the end[IEC 61508-3 Part 3 Table B.9], [ISO 26262-6 Table 8] Category: Advisory Analysis: Decidable, Single Translation Unit Applies to: C90, C99 AmplificationA function should have no more than one return statement. When a return statement is used, it should be the final statement in the compound statement that forms the body of the function. RationaleA single point of exit is required by IEC 61508 and ISO 26262 as part of the requirements for a modular approach. Early returns may lead to the unintentional omission of function termination code. If a function has exit points interspersed with statements that produce persistent side effects, it is not easy to determine which side effects will occur when the function is executed. ExampleIn the following non-compliant code example, early returns are used to validate the function parameters. bool_t f ( uint16_t n, char *p ) { if ( n > MAX ) { return false; } if ( p == NULL ) { return false; } return true; } MISRA-C 2004 Rule 14.7 (required): A function shall have a single point of exit at the end of the function.[IEC 61508-3 Part 3 Table B.9] This is required by IEC 61508, under good programming style. MISRA-C++ 2008 Rule 6-6-5 (required): A function shall have a single point of exit at the end of the function.RationaleThis is required by IEC 61508 [12], as part of the requirements for a modular approach. ExceptionA function implementing a function-try-block is permitted to have multiple points of exit, one for the try block and one for each catch handler. Throwing an exception that is not caught within the function is not considered a point of exit for this rule. Examplevoid fn ( void ) { if ( ... ) { return; // Non-compliant } try { if ( ... ) { throw ( 1 ); // Compliant by exception } } catch ( int32_t ) { throw; // Compliant by exception } return; // Non-compliant } void fn2 ( void ) { try { return; // Non-compliant } catch ( ... ) { return; // Non-compliant } } void fn3 ( void ) try { return; // Compliant by exception } catch ( int32_t ) { return; // Compliant by exception } catch ( ... ) { return; // Compliant by exception } |