C and C++


LANG.TYPE.VCBC : const宣言もしくはconstexpr宣言可能な変数

要旨

対象となる変数は変更されておらず、けれども const 宣言若しくは(C++のみ) constexpr宣言もされていません。

Pointed-to Type Could Be constも併せて確認して下さい。

プロパティ

クラス名 Variable Could Be const
日本語クラス名 const宣言もしくはconstexpr宣言可能な変数
クラス分類 スタイル (style)
ニーモニック LANG.TYPE.VCBC
カテゴリー
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
対応言語 C および C++ で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで無効になっています。チェックを有効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += allow class="Variable Could Be const"

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;
}

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。