C and C++


LANG.STRUCT.ASSIGNRET : Inappropriate Assignment Operator Return

要旨

An assignment operator (operator=) does not return a value equal to *this, and is used one or more times in the analyzed code.

プロパティ

クラス名 Inappropriate Assignment Operator Return
日本語クラス名 Inappropriate Assignment Operator Return
クラス分類 スタイル (style)
ニーモニック LANG.STRUCT.ASSIGNRET
カテゴリー
AUTOSARC++14 AUTOSARC++14:A13-2-1 An assignment operator shall return a reference to "this".
JSF++ JSF++:82 An assignment operator shall return a reference to *this.
対応言語 C++ のみ利用可能です。 C は利用できません。
有効/無効設定 このワーニングクラスのチェックはデフォルトで無効になっています。チェックを有効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += allow class="Inappropriate Assignment Operator Return"

class MyClass1 {
public:
    MyClass1(int x) : m_x(x) {};
    MyClass1 & operator=(MyClass1 & other) {
        m_x = other.m_x;
        return other;                  // 'Inappropriate Assignment Operator Return' warning issued here
    }
private:
    int m_x;
};

class MyClass2 {
public:
    MyClass2(int x) : m_x(x) {};
    void operator=(MyClass2 & other) { // 'Inappropriate Assignment Operator Return' warning issued here
        m_x = other.m_x;
    }
private:
    int m_x;
};

class MyClass3 {
public:
    MyClass3(int x) : m_x(x) {};
    MyClass3 & operator=(MyClass3 & other) {
        m_x = other.m_x;
        return *this;                                              // ok: returns *this
    }
private:
    int m_x;
};

// Use the assignment operators so that they are instantiated.
void test( MyClass1 & p1, MyClass2 & p2, MyClass3 & p3 )
{
    MyClass1 v1(0);
    MyClass2 v2(0);
    MyClass3 v3(0);

    v1 = p1;
    v2 = p2;
    v3 = p3;
}

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

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