[clang-tools-extra] [clang-tidy] Fix false positive in bugprone-use-after-move for std::tie (PR #192895)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Apr 19 21:51:27 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tidy
Author: Yuta Nakamura (nakasan617)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/192895.diff
3 Files Affected:
- (modified) clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp (+9)
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+8)
- (modified) clang-tools-extra/test/clang-tidy/checkers/bugprone/use-after-move.cpp (+39)
``````````diff
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
``````````
</details>
https://github.com/llvm/llvm-project/pull/192895
More information about the cfe-commits
mailing list