MISRA C:2012 Rule 14.2
A for loop shall be well-formed
Description
Rule Definition
A for loop shall be well-formed.
Rationale
The for
loop provides a flexible looping facility. You can perform other
operations besides the loop counter initialization, termination, and increment in
the control statement, and increment the loop counter anywhere inside the loop body.
However, using a restricted loop format makes your code easier to review and to
analyze.
Polyspace Implementation
A for loop consists of a control statement with three clauses and a loop body. The checker raises a violation if:
The first clause does not contain an initialization (except for when the clause is empty). The checker considers the last assigned variable of the first
for
-loop clause as the loop counter. If the first clause is empty, the checker considers the variable incremented or decremented in the third clause as the loop counter.The second clause does not contain a comparison operation involving the loop counter.
The third clause contains an operation other than incrementing or decrementing the loop counter (separated by a comma from the increment or decrement).
The loop counter has a data type that is not an integer or a pointer type.
The loop counter is incremented inside the loop body.
Polyspace® does not raise a violation when the second clause includes a binary operation that involves the loop counter.
Troubleshooting
If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Control Statement Expressions |
Category: Required |
AGC Category: Readability |