[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
Fri May 1 18:53:26 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 01/11] [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 02/11] 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 03/11] [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 04/11] 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
}
}
>From e345baed3437f2db9465f5ceec3d31a4fbabbe50 Mon Sep 17 00:00:00 2001
From: Yuta Nakamura <yutanak6 at gmail.com>
Date: Sun, 26 Apr 2026 00:03:08 -0700
Subject: [PATCH 05/11] Fix std::tie reinit matcher to strip
MaterializeTemporaryExpr
std::tie(a, b) returns a prvalue tuple; calling operator= on it causes
Clang to insert a MaterializeTemporaryExpr between the binary-operation
node and the callExpr node. hasLHS(callExpr(...)) therefore never matched,
leaving Reinits empty and allowing the loop-back-edge path to report a
false positive in stdTieInLoop.
Wrap the inner callExpr with ignoringImplicit() so the matcher sees
through the MaterializeTemporaryExpr.
---
clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
index 04aa17fb42efe..dd41ca5449899 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
@@ -136,12 +136,14 @@ makeReinitMatcher(const ValueDecl *MovedVariable,
// 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.
+ // ignoringImplicit strips the MaterializeTemporaryExpr that
+ // Clang inserts when calling operator= on the prvalue tuple.
binaryOperation(
hasOperatorName("="),
- hasLHS(
+ hasLHS(ignoringImplicit(
callExpr(callee(functionDecl(hasName("::std::tie"))),
hasAnyArgument(
- ignoringParenImpCasts(DeclRefMatcher))))),
+ 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))),
>From 968af00822f1d43a59d7c18ee32f28d28f4a3cae Mon Sep 17 00:00:00 2001
From: Yuta Nakamura <yutanak6 at gmail.com>
Date: Sun, 26 Apr 2026 00:04:23 -0700
Subject: [PATCH 06/11] Restore original stdTieInLoop test (implementation fix
handles it)
---
.../clang-tidy/checkers/bugprone/use-after-move.cpp | 10 +++-------
1 file changed, 3 insertions(+), 7 deletions(-)
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 857c00df8efa9..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
@@ -1819,14 +1819,10 @@ void stdTiePartialReinit() {
}
void stdTieInLoop() {
- // 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::tie on the LHS reinitializes before the next iteration.
std::string a, b;
while (true) {
- 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
+ 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 8dace60f60a8771d875d22cc3c26dfe20b0ad4a8 Mon Sep 17 00:00:00 2001
From: Yuta Nakamura <44285097+nakasan617 at users.noreply.github.com>
Date: Sun, 26 Apr 2026 10:31:09 -0700
Subject: [PATCH 07/11] Update clang-tools-extra/docs/ReleaseNotes.rst
Co-authored-by: Zeyi Xu <zeyi2 at nekoarch.cc>
---
clang-tools-extra/docs/ReleaseNotes.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index d86f25b67ce02..d07bb3f4b60d2 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -321,7 +321,7 @@ 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
+ - Fix 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.
>From ff79376f917260c764f2caf2e72b872f16027749 Mon Sep 17 00:00:00 2001
From: Yuta Nakamura <yutanak6 at gmail.com>
Date: Sun, 26 Apr 2026 10:41:58 -0700
Subject: [PATCH 08/11] [clang-tidy] Add std::ignore test for
bugprone-use-after-move std::tie
Add test case verifying that std::ignore placeholder does not prevent
reinitialization recognition for named variables in std::tie.
Co-Authored-By: Claude Sonnet 4.6 <noreply at anthropic.com>
---
.../test/clang-tidy/checkers/bugprone/use-after-move.cpp | 8 ++++++++
1 file changed, 8 insertions(+)
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..ffa6bd82742cd 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
@@ -1807,6 +1807,14 @@ void stdTieIsReinit() {
b.size();
}
+void stdTieWithIgnore() {
+ // std::ignore placeholder does not prevent reinitialization of named variables.
+ std::string a, b;
+ std::move(a);
+ std::tie(a, std::ignore) = makeStringPair("x", "y"); // no-warning: a reinitialized
+ a.size();
+}
+
void stdTiePartialReinit() {
// Only variables named in std::tie are reinitialized.
std::string a, b;
>From afa81d2cc538aa8f8609d1a1ae9764a247fe92a5 Mon Sep 17 00:00:00 2001
From: Yuta Nakamura <yutanak6 at gmail.com>
Date: Sun, 26 Apr 2026 10:47:44 -0700
Subject: [PATCH 09/11] [clang-tidy] Add flipped std::ignore position test for
std::tie
Test that std::ignore in the first argument position still correctly
reinitializes named variables in bugprone-use-after-move.
Co-Authored-By: Claude Sonnet 4.6 <noreply at anthropic.com>
---
.../test/clang-tidy/checkers/bugprone/use-after-move.cpp | 8 ++++++++
1 file changed, 8 insertions(+)
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 ffa6bd82742cd..aa188b7a476a3 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,6 +1815,14 @@ void stdTieWithIgnore() {
a.size();
}
+void stdTieIgnoreFlipped() {
+ // std::ignore in first position still reinitializes named variables.
+ std::string a, b;
+ std::move(a);
+ std::tie(std::ignore, a) = makeStringPair("x", "y"); // no-warning: a reinitialized
+ a.size();
+}
+
void stdTiePartialReinit() {
// Only variables named in std::tie are reinitialized.
std::string a, b;
>From c7a350aa706b1f43486c73332aae0337b0df27ed Mon Sep 17 00:00:00 2001
From: Yuta Nakamura <yutanak6 at gmail.com>
Date: Sun, 26 Apr 2026 10:48:12 -0700
Subject: [PATCH 10/11] [clang-tidy] Remove unused variable b from std::ignore
test cases
Co-Authored-By: Claude Sonnet 4.6 <noreply at anthropic.com>
---
.../test/clang-tidy/checkers/bugprone/use-after-move.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
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 aa188b7a476a3..5a01bc903137e 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
@@ -1809,7 +1809,7 @@ void stdTieIsReinit() {
void stdTieWithIgnore() {
// std::ignore placeholder does not prevent reinitialization of named variables.
- std::string a, b;
+ std::string a;
std::move(a);
std::tie(a, std::ignore) = makeStringPair("x", "y"); // no-warning: a reinitialized
a.size();
@@ -1817,7 +1817,7 @@ void stdTieWithIgnore() {
void stdTieIgnoreFlipped() {
// std::ignore in first position still reinitializes named variables.
- std::string a, b;
+ std::string a;
std::move(a);
std::tie(std::ignore, a) = makeStringPair("x", "y"); // no-warning: a reinitialized
a.size();
>From 9c409d83d6fd73b2cf3bee911cfa278a92808880 Mon Sep 17 00:00:00 2001
From: Yuta Nakamura <yutanak6 at gmail.com>
Date: Wed, 29 Apr 2026 23:42:57 -0700
Subject: [PATCH 11/11] [clang-tidy] Fix test comment: use CHECK-NOTES, not
expected-warning
The test file uses FileCheck via %check_clang_tidy, which processes
// CHECK-NOTES: patterns. The // expected-warning{...} format is only
recognised by clang's -verify mode, which is not used here. Remove the
redundant and misleading comment; the adjacent CHECK-NOTES lines already
verify the same diagnostic.
Also remove an extra space before a trailing // comment.
Co-Authored-By: Claude Sonnet 4.6 <noreply at anthropic.com>
---
.../test/clang-tidy/checkers/bugprone/use-after-move.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
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 5a01bc903137e..116e51b155c37 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
@@ -1828,8 +1828,8 @@ void stdTiePartialReinit() {
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}}
+ b.size(); // no-warning: b was reinitialized
+ a.size();
// CHECK-NOTES: [[@LINE-1]]:3: warning: 'a' used after it was moved
// CHECK-NOTES: [[@LINE-5]]:3: note: move occurred here
}
More information about the cfe-commits
mailing list