[PATCH] D146897: [clang:diagnostics] Turning off warn_self_assignment_overloaded for user-defined compound assignments

Xiang Li via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 4 12:06:06 PDT 2023


python3kgae added inline comments.


================
Comment at: clang/lib/Sema/SemaExpr.cpp:15656
   case BO_XorAssign:
-    DiagnoseSelfAssignment(S, LHS, RHS, OpLoc, false);
     CheckIdentityFieldAssignment(LHS, RHS, OpLoc, S);
     break;
----------------
rsmith wrote:
> This is the same thing, but for `this->x += this->x`. I think we should also suppress those warnings for compound assignment, except when one of the operands is an implicit member access and one is an explicit member access (`this->x += x;` should still warn if the latter `x` is also interpreted as `this->x`).
For case like this:

```
class Vector {
public:
    Vector& operator+=(const Vector &v) { return *this; }
    Vector& operator-=(const Vector &v) { return *this; }
};

class A {
public:
  A(){}
  Vector x;
  void foo() {
     this->x -= this->x;
     this->x -= x;
     this->x += this->x;
     this->x += x;
  }
};
```
clang will report 2 warning:

```
<source>:14:14: warning: assigning field to itself [-Wself-assign-field]
     this->x -= this->x;
             ^
<source>:15:14: warning: assigning field to itself [-Wself-assign-field]
     this->x -= x;
```

Do you mean we should make it report warning on this->x -= x; and this->x += x; ?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146897/new/

https://reviews.llvm.org/D146897



More information about the cfe-commits mailing list