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

via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 8 02:03:11 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Balázs Kéri (balazske)

<details>
<summary>Changes</summary>



---
Full diff: https://github.com/llvm/llvm-project/pull/208173.diff


2 Files Affected:

- (modified) clang/lib/StaticAnalyzer/Checkers/UndefinedAssignmentChecker.cpp (+14) 
- (modified) clang/test/Analysis/operator-calls.cpp (+16-4) 


``````````diff
diff --git a/clang/lib/StaticAnalyzer/Checkers/UndefinedAssignmentChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/UndefinedAssignmentChecker.cpp
index 7f8923c7c09fc..f97dd8ee67d57 100644
--- a/clang/lib/StaticAnalyzer/Checkers/UndefinedAssignmentChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/UndefinedAssignmentChecker.cpp
@@ -73,6 +73,20 @@ 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()) {
+          OS << "Value assigned to field '"
+             << cast<MemberExpr>(B->getRHS()->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..5af0d72b60bc0 100644
--- a/clang/test/Analysis/operator-calls.cpp
+++ b/clang/test/Analysis/operator-calls.cpp
@@ -111,6 +111,11 @@ namespace SynthesizedAssignment {
     B& operator=(B&&) = default;
   };
 
+  struct C {
+    int x;
+    A a[3];
+  };
+
   // This used to produce a warning about the iteration variable in the
   // synthesized assignment operator being undefined.
   //
@@ -121,16 +126,16 @@ namespace SynthesizedAssignment {
   void testNoWarning() {
 
     B v, u;
-    u = v; // expected-warning at 110{{Assigned value is uninitialized}}
+    u = v; // expected-warning at 110{{Value assigned to field 'x' in default assignment operator is uninitialized}}
     // expected-note at -1{{Calling defaulted copy assignment operator for 'B'}}
-    // expected-note at 110{{Assigned value is uninitialized}}
+    // expected-note at 110{{Value assigned to field 'x' in default assignment operator is uninitialized}}
   }
 
   void testNoWarningMove() {
     B v, u;
-    u = static_cast<B &&>(v); // expected-warning at 111{{Assigned value is uninitialized}}
+    u = static_cast<B &&>(v); // expected-warning at 111{{Value assigned to field 'x' in default assignment operator is uninitialized}}
     // expected-note at -1{{Calling defaulted move assignment operator for 'B'}}
-    // expected-note at 111{{Assigned value is uninitialized}}
+    // expected-note at 111{{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 at -1{{TRUE}}
   }
+
+  void testImplicitAssign() {
+    C c1, c2;
+    c1 = c2; // expected-warning at 114{{Value assigned to field 'x' in implicit assignment operator is uninitialized}}
+    // expected-note at -1{{Calling implicit copy assignment operator for 'C'}}
+    // expected-note at 114{{Value assigned to field 'x' in implicit assignment operator is uninitialized}}
+  }
 }

``````````

</details>


https://github.com/llvm/llvm-project/pull/208173


More information about the cfe-commits mailing list