Main Content

AUTOSAR C++14 Rule A6-5-4

For-init-statement and expression should not perform actions other than loop-counter initialization and modification

Description

Rule Definition

For-init-statement and expression should not perform actions other than loop-counter initialization and modification.

Rationale

For-init-statements and expressions are easier to read, understand, and maintain when they contain only the loop-counter initialization and modification.

Polyspace Implementation

This checker flags the following situations

  • Reports if loop parameter cannot be determined. Assumes JSF® C++ Rule 200 is not violated. The loop variable parameter is assumed to be a variable.

  • Assumes 1 loop parameter (see JSF C++ Rule 198), with non class type. JSF C++ Rule 200 must not be violated for this rule to be reported.

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

Because the for loop contains multiple loop parameters, Polyspace flags the for loop as noncompliant.

#include <cstdint> 

void example()
{
	for (std::int32_t x, y = 0; x < y; x++)  //Noncompliant
	{
		//...
	}
}

Check Information

Group: Statements
Category: Advisory, Automated

Version History

Introduced in R2019a