C and C++


LANG.TYPE.MFCBCONST : const宣言可能なメンバ関数

要旨

[C++のみ]

メンバ関数 f()const宣言されておらず、加えて下記に挙げる全てが真となっています。

もしf()static(静的)クラスデータのみ変更する、若しくはいかなる non-static(非静的)クラスデータにアクセスしないということであれば、本ワーニングクラスの警告は発行されません。: Member Function Could Be static が代わりに発行されます(そのワーニングクラス検出が有効化されている場合)。

プロパティ

クラス名 Member Function Could Be const
日本語クラス名 const宣言可能なメンバ関数
クラス分類 スタイル (style)
ニーモニック LANG.TYPE.MFCBCONST
カテゴリー
AUTOSARC++14 AUTOSARC++14:M9-3-3 If a member function can be made static then it shall be made static, otherwise if it can be made const then it shall be made const.
MisraC++2008 MisraC++2008:9-3-3 If a member function can be made static then it shall be made static, otherwise if it can be made const then it shall be made const.
対応言語 C および C++ で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで無効になっており、プロジェクトには非正規の C向けAST が必要になります。有効にするにはプロジェクト設定ファイル (configuration file) に以下の WARNING_FILTER ルールと RETAIN_UNNORMALIZED_C_AST 設定を追加してください。
RETAIN_UNNORMALIZED_C_AST = Yes
WARNING_FILTER += allow class="Member Function Could Be const"
注:非正規化された AST を継続して使用した場合、使用ディスク容量が増加し解析時間が長くなる可能性があります。

class MyClass {
public:
    MyClass(void){ myint=5; my_static_int=10; }

    int cbconst(int i){                // 'Member Function Could Be const' warning issued here
        return myint;
    }

    int marked_const(int i) const {               // ok: declared const
        return myint + 1;
    }

    int cbconst_uncalled(int i){                  // inline and not called, so no warning of this class is issued
        return myint;
    }

    int modify_member(int i){                     // ok: modifies a (non-static) member
        myint += i;
        return myint;
    }
                                                  // modifies a static member:
                                                  // - a warning of this class is NOT issued 
                                                  // - a 'Member Function Could Be static' warning is issued (if enabled)
    int modify_static_member(int i){
        my_static_int += i;
        return my_static_int;
    }

   int noninline_cbconst(int i);

private:
    int myint;
    static int my_static_int;
};

int MyClass::noninline_cbconst(int i){  // 'Member Function Could Be const' warning issued here
    return myint + i;
}

// inline member functions can only trigger warnings of this class if they are used in the analyzed code
int use_MyClass( void ){
    MyClass mc;
    int rv=0;
    rv = mc.cbconst(rv);
    rv = mc.marked_const(rv);
    rv = mc.modify_member(rv);
    rv = mc.modify_static_member(rv);
    return rv;
}

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

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