Rule A8-4-2 (required, implementation, automated)

All exit paths from a function with non-void return type shall have an explicit return statement with an expression.

Rationale

In a function with non-void return type, return expression gives the value that the function returns. The absence of a return with an expression leads to undefined behavior (and the compiler may not give an error).

Exception

A function may additionally exit due to exception handling (i.e. a throw statement).

Example

// $Id: A8-4-2.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <cstdint>
#include <stdexcept>
std::int32_t F1() noexcept // Non-compliant
{
}
std::int32_t F2(std::int32_t x) noexcept(false)
{
if (x > 100)
{
throw std::logic_error("Logic Error"); // Compliant by exception
}

return x; // Compliant
}
std::int32_t F3(std::int32_t x, std::int32_t y)
{
if (x > 100 || y > 100)
{
throw std::logic_error("Logic Error"); // Compliant by exception
}
if (y > x)
{
return (y - x); // Compliant
}
return (x - y); // Compliant
}

See also

MISRA C++ 2008 [7]: Rule 8-4-3 All exit paths from a function with non-void return type shall have an explicit return statement with an expression. SEI CERT C++ [10]: MSC52-CPP. Value-returning functions must return a value from all exit paths.