Skip to content

Commit deb5737

Browse files
martinboehmetstellar
authored andcommitted
[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 (cherry picked from commit 8b90b25)
1 parent d0cd5a8 commit deb5737

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
@@ -360,6 +360,11 @@ Changes in existing checks
360360
<clang-tidy/checks/readability-suspicious-call-argument>` related to passing
361361
arguments that refer to program elements without a trivial identifier.
362362

363+
- Fixed a bug in :doc:`bugprone-use-after-move
364+
<clang-tidy/checks/bugprone-use-after-move> where a move in a lambda capture
365+
was treated as if it happened within the body of the lambda, not within the
366+
function that defines the lambda.
367+
363368
Removed checks
364369
^^^^^^^^^^^^^^
365370

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)