[clang-tools-extra] 8b90b25 - [clang-tidy] `bugprone-use-after-move`: Fix handling of moves in lambda captures

Martin Boehme via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 3 00:34:16 PDT 2022


Author: Martin Boehme
Date: 2022-06-03T09:34:09+02:00
New Revision: 8b90b2539048a581052a4b0d7628ffba0cd582a9

URL: https://github.com/llvm/llvm-project/commit/8b90b2539048a581052a4b0d7628ffba0cd582a9
DIFF: https://github.com/llvm/llvm-project/commit/8b90b2539048a581052a4b0d7628ffba0cd582a9.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

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 c709f6d35e155..a3e616357398c 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -209,6 +209,11 @@ Changes in existing checks
   <clang-tidy/checks/readability-suspicious-call-argument>` when the specialization
   template has an unnecessary value paramter. Removed the fix for a template.
 
+- 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 cfe-commits mailing list