C and C++


LANG.TYPE.VCBC : Variable Could Be const

Summary

A variable is not modified, but is not declared with a const or (C++ only) constexpr specifier.

See also Pointed-to Type Could Be const.

Properties

Class Name Variable Could Be const
Significance style
Mnemonic LANG.TYPE.VCBC
Categories
AUTOSARC++14 AUTOSARC++14:A7-1-1 Constexpr or const specifiers shall be used for immutable data declaration.
MisraC++2008 MisraC++2008:7-1-1 A variable which is not modified shall be const qualified.
CWE CWE:710 Improper Adherence to Coding Standards
CERT-C CERT-C:DCL00-C Const-qualify immutable objects
Availability Available for C and C++.
Enabling Checks for this warning class are disabled by default. To enable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += allow class="Variable Could Be const"

Example

int use_params(int * p1,                             // ok: p1 is modified
               int * p2,             // 'Variable Could Be const' warning issued here
               int * const p3){                      // ok: unmodified parameter marked const
    if (!p1 || !p2 || !p3) {return -1;}
    p1 = p2;                                         // p1 is modified here
    *p2 = 2;                                         // *p2 is modified here, but p2 is not modified
    return *p1 + *p2 + *p3;
}

constexpr int five(void) {return 5;}

int use_locals(const int k){                         // ok: unmodified parameter marked const
    int a = five();                   // 'Variable Could Be const' warning issued here
    const int b = five();                            // ok: unmodified local marked const
    constexpr int c = five();                        // ok: unmodified local marked constexpr
    int d = five();                                  // ok: local modified on some paths
    if (k){
        d++;
    }
    return a + b + c + d;
}

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.