AUTOSAR C++14 Rule A7-1-1

Constexpr or const specifiers shall be used for immutable data declaration

Description

Rule Definition

Constexpr or const specifiers shall be used for immutable data declaration.

Rationale

Declaring a variable const or constexpr reduces the chances that you modify the variable by accident. In addition, compilers can perform various optimizations on const and constexpr variables to improve run-time performance.

Polyspace Implementation

The checker flags function parameters or local variables that are not const-qualified but never modified in the function body. Function parameters of integer, float, enum and boolean types are not flagged.

If a variable is passed to another function by reference or pointers, the checker assumes that the variable can be modified. These variables are not flagged.

Troubleshooting

If you expect a rule violation but do not see it, refer to the documentation for Polyspace® Bug Finder™ or Polyspace Bug Finder Server™.

Examples

expand all

#include <string.h>

char returnNthCharacter (int n) {
    char* pwd = "aXeWdf10fg" ; //Noncompliant
    char nthCharacter;
        
    for(int i=0; i < strlen(pwd); i++) {
        if(i==n)
            nthCharacter = pwd[i];
    }
    return nthCharacter;
}

In this example, the pointer pwd is not const-qualified. However, beyond initialization with a constant, it is not reassigned anywhere in the returnNthCharacter function.

Check Information

Group: Declaration
Category: Required, Automated
Introduced in R2020b