A copy assignment operator deletes a member before it reads the source, which is the same object on self-assignment
What it finds
Buffer &operator=(const Buffer &other) { delete[] data_; data_ = copy(other.data_); }: when an object is assigned to itself, other is the object, so the copy reads the memory just deleted. Self-assignment happens through aliases, as in a[i] = a[j] or std::swap on equal elements, and the read is undefined.
A test if (this == &other) before the deletion, or copying into a new allocation before deleting the old one, or copy and swap, makes the operator safe.
The rule looks at copy assignment operators, taking a const reference, whose body deletes a member with a statement of its own and afterwards reads the same member of the parameter, and that neither take the address of the parameter, as this == &other does, nor call swap.
From the rule's source, copy_assignment_not_self_safe.rs.
Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/cpp:copy-assignment-not-self-safe" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/cpp:copy-assignment-not-self-safe] reason="..."tx0 explain tx0/cpp:copy-assignment-not-self-safe shows the same in the terminal. Found a false positive? Report it.