Main Content

AUTOSAR C++14 Rule A8-4-2

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

Description

Rule Definition

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

Rationale

When you omit a return statement from a function with nonvoid return type, the function returns an undefined value. The undefined value can cause undefined behavior and unexpected results during code execution.

Polyspace Implementation

Polyspace® reports a rule violation when you do not include a return statement in every execution path of a function with nonvoid return type.

Troubleshooting

If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

int launch_angle (int mass) {
    if (mass < 50) {
        return 15;
    } else if (mass < 100) {
        return 45;
    } else {
        return 90; 
    }
} //Compliant

bool admission (bool has_ticket, bool valid_date) {
    if (has_ticket && valid_date) {
        return true;
    }
} //Noncompliant

In this example, the function launch_angle has a return statement for every execution path, so Polyspace does not report a rule violation. Because the admission function does not have a return statement outside of the if statement, Polyspace reports a rule violation.

Check Information

Group: Declarators
Category: Required, Automated

Version History

Introduced in R2019a