AutosarC++17_03-A8.4.2

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

Required inputs: IR

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

Exceptions

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

Example
// $Id: A8-4-2.cpp 271687 2017-03-23 08:57:35Z piotr.tanski $
#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 [6]: 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++ [9]: MSC52-CPP. Value-returning functions must return a value from all exit paths.

Possible Messages

Key

Text

Severity

Disabled

lambda_return_missing_value

Return without value in non-void lambda expression

None

False

missing_return

Non-void function needs return with value at end.

None

False

missing_return_in_lambda

Non-void lambda expression needs return with value at end.

None

False

return_missing_value

Return without value in non-void function

None

False

Options

exclude_missing_return_for_main

exclude_missing_return_for_main : bool = False

If True, no message for a missing return in main() will be reported.