[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
Sat Apr 25 23:58:15 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 1/4] [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
+ }
+}
>From 2ecac36519f282296e88cce9ab85d470a5e3da3c Mon Sep 17 00:00:00 2001
From: Yuta Nakamura <44285097+nakasan617 at users.noreply.github.com>
Date: Mon, 20 Apr 2026 20:37:50 -0700
Subject: [PATCH 2/4] Update clang-tools-extra/docs/ReleaseNotes.rst
Co-authored-by: EugeneZelenko <eugene.zelenko at gmail.com>
---
clang-tools-extra/docs/ReleaseNotes.rst | 1 -
1 file changed, 1 deletion(-)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 1a6464b373e36..0973cac81004b 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -237,7 +237,6 @@ Changes in existing checks
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.
>From 26d176b3d26c5a3fc43644fe7e785a9b500e5c29 Mon Sep 17 00:00:00 2001
From: Yuta Nakamura <yutanak6 at gmail.com>
Date: Sat, 25 Apr 2026 17:43:33 -0700
Subject: [PATCH 3/4] [clang-tidy] Fix off-by-one in CHECK-NOTES for
stdTiePartialReinit test
@LINE-4 from the check line resolved to the std::tie assignment, not
the std::move. Change to @LINE-5 to correctly reference std::move(a).
---
.../test/clang-tidy/checkers/bugprone/use-after-move.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
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 bd4676e9e3af9..6c5daedf6cfcd 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
@@ -1815,7 +1815,7 @@ void stdTiePartialReinit() {
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
+ // CHECK-NOTES: [[@LINE-5]]:3: note: move occurred here
}
void stdTieInLoop() {
>From e32eb20d45d83bed4d63d7f749f2ce6d2e0c4b79 Mon Sep 17 00:00:00 2001
From: Yuta Nakamura <yutanak6 at gmail.com>
Date: Sat, 25 Apr 2026 23:54:18 -0700
Subject: [PATCH 4/4] Fix alphabetical ordering and stdTieInLoop test for
use-after-move
Move the bugprone-use-after-move std::tie entry into the existing
use-after-move bullet (was incorrectly placed before bugprone-argument-comment).
Rewrite stdTieInLoop to avoid the self-referential pattern
(std::tie(a,b) = f(std::move(a),std::move(b))) which correctly fires a
within-statement use-after-move. The new test exercises the same reinit
semantics without triggering that case.
---
clang-tools-extra/docs/ReleaseNotes.rst | 12 +++++-------
.../clang-tidy/checkers/bugprone/use-after-move.cpp | 10 +++++++---
2 files changed, 12 insertions(+), 10 deletions(-)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 70696eb4e23a1..d86f25b67ce02 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -230,13 +230,6 @@ 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.
@@ -328,6 +321,11 @@ Changes in existing checks
- Avoid false positives when moving object to a base type then accessing
non-base members.
+ - 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:`cppcoreguidelines-avoid-capturing-lambda-coroutines
<clang-tidy/checks/cppcoreguidelines/avoid-capturing-lambda-coroutines>`
check by adding the `AllowExplicitObjectParameters` option. When enabled,
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 6c5daedf6cfcd..857c00df8efa9 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
@@ -1819,10 +1819,14 @@ void stdTiePartialReinit() {
}
void stdTieInLoop() {
- // std::tie on the LHS reinitializes before the next iteration.
+ // std::tie reinitializes a and b so subsequent uses in the same iteration
+ // and across loop iterations do not produce use-after-move warnings.
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
+ std::move(a);
+ std::move(b);
+ std::tie(a, b) = makeStringPair("x", "y"); // reinitializes a and b
+ a.size(); // no-warning: reinitialized by tie above
+ b.size(); // no-warning: reinitialized by tie above
}
}
More information about the cfe-commits
mailing list