Main Content

AUTOSAR C++14 Rule A6-5-1

A for-loop that loops through all elements of the container and does not use its loop-counter shall not be used

Since R2022a

Description

Rule Definition

A for-loop that loops through all elements of the container and does not use its loop-counter shall not be used.

Rationale

If you loop through all the elements of a container and you use the loop counter variable to access only the value of the elements, a for loop is slower, less readable, and more difficult to maintain than an equivalent range-based for loop.

Polyspace Implementation

Polyspace® flags the use of for loops that loop through all elements of a container or a C-style array when both of these conditions are true:

  • The for loop has only one loop counter variable. For instance, Polyspace does not flag this for loop:

    for(int i=0, j=0; i < 10; ++i, ++j){ //Compliant
    //...
    }

  • The for loop uses the loop counter variable only to access the elements of the container or the C-style array. For instance, in this code snippet, Polyspace flags the first for loop because the array counter "i" is used only the access elements of myArray which are then assigned a value. Use a range-based for loop instead to perform an equivalent operation.

    Polyspace does not flag the second for loop because the array counter is used to access the array elements and is assigned to the element.

    #include <iostream>
    
    int myArray[10];
    
    void cArray()
    {
        //First for loop: Loop counter 'i' used only to access elements
        //of myArray which are then assigned a value
        for (int i = 0; i < 10; ++i)
        { //Non-compliant
            myArray[i] = 0;
        }
        //Range-based for loop - equivalent to first for loop
        for (int idx : myArray)
        {
            myArray[idx] = 0; // compliant
        }  
    
        //Second for loop: Loop counter assigned to elements of myArray
        for (int i = 0; i < 10; ++i)
        { //Compliant
            myArray[i] = i;
        }
    }

Polyspace does not flag violations of this rule in these scenarios:

  • You iterate through a user-defined container. For instance, this for loop is compliant.

    template<typename T>
    class customContainer {
      typedef T* iterator;
      public:
        iterator begin();
        iterator end();
    };
    
    void func() {
      customContainer<int> myContainer;
    
      for(auto it = myContainer.begin(); it != myContainer.end(); it++) { //Compliant 
        std::cout << *it;
      }
    }
    Polyspace flags violations of the rule only for C-style arrays and Standard Template Library containers.

  • You use reverse iterators to loop through the elements of the C-style array or container. For instance, in this code snippet, there is no violation of the rule because you cannot use a range-based for loop to perform an equivalent operation.

    #include <iostream>
    #include <vector>
    #include <cstdint>
    
    std::vector<std::uint32_t> myVector(10);
    
    void myContainer()
    {
        //loop uses reverse iteration
        for (auto it = myVector.rbegin(); it != myVector.rend(); ++it) { //Compliant
    
            std::cout << *it;
        }
    }
    

  • If you loop through arrays:

    • You loop through the elements of multiple arrays or you loop through the elements of a multidimensional array.

    • The array size is unknown.

    For instance, Polyspace considers these for loops compliant:

    int myArray[10];
    int myOtherArray[10];
    int multiArray[10][10];
    
    void cArray()
    {
        //loop through multiple array
        for (int i = 0; i < 10; ++i) { //Compliant
            myArray[i] = 0;
            myOtherArray[i] = 0;
        }
        //loop through 2-dimensional array
        for (int i = 0; i < 10; ++i) { //Compliant
            multiArray[i][i] = 0;
        }
    
    }
    
    void unknownSize(int someArray[])
    {
        //loop through array of unknown size
        for (int i = 0; i < 10; ++i) { //Compliant
            someArray[i] = 0;
        }
    }

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

#include <iostream>
#include <vector>
#include <cstdint>

std::vector<std::uint32_t> myVector(10);

void myContainer()
{
    for (auto it = myVector.begin(); it != myVector.end(); ++it) { //Non-compliant
        std::cout << *it;
    }

    for (auto it : myVector) { //Compliant
        std::cout << it;
    }
}

In this example, Polyspace flags the for loop, which iterates through all the elements of vector myVector and uses the loop counter it to only print the value of each element. The range-based for loop for (auto it: myVector) performs an equivalent operation, uses less code and makes your code run more efficiently, more readable, and easier to maintain.

Check Information

Group: Statements
Category: Required, Automated

Version History

Introduced in R2022a