Author: Balázs Kéri
Date: 2026-07-15T11:07:09+02:00
New Revision: ba9f8e448ecd6f99664ed6b15c8d193650197845

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

LOG: [clang][analyzer] Improved message in uninitialized.Assign at default 
assignment (#208173)

Added: 
    

Modified: 
    clang/lib/StaticAnalyzer/Checkers/UndefinedAssignmentChecker.cpp
    clang/test/Analysis/operator-calls.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/StaticAnalyzer/Checkers/UndefinedAssignmentChecker.cpp 
b/clang/lib/StaticAnalyzer/Checkers/UndefinedAssignmentChecker.cpp
index 7f8923c7c09fc..331df5d8c750d 100644
--- a/clang/lib/StaticAnalyzer/Checkers/UndefinedAssignmentChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/UndefinedAssignmentChecker.cpp
@@ -73,6 +73,21 @@ void UndefinedAssignmentChecker::checkBind(SVal location, 
SVal val,
         }
       }
 
+      if (const auto *MD =
+              dyn_cast<CXXMethodDecl>(C.getStackFrame()->getDecl())) {
+        if ((MD->isCopyAssignmentOperator() ||
+             MD->isMoveAssignmentOperator()) &&
+            MD->isDefaulted() && B->isAssignmentOp() &&
+            isa<MemberExpr>(B->getLHS()->IgnoreImpCasts())) {
+          OS << "Value assigned to field '"
+             << cast<MemberExpr>(B->getLHS()->IgnoreImpCasts())
+                    ->getMemberDecl()
+                    ->getName()
+             << "' in " << (!MD->isImplicit() ? "default" : "implicit")
+             << " assignment operator is uninitialized";
+          break;
+        }
+      }
       ex = B->getRHS();
       break;
     }

diff  --git a/clang/test/Analysis/operator-calls.cpp 
b/clang/test/Analysis/operator-calls.cpp
index a89624d44ac42..57da7cdc16923 100644
--- a/clang/test/Analysis/operator-calls.cpp
+++ b/clang/test/Analysis/operator-calls.cpp
@@ -107,8 +107,13 @@ namespace SynthesizedAssignment {
   struct B {
     int x;
     A a[3];
-    B& operator=(B&) = default;
-    B& operator=(B&&) = default;
+    B& operator=(B&) = default; // #b_copy_assign
+    B& operator=(B&&) = default; // #b_move_assign
+  };
+
+  struct C { // #c_definition
+    int x;
+    A a[3];
   };
 
   // This used to produce a warning about the iteration variable in the
@@ -121,16 +126,16 @@ namespace SynthesizedAssignment {
   void testNoWarning() {
 
     B v, u;
-    u = v; // expected-warning@110{{Assigned value is uninitialized}}
+    u = v; // expected-warning@#b_copy_assign{{Value assigned to field 'x' in 
default assignment operator is uninitialized}}
     // expected-note@-1{{Calling defaulted copy assignment operator for 'B'}}
-    // expected-note@110{{Assigned value is uninitialized}}
+    // expected-note@#b_copy_assign{{Value assigned to field 'x' in default 
assignment operator is uninitialized}}
   }
 
   void testNoWarningMove() {
     B v, u;
-    u = static_cast<B &&>(v); // expected-warning@111{{Assigned value is 
uninitialized}}
+    u = static_cast<B &&>(v); // expected-warning@#b_move_assign{{Value 
assigned to field 'x' in default assignment operator is uninitialized}}
     // expected-note@-1{{Calling defaulted move assignment operator for 'B'}}
-    // expected-note@111{{Assigned value is uninitialized}}
+    // expected-note@#b_move_assign{{Value assigned to field 'x' in default 
assignment operator is uninitialized}}
   }
 
   void testConsistency() {
@@ -162,4 +167,11 @@ namespace SynthesizedAssignment {
     clang_analyzer_eval(u.a[2].a == 43); // expected-warning{{TRUE}}
     // expected-note@-1{{TRUE}}
   }
+
+  void testImplicitAssign() {
+    C c1, c2;
+    c1 = c2; // expected-warning@#c_definition{{Value assigned to field 'x' in 
implicit assignment operator is uninitialized}}
+    // expected-note@-1{{Calling implicit copy assignment operator for 'C'}}
+    // expected-note@#c_definition{{Value assigned to field 'x' in implicit 
assignment operator is uninitialized}}
+  }
 }


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to