[llvm-branch-commits] [clang-tools-extra] deb5737 - [clang-tidy] `bugprone-use-after-move`: Fix handling of moves in lambda captures

Tom Stellard via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Jun 9 13:38:09 PDT 2022


Author: Martin Boehme
Date: 2022-06-09T13:37:09-07:00
New Revision: deb573739df926c9b7321d0b05994370074aa53c

URL: https://github.com/llvm/llvm-project/commit/deb573739df926c9b7321d0b05994370074aa53c
DIFF: https://github.com/llvm/llvm-project/commit/deb573739df926c9b7321d0b05994370074aa53c.diff

LOG: [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)

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/test/clang-tidy/checkers/bugprone-use-after-move.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
index 064b6ae19784e..55f7b87f48a3b 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 34b2fc0b588f5..30b61805908f8 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 e26db0f6793e0..d6ce626e99d02 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


        


More information about the llvm-branch-commits mailing list