Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.RETURN.NOT_LAST

Return 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

Amplification

A 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.

Rationale

A 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.

Example

In 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.

Rationale

This is required by IEC 61508 [12], as part of the requirements for a modular approach.

Exception

A 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.

Example

void 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
}