From deb573739df926c9b7321d0b05994370074aa53c Mon Sep 17 00:00:00 2001 From: Martin Boehme Date: Fri, 3 Jun 2022 09:08:17 +0200 Subject: [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) --- clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp | 3 ++- clang-tools-extra/docs/ReleaseNotes.rst | 5 +++++ .../test/clang-tidy/checkers/bugprone-use-after-move.cpp | 7 +++++++ 3 files changed, 14 insertions(+), 1 deletion(-) 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 ` related to passing arguments that refer to program elements without a trivial identifier. +- Fixed a bug in :doc:`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 -- cgit v1.2.1