[clang-tools-extra] [clang-tidy]fix false positives of the result of std::move() is used as rvalue for performance-move-const-arg (PR #95633)

Congcong Cai via cfe-commits cfe-commits at lists.llvm.org
Sat Jun 15 05:56:39 PDT 2024


https://github.com/HerrCai0907 updated https://github.com/llvm/llvm-project/pull/95633

>From 0022050f7b537914dff7adf937103def4c9c939a Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Sat, 15 Jun 2024 05:40:50 +0000
Subject: [PATCH 1/2] [clang-tidy]fix false positives of the result of
 std::move() is used as rvalue for performance-move-const-arg

Fixes: #86404
---
 .../clang-tidy/performance/MoveConstArgCheck.cpp   |  8 +++++++-
 clang-tools-extra/docs/ReleaseNotes.rst            |  4 ++++
 .../checkers/performance/move-const-arg.cpp        | 14 +++++++++-----
 3 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp b/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp
index aa54cf284f627..d29b9e91f2e35 100644
--- a/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp
+++ b/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp
@@ -44,7 +44,13 @@ void MoveConstArgCheck::registerMatchers(MatchFinder *Finder) {
                unless(isInTemplateInstantiation()))
           .bind("call-move");
 
-  Finder->addMatcher(MoveCallMatcher, this);
+  Finder->addMatcher(
+      expr(anyOf(
+          castExpr(hasSourceExpression(MoveCallMatcher)),
+          cxxConstructExpr(hasDeclaration(cxxConstructorDecl(anyOf(
+                               isCopyConstructor(), isMoveConstructor()))),
+                           hasArgument(0, MoveCallMatcher)))),
+      this);
 
   auto ConstTypeParmMatcher =
       qualType(references(isConstQualified())).bind("invocation-parm-type");
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 5d5aecd67b2d7..aeb0d72651c34 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -387,6 +387,10 @@ Changes in existing checks
 - Improved :doc:`modernize-use-using <clang-tidy/checks/modernize/use-using>`
   check by adding support for detection of typedefs declared on function level.
 
+- Improved :doc:`performance-move-const-arg
+  <clang-tidy/checks/performance/move-const-arg>` check ignoring the result
+  of ``std::move()`` is used as rvalue.
+
 - Improved :doc:`performance-unnecessary-copy-initialization
   <clang-tidy/checks/performance/unnecessary-copy-initialization>` check by
   detecting more cases of constant access. In particular, pointers can be
diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp
index 4d90c124ad72c..4505eef6df24b 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp
@@ -114,14 +114,18 @@ void f8() {
 int f9() { return M2(1); }
 
 template <typename T>
-T f10(const int x10) {
+T f_unknown_target(const int x10) {
   return std::move(x10);
-  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the const variable 'x10' of the trivially-copyable type 'const int' has no effect; remove std::move() [performance-move-const-arg]
-  // CHECK-FIXES: return x10;
 }
+
 void f11() {
-  f10<int>(1);
-  f10<double>(1);
+  f_unknown_target<int>(1);
+  f_unknown_target<double>(1);
+}
+
+A&& f_return_right_ref() {
+  static A a{};
+  return std::move(a);
 }
 
 class NoMoveSemantics {

>From e2a8a0dbc63ad4aaceb44ddd62d56bea6142dc7d Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Sat, 15 Jun 2024 20:56:32 +0800
Subject: [PATCH 2/2] Update clang-tools-extra/docs/ReleaseNotes.rst
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Co-authored-by: Danny Mösch <danny.moesch at icloud.com>
---
 clang-tools-extra/docs/ReleaseNotes.rst | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index aeb0d72651c34..7092d0b6fdb02 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -388,8 +388,8 @@ Changes in existing checks
   check by adding support for detection of typedefs declared on function level.
 
 - Improved :doc:`performance-move-const-arg
-  <clang-tidy/checks/performance/move-const-arg>` check ignoring the result
-  of ``std::move()`` is used as rvalue.
+  <clang-tidy/checks/performance/move-const-arg>` check by ignoring
+  ``std::move()`` calls when their target is used as an rvalue.
 
 - Improved :doc:`performance-unnecessary-copy-initialization
   <clang-tidy/checks/performance/unnecessary-copy-initialization>` check by



More information about the cfe-commits mailing list