MISRA C++:2023 Rule 9.6.5
A function with non-void
return type shall return a value on all
paths
Since R2024b
Description
Rule Definition
A function with non-void
return type shall return a value on
all paths.
Rationale
If a function has a non-void
return type, it is expected to return a value. If the execution of the function body goes through a path where a return
statement is missing, the function return value is indeterminate. Subsequent computations with this return value can lead to unpredictable results.
Polyspace Implementation
The rule checker reports a violation when a function with a non-void
return type does not return a value on at least one execution path (unless the function is explicitly specified as [[noreturn]]
). The violation is reported on the closing brace of the function body.
For instance, the violation occurs when a function contains a return
statement inside one branch of an if
-else
statement. If this function does not have any other return
statement, a path that bypasses this branch does not return a value.
The checker does not report a violation if a function does not return a value because of a deliberate infinite loop such as while(1)
.
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
Check Information
Group: Statements |
Category: Required |
Version History
Introduced in R2024b