Qt-Autosar-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 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.

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.