Author: Xiang Li
Date: 2023-04-07T18:33:47-04:00
New Revision: c5302325b2a62d77cf13dd16cd5c19141862fed0

URL: 
https://github.com/llvm/llvm-project/commit/c5302325b2a62d77cf13dd16cd5c19141862fed0
DIFF: 
https://github.com/llvm/llvm-project/commit/c5302325b2a62d77cf13dd16cd5c19141862fed0.diff

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

Fixes 42469 https://github.com/llvm/llvm-project/issues/42469

Only check self assignment on BO_Assign when BuildOverloadedBinOp.

Added: 
    

Modified: 
    clang/lib/Sema/SemaExpr.cpp
    clang/test/SemaCXX/warn-self-assign-overloaded.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 351db6d7ccf27..86a4cbf23b765 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -15657,13 +15657,22 @@ static ExprResult BuildOverloadedBinOp(Sema &S, Scope 
*Sc, SourceLocation OpLoc,
                                        Expr *LHS, Expr *RHS) {
   switch (Opc) {
   case BO_Assign:
+    // In the non-overloaded case, we warn about self-assignment (x = x) for
+    // both simple assignment and certain compound assignments where algebra
+    // tells us the operation yields a constant result.  When the operator is
+    // overloaded, we can't do the latter because we don't want to assume that
+    // those algebraic identities still apply; for example, a path-building
+    // library might use operator/= to append paths.  But it's still reasonable
+    // to assume that simple assignment is just moving/copying values around
+    // and so self-assignment is likely a bug.
+    DiagnoseSelfAssignment(S, LHS, RHS, OpLoc, false);
+    [[fallthrough]];
   case BO_DivAssign:
   case BO_RemAssign:
   case BO_SubAssign:
   case BO_AndAssign:
   case BO_OrAssign:
   case BO_XorAssign:
-    DiagnoseSelfAssignment(S, LHS, RHS, OpLoc, false);
     CheckIdentityFieldAssignment(LHS, RHS, OpLoc, S);
     break;
   default:

diff  --git a/clang/test/SemaCXX/warn-self-assign-overloaded.cpp 
b/clang/test/SemaCXX/warn-self-assign-overloaded.cpp
index b27f4c857aa7d..428797ae40cd0 100644
--- a/clang/test/SemaCXX/warn-self-assign-overloaded.cpp
+++ b/clang/test/SemaCXX/warn-self-assign-overloaded.cpp
@@ -53,15 +53,15 @@ void f() {
 
 #ifndef DUMMY
   a *= a;
-  a /= a; // expected-warning {{explicitly assigning}}
-  a %= a; // expected-warning {{explicitly assigning}}
+  a /= a;
+  a %= a;
   a += a;
-  a -= a; // expected-warning {{explicitly assigning}}
+  a -= a;
   a <<= a;
   a >>= a;
-  a &= a; // expected-warning {{explicitly assigning}}
-  a |= a; // expected-warning {{explicitly assigning}}
-  a ^= a; // expected-warning {{explicitly assigning}}
+  a &= a;
+  a |= a;
+  a ^= a;
 #endif
 }
 


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to