C and C++


LANG.STRUCT.ASSIGNRET : Inappropriate Assignment Operator Return

Summary

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

Properties

Class Name Inappropriate Assignment Operator Return
Significance style
Mnemonic LANG.STRUCT.ASSIGNRET
Categories
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.
Availability Available for C++ only (not 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="Inappropriate Assignment Operator Return"

Example

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

Relevant Configuration File Parameters

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