I believe this pattern comes from the (now very old) C++ FAQ which goes to great pains to explain the problem of not using a self-assignment check, and proposes this as the solution:
However, note that this explicitly states that "If self-assignment can be handled without any extra code, don’t add any extra code." and "the goal is not to make self-assignment fast. If you don’t need to explicitly test for self-assignment, for example, if your code works correctly (even if slowly) in the case of self-assignment, then do not put an if test in your assignment operator..."
That is echoed by the CppCoreGuidelines, but their "simple" enforcement is not simple at all... because sometimes you do have to add this check, if your objects manage their own pointers.
Generally, on Chromium code:
- Most objects should be non-copyable.
- Classes that are copyable should contain only primitive and self-managing types (i.e., not raw pointers), so should not require a custom assignment operator and not need to explicitly handle self-assignment.
- In rare cases, you will be managing your own resources and therefore need to check for self-assignment (not as an optimization, but for correctness). Mostly these should be in low-level container classes which we probably don't have many left since now we use std containers for most things.
So yeah, generally we should design classes to not need this. But it may be needed in classes that manage their own resources.