diff options
author | Martin Boehme <mboehme@google.com> | 2022-06-03 09:08:17 +0200 |
---|---|---|
committer | Tom Stellard <tstellar@redhat.com> | 2022-06-09 13:37:09 -0700 |
commit | deb573739df926c9b7321d0b05994370074aa53c (patch) | |
tree | 635aba8f9facd8b4f748dae3e10815755a467a63 | |
parent | d0cd5a872f8d0d3720d2428dd420ae9177e6a8f1 (diff) | |
download | llvm-deb573739df926c9b7321d0b05994370074aa53c.zip llvm-deb573739df926c9b7321d0b05994370074aa53c.tar.gz llvm-deb573739df926c9b7321d0b05994370074aa53c.tar.bz2 |
[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)
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 064b6ae..55f7b87 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 34b2fc0..30b6180 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 e26db0f..d6ce626 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 |