[clang-tools-extra] 1b66446 - [clang-tidy] `bugprone-use-after-move`: Don't warn on self-moves.
Martin Boehme via cfe-commits
cfe-commits at lists.llvm.org
Tue Jun 7 03:53:10 PDT 2022
Author: Martin Boehme
Date: 2022-06-07T12:53:03+02:00
New Revision: 1b664460fa4cb507e2af87c48cd269964f3ad747
URL: https://github.com/llvm/llvm-project/commit/1b664460fa4cb507e2af87c48cd269964f3ad747
DIFF: https://github.com/llvm/llvm-project/commit/1b664460fa4cb507e2af87c48cd269964f3ad747.diff
LOG: [clang-tidy] `bugprone-use-after-move`: Don't warn on self-moves.
Reviewed By: sammccall, njames93
Differential Revision: https://reviews.llvm.org/D126853
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 55f7b87f48a3b..c67efa341f629 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
@@ -158,9 +158,12 @@ bool UseAfterMoveFinder::findInternal(const CFGBlock *Block,
// Ignore all reinitializations where the move potentially comes after the
// reinit.
+ // If `Reinit` is identical to `MovingCall`, we're looking at a move-to-self
+ // (e.g. `a = std::move(a)`). Count these as reinitializations.
llvm::SmallVector<const Stmt *, 1> ReinitsToDelete;
for (const Stmt *Reinit : Reinits) {
- if (MovingCall && Sequence->potentiallyAfter(MovingCall, Reinit))
+ if (MovingCall && Reinit != MovingCall &&
+ Sequence->potentiallyAfter(MovingCall, Reinit))
ReinitsToDelete.push_back(Reinit);
}
for (const Stmt *Reinit : ReinitsToDelete) {
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 77eaf8516c829..484babe31eb80 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -207,12 +207,14 @@ Changes in existing checks
- Fixed a crash in :doc:`performance-unnecessary-value-param
<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.
+ template has an unnecessary value parameter. Removed the fix for a template.
+
+- Fixed bugs in :doc:`bugprone-use-after-move
+ <clang-tidy/checks/bugprone-use-after-move>`:
+ - Treat a move in a lambda capture as happening in the function that defines
+ the lambda, not within the body of the lambda (as we were previously doing
+ erroneously).
+ - Don't emit an erroneous warning on self-moves.
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 d6ce626e99d02..281f2083857ad 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
@@ -152,6 +152,13 @@ void simple() {
// CHECK-NOTES: [[@LINE-3]]:15: note: move occurred here
}
+// Don't flag a move-to-self.
+void selfMove() {
+ A a;
+ a = std::move(a);
+ a.foo();
+}
+
// A warning should only be emitted for one use-after-move.
void onlyFlagOneUseAfterMove() {
A a;
More information about the cfe-commits
mailing list