[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 21:50:58 PDT 2026


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

>From 4a3e3a1500cb1c0b0c74cd6d37789483e795a4d3 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      | 39 +++++++++++++++++++
 3 files changed, 56 insertions(+)

diff --git a/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
index 31c70b3643be6..92c4cc10cf908 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..3403f887edf03 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
@@ -1789,4 +1789,43 @@ void Run() {
   RegularReset(db6);
   db6.Query();
 }
+
+// Tests for std::tie() reinitialization.
+namespace std {
+template <typename... Args>
+struct tuple {};
+template <typename... Args>
+tuple<Args &...> tie(Args &...args);
+} // namespace std
+
+std::pair<std::string, std::string> makeStringPair(std::string a,
+                                                   std::string b);
+
+void stdTieIsReinit() {
+  // std::tie on the LHS of assignment reinitializes all variables it captures.
+  std::string a, b;
+  std::move(a);
+  std::move(b);
+  std::tie(a, b) = makeStringPair(a, b); // no-warning: both reinitialised
+  a.size();
+  b.size();
+}
+
+void stdTiePartialReinit() {
+  // Only variables named in std::tie are reinitialized.
+  std::string a, b;
+  std::move(a);
+  std::tie(b) = makeStringPair("x", "y"); // reinitializes b, not a
+  b.size();                               // no-warning: b was reinitialized
+  a.size(); // expected-warning{{'a' used after it was moved}}
+}
+
+void stdTieInLoop() {
+  std::string a, b;
+  while (true) {
+    std::tie(a, b) = makeStringPair(std::move(a), std::move(b));
+    // no-warning: std::tie reinitializes a and b before next iteration
+    std::tie(a, b) = makeStringPair(std::move(a), std::move(b));
+  }
+}
 } // namespace custom_reinitialization



More information about the cfe-commits mailing list