[clang] [analyzer] Variant checker bindings (PR #87886)

DonĂ¡t Nagy via cfe-commits cfe-commits at lists.llvm.org
Tue May 14 05:26:04 PDT 2024


================
@@ -355,4 +356,38 @@ void nonInlineFunctionCallPtr() {
   char c = std::get<char> (v); // no-warning
   (void)a;
   (void)c;
-}
\ No newline at end of file
+}
+
+//----------------------------------------------------------------------------//
+// std::swap for std::variant
+//----------------------------------------------------------------------------//
+
+void swapForVariants() {
+  std::variant<int, char> a = 5;
+  std::variant<int, char> b = 'C';
+  std::swap(a, b);
+  int a1 = std::get<int>(b);
+  char c = std::get<int>(a); // expected-warning {{std::variant 'a' held a 'char', not an 'int'}}
+  (void)a1;
+  (void)c;
+}
+
+//----------------------------------------------------------------------------//
+// std::swap for std::variant
+//----------------------------------------------------------------------------//
+
+void stdEmplace() {
+  std::variant<int, char> v = 'c';
+  v.emplace<int> (5);
+  int a = std::get<int> (v); // no-warning
+  char c = std::get<char> (v); // no-warning
+  (void)a;
+  (void)c;
+}
+
+void followHeldValue() {
----------------
NagyDonat wrote:

It's good that this case highlights that the value is _not yet_ followed by this checker; but consider adding a TODO or similar commit to highlight that this is a limitation of the checker and eventually it could / will be modeled. (Or, if you believe that the value shouldn't be tracked, then explain that.)

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


More information about the cfe-commits mailing list