Skip to content

Commit 8b90b25

Browse files
committed
[clang-tidy] bugprone-use-after-move: Fix handling of moves in lambda captures
Previously, we were treating a move in the lambda capture as if it happened within the body of the lambda, not within the function that defines the lambda. This fixes the same bug as https://reviews.llvm.org/D119165 (which it appears may have been abandoned by the author?) but does so more simply. Reviewed By: njames93 Differential Revision: https://reviews.llvm.org/D126780
1 parent df0f30d commit 8b90b25

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,8 @@ void UseAfterMoveCheck::registerMatchers(MatchFinder *Finder) {
400400
auto CallMoveMatcher =
401401
callExpr(callee(functionDecl(hasName("::std::move"))), argumentCountIs(1),
402402
hasArgument(0, declRefExpr().bind("arg")),
403-
anyOf(hasAncestor(lambdaExpr().bind("containing-lambda")),
403+
anyOf(hasAncestor(compoundStmt(
404+
hasParent(lambdaExpr().bind("containing-lambda")))),
404405
hasAncestor(functionDecl().bind("containing-func"))),
405406
unless(inDecltypeOrTemplateArg()),
406407
// try_emplace is a common maybe-moving function that returns a

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ Changes in existing checks
209209
<clang-tidy/checks/readability-suspicious-call-argument>` when the specialization
210210
template has an unnecessary value paramter. Removed the fix for a template.
211211

212+
- Fixed a bug in :doc:`bugprone-use-after-move
213+
<clang-tidy/checks/bugprone-use-after-move> where a move in a lambda capture
214+
was treated as if it happened within the body of the lambda, not within the
215+
function that defines the lambda.
216+
212217
Removed checks
213218
^^^^^^^^^^^^^^
214219

clang-tools-extra/test/clang-tidy/checkers/bugprone-use-after-move.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,13 @@ void lambdas() {
416416
auto lambda = [&]() { a.foo(); };
417417
std::move(a);
418418
}
419+
{
420+
A a;
421+
auto lambda = [a = std::move(a)] { a.foo(); };
422+
a.foo();
423+
// CHECK-NOTES: [[@LINE-1]]:5: warning: 'a' used after it was moved
424+
// CHECK-NOTES: [[@LINE-3]]:24: note: move occurred here
425+
}
419426
}
420427

421428
// Use-after-moves are detected in uninstantiated templates if the moved type

0 commit comments

Comments
 (0)