MISRA C++:2023 Rule 9.5.2
Description
Rule Definition
A for-range-initializer shall contain at most one function call.
Rationale
If a chain of function calls is used in a range-based for
-loop
initializer, for
example,
for (auto it: myData().getValues()) {}
By limiting the range-based for
-loop initialization statement to a
single function call, you can avoid iterating over multiple temporary objects and
encountering potential issues with their lifetimes. This rule considers use of an
overloaded operator and any expression that creates an object of
class
type to be function calls.
Polyspace Implementation
Polyspace® reports a rule violation whenever more than one function call, object
creation, or data member access is attempted during a range-based
for
-loop initialization statement.
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