[clang-tools-extra] [clang-tidy] Fix false positive in bugprone-use-after-move for std::tie (PR #192895)

Yuta Nakamura via cfe-commits cfe-commits at lists.llvm.org
Sun Apr 19 23:57:04 PDT 2026


https://github.com/nakasan617 updated https://github.com/llvm/llvm-project/pull/192895

>From 277e521120a76b16ebbe4d6565c69a5428fc28e8 Mon Sep 17 00:00:00 2001
From: Yuta Nakamura <yutanak6 at gmail.com>
Date: Sun, 19 Apr 2026 21:45:30 -0700
Subject: [PATCH] [clang-tidy] Fix false positive in bugprone-use-after-move
 for std::tie

std::tie(a, b) = expr reinitializes all variables passed to std::tie
because the tuple assignment operator writes back through the stored
references. The check was not recognizing this pattern, causing a false
positive on the second std::tie assignment in loops like:

  std::tie(a, b) = foo(std::move(a), std::move(b));
  std::tie(a, b) = foo(std::move(a), std::move(b)); // false positive

Add std::tie assignment as a reinitialization case in makeReinitMatcher().

Fixes #136105.
---
 .../clang-tidy/bugprone/UseAfterMoveCheck.cpp |  9 +++++
 clang-tools-extra/docs/ReleaseNotes.rst       |  8 ++++
 .../checkers/bugprone/use-after-move.cpp      | 38 ++++++++++++++++++-
 3 files changed, 54 insertions(+), 1 deletion(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
index 31c70b3643be6..04aa17fb42efe 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
@@ -133,6 +133,15 @@ makeReinitMatcher(const ValueDecl *MovedVariable,
                  // template functions may be instantiated to use std::move() on
                  // built-in types.
                  binaryOperation(hasOperatorName("="), hasLHS(DeclRefMatcher)),
+                 // std::tie() assignment: std::tie(a, b) = expr reinitializes
+                 // all variables passed to std::tie because the tuple
+                 // assignment writes back through the stored references.
+                 binaryOperation(
+                     hasOperatorName("="),
+                     hasLHS(
+                         callExpr(callee(functionDecl(hasName("::std::tie"))),
+                                  hasAnyArgument(
+                                      ignoringParenImpCasts(DeclRefMatcher))))),
                  // Declaration. We treat this as a type of reinitialization
                  // too, so we don't need to treat it separately.
                  declStmt(hasDescendant(equalsNode(MovedVariable))),
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 95ed0061d654c..1a6464b373e36 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -230,6 +230,14 @@ New check aliases
 Changes in existing checks
 ^^^^^^^^^^^^^^^^^^^^^^^^^^
 
+- Improved :doc:`bugprone-use-after-move
+  <clang-tidy/checks/bugprone/use-after-move>` check to no longer emit a
+  false positive when a moved-from variable is reinitialized via a
+  ``std::tie()`` assignment (e.g. ``std::tie(a, b) = f(std::move(a),
+  std::move(b))``). The tuple assignment writes back through the stored
+  references, which fully reinitializes the captured variables.
+
+
 - Improved :doc:`bugprone-argument-comment
   <clang-tidy/checks/bugprone/argument-comment>` to also check for C++11
   inherited constructors.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/use-after-move.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/use-after-move.cpp
index 80df2b99eb874..bd4676e9e3af9 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/use-after-move.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/use-after-move.cpp
@@ -15,12 +15,13 @@
 #include <forward_list>
 #include <list>
 #include <map>
+#include <memory>
 #include <set>
 #include <string>
+#include <tuple>
 #include <unordered_map>
 #include <unordered_set>
 #include <utility>
-#include <memory>
 #include <vector>
 
 typedef decltype(nullptr) nullptr_t;
@@ -1789,4 +1790,39 @@ void Run() {
   RegularReset(db6);
   db6.Query();
 }
+
 } // namespace custom_reinitialization
+
+// Tests for std::tie() reinitialization.
+std::pair<std::string, std::string> makeStringPair(std::string a,
+                                                   std::string b);
+
+void stdTieIsReinit() {
+  // std::tie on the LHS reinitializes all captured variables.
+  std::string a, b;
+  std::move(a);
+  std::move(b);
+  std::tie(a, b) = makeStringPair("x", "y"); // no-warning: both reinitialized
+  a.size();
+  b.size();
+}
+
+void stdTiePartialReinit() {
+  // Only variables named in std::tie are reinitialized.
+  std::string a, b;
+  std::move(a);
+  std::tie(b) = std::make_tuple(std::string("y")); // reinitializes b, not a
+  b.size();  // no-warning: b was reinitialized
+  a.size(); // expected-warning{{'a' used after it was moved}}
+  // CHECK-NOTES: [[@LINE-1]]:3: warning: 'a' used after it was moved
+  // CHECK-NOTES: [[@LINE-4]]:3: note: move occurred here
+}
+
+void stdTieInLoop() {
+  // std::tie on the LHS reinitializes before the next iteration.
+  std::string a, b;
+  while (true) {
+    std::tie(a, b) = makeStringPair(std::move(a), std::move(b));
+    std::tie(a, b) = makeStringPair(std::move(a), std::move(b)); // no-warning
+  }
+}



More information about the cfe-commits mailing list