C and C++


LANG.TYPE.MFCBCONST : Member Function Could Be const

Summary

[C++ only]

A member function f() is not declared const, and all of the following are true.

If f() modifies only static class data, or does not access any non-static class data, a warning of this class will not be issued: a Member Function Could Be static warning will be issued instead (if enabled).

Properties

Class Name Member Function Could Be const
Significance style
Mnemonic LANG.TYPE.MFCBCONST
Categories
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.
Availability Available for C and C++.
Enabling Checks for this warning class are disabled by default, and require the unnormalized C ASTs for the project. To enable them, add the following WARNING_FILTER rule and RETAIN_UNNORMALIZED_C_AST specification to the project configuration file.
RETAIN_UNNORMALIZED_C_AST = Yes
WARNING_FILTER += allow class="Member Function Could Be const"
Note that retaining the unnormalized ASTs will increase the disk space used to store the project representation, and may make the analysis take longer.

Example

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

Relevant Configuration File Parameters

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