COP-assign-op-ret (C++ only)
Synopsis
An assignment operator of a C++ class does not return a non-const reference to this.
Enabled by default
Yes
Severity/Certainty
Low/High

Full description
An assignment operator of a C++ class is incorrectly defined. Probably it does not return a non-const reference to the left-hand side of the assignment. This can cause unexpected behavior in situations where the assignment is chained with others, or the return value is used as a left-hand side argument to a subsequent assignment. A non-const reference as the return type should be used because it is the convention; it will not achieve any added code safety, and it makes the assignment operator more restrictive.
Coding standards
This check does not correspond to any coding standard rules.
Code examples
The following code example fails the check and will give a warning:
class MyClass{
int x;
public:
MyClass &operator=(MyClass &rhs){
x = rhs.x;
return rhs; // should return *this
}
};
The following code example passes the check and will not give a warning about this issue:
class MyClass{
int x;
public:
MyClass &operator=(const MyClass &rhs) {
x = rhs.x;
return *this; // a properly defined operator=
}
};