[llvm-branch-commits] [clang-tools-extra] release/22.x: [clang-tidy] Fix performance-move-const-arg for trivially copyable types with private copy constructor (#175449) (PR #176186)
Cullen Rhodes via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Thu Jan 15 11:48:51 PST 2026
https://github.com/c-rhodes updated https://github.com/llvm/llvm-project/pull/176186
>From cc8df4d6a76fde3241e0a6563dc2c3d4905a6398 Mon Sep 17 00:00:00 2001
From: mitchell <mitchell.xu2 at gmail.com>
Date: Thu, 15 Jan 2026 23:13:14 +0800
Subject: [PATCH] [clang-tidy] Fix performance-move-const-arg for trivially
copyable types with private copy constructor (#175449)
Closes [#174826](https://github.com/llvm/llvm-project/issues/174826)
---
.../performance/MoveConstArgCheck.cpp | 3 +-
clang-tools-extra/docs/ReleaseNotes.rst | 4 +++
.../checkers/performance/move-const-arg.cpp | 34 +++++++++++++++++++
3 files changed, 40 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp b/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp
index 91f025dc87d8e..63be104df5e05 100644
--- a/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp
+++ b/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp
@@ -136,7 +136,8 @@ void MoveConstArgCheck::check(const MatchFinder::MatchResult &Result) {
return;
// Don't warn when the type is not copyable.
for (const auto *Ctor : R->ctors())
- if (Ctor->isCopyConstructor() && Ctor->isDeleted())
+ if (Ctor->isCopyConstructor() &&
+ (Ctor->isDeleted() || Ctor->getAccess() != AS_public))
return;
}
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 7747c5d0e96a7..186aa4cad2c8d 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -664,6 +664,10 @@ Changes in existing checks
<clang-tidy/checks/modernize/use-using>` check to correctly provide fix-its
for typedefs of pointers or references to array types.
+- Improved :doc:`performance-move-const-arg
+ <clang-tidy/checks/performance/move-const-arg>` check by avoiding false
+ positives on trivially copyable types with a non-public copy constructor.
+
- Improved :doc:`performance-unnecessary-copy-initialization
<clang-tidy/checks/performance/unnecessary-copy-initialization>` by printing
the type of the diagnosed variable.
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 e616cbe78bc3a..34d51930ac6c8 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
@@ -583,3 +583,37 @@ void testTernaryMove() {
};
} // namespace GH126515
+
+namespace GH174826 {
+
+struct PrivateCopy {
+ PrivateCopy() = default;
+ PrivateCopy(PrivateCopy &&) = default;
+
+private:
+ PrivateCopy(const PrivateCopy &) = default;
+};
+
+void receive(PrivateCopy) {}
+
+void testPrivate() {
+ PrivateCopy v;
+ receive(std::move(v));
+}
+
+struct PublicCopy {
+ PublicCopy() = default;
+ PublicCopy(const PublicCopy &) = default;
+ PublicCopy(PublicCopy &&) = default;
+};
+
+void receive(PublicCopy) {}
+
+void testPublic() {
+ PublicCopy v;
+ receive(std::move(v));
+ // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: std::move of the variable 'v' of the trivially-copyable type 'PublicCopy' has no effect; remove std::move() [performance-move-const-arg]
+ // CHECK-FIXES: receive(v);
+}
+
+} // namespace GH174826
More information about the llvm-branch-commits
mailing list