Qt-Autosar-A13.2.1¶
An assignment operator shall return a reference to “this”
Required inputs: IR
a = b = c (chained assignment).
Returning void or other types prevents chaining and breaks standard assignment semantics that users expect.
Bad code (assignment not returning reference to this):
class MyInt {
int value_;
public:
void operator=(const MyInt& other) { // ERROR: returns void
value_ = other.value_;
}
};
void Use() {
MyInt a, b, c;
a = b = c; // Compile error: operator= returns void
}
Good code (assignment returning reference to this):
class MyInt {
int value_;
public:
MyInt& operator=(const MyInt& other) { // OK: returns reference to this
if (this != &other) {
value_ = other.value_;
}
return *this;
}
};
void Use() {
MyInt a, b, c;
a = b = c; // OK: chaining works
}
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
asgn_ref_this |
An assignment operator shall return a reference to “this”. |
None |
False |
Options
This rule shares the following common options: exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
This rule has no individual options.