summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Boehme <mboehme@google.com>2022-06-03 09:08:17 +0200
committerTom Stellard <tstellar@redhat.com>2022-06-09 13:37:09 -0700
commitdeb573739df926c9b7321d0b05994370074aa53c (patch)
tree635aba8f9facd8b4f748dae3e10815755a467a63
parentd0cd5a872f8d0d3720d2428dd420ae9177e6a8f1 (diff)
downloadllvm-deb573739df926c9b7321d0b05994370074aa53c.tar.gz
[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 8b90b2539048a581052a4b0d7628ffba0cd582a9)
-rw-r--r--clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp3
-rw-r--r--clang-tools-extra/docs/ReleaseNotes.rst5
-rw-r--r--clang-tools-extra/test/clang-tidy/checkers/bugprone-use-after-move.cpp7
3 files changed, 14 insertions, 1 deletions
diff --git a/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
index 064b6ae19784..55f7b87f48a3 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
@@ -400,7 +400,8 @@ void UseAfterMoveCheck::registerMatchers(MatchFinder *Finder) {
auto CallMoveMatcher =
callExpr(callee(functionDecl(hasName("::std::move"))), argumentCountIs(1),
hasArgument(0, declRefExpr().bind("arg")),
- anyOf(hasAncestor(lambdaExpr().bind("containing-lambda")),
+ anyOf(hasAncestor(compoundStmt(
+ hasParent(lambdaExpr().bind("containing-lambda")))),
hasAncestor(functionDecl().bind("containing-func"))),
unless(inDecltypeOrTemplateArg()),
// try_emplace is a common maybe-moving function that returns a
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 34b2fc0b588f..30b61805908f 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -360,6 +360,11 @@ Changes in existing checks
<clang-tidy/checks/readability-suspicious-call-argument>` related to passing
arguments that refer to program elements without a trivial identifier.
+- Fixed a bug in :doc:`bugprone-use-after-move
+ <clang-tidy/checks/bugprone-use-after-move> where a move in a lambda capture
+ was treated as if it happened within the body of the lambda, not within the
+ function that defines the lambda.
+
Removed checks
^^^^^^^^^^^^^^
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 e26db0f6793e..d6ce626e99d0 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
@@ -416,6 +416,13 @@ void lambdas() {
auto lambda = [&]() { a.foo(); };
std::move(a);
}
+ {
+ A a;
+ auto lambda = [a = std::move(a)] { a.foo(); };
+ a.foo();
+ // CHECK-NOTES: [[@LINE-1]]:5: warning: 'a' used after it was moved
+ // CHECK-NOTES: [[@LINE-3]]:24: note: move occurred here
+ }
}
// Use-after-moves are detected in uninstantiated templates if the moved type