[clang-tools-extra] [clang-tidy] Fix performance-move-const-arg for trivially copyable types with private copy constructor (PR #175449)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Jan 11 09:58:04 PST 2026
https://github.com/zeyi2 created https://github.com/llvm/llvm-project/pull/175449
Closes [#174826](https://github.com/llvm/llvm-project/issues/174826)
>From 522cdc7921a73b281cddf6a79d644903324ac9a1 Mon Sep 17 00:00:00 2001
From: mtx <mitchell.xu2 at gmail.com>
Date: Sat, 10 Jan 2026 16:51:28 +0800
Subject: [PATCH] [clang-tidy] Fix performance-move-const-arg for trivially
copyable types with private copy constructor
---
.../performance/MoveConstArgCheck.cpp | 3 +-
clang-tools-extra/docs/ReleaseNotes.rst | 16 +++++----
.../checkers/performance/move-const-arg.cpp | 34 +++++++++++++++++++
3 files changed, 46 insertions(+), 7 deletions(-)
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 391c3a6b3db79..a9c3547624594 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -111,10 +111,10 @@ Hover
Code completion
^^^^^^^^^^^^^^^
-- Added a new ``MacroFilter`` configuration option to ``Completion`` to
- allow fuzzy-matching with the ``FuzzyMatch`` option when suggesting
- macros. ``ExactPrefix`` is the default, which retains previous
- behavior of suggesting macros which match the prefix exactly.
+- Added a new ``MacroFilter`` configuration option to ``Completion`` to
+ allow fuzzy-matching with the ``FuzzyMatch`` option when suggesting
+ macros. ``ExactPrefix`` is the default, which retains previous
+ behavior of suggesting macros which match the prefix exactly.
Code actions
^^^^^^^^^^^^
@@ -205,7 +205,7 @@ Improvements to clang-tidy
- Improved :program:`clang-tidy` by adding the `--removed-arg` option to remove
arguments sent to the compiler when invoking Clang-Tidy. This option was also
- added to :program:`run-clang-tidy.py` and :program:`clang-tidy-diff.py` and
+ added to :program:`run-clang-tidy.py` and :program:`clang-tidy-diff.py` and
can be configured in the config file through the `RemovedArgs` option.
- Deprecated the :program:`clang-tidy` ``zircon`` module. All checks have been
@@ -364,7 +364,7 @@ New check aliases
keeping initial check as an alias to the new one.
- Renamed :doc:`google-build-namespaces <clang-tidy/checks/google/build-namespaces>` to
- :doc:`misc-anonymous-namespace-in-header
+ :doc:`misc-anonymous-namespace-in-header
<clang-tidy/checks/misc/anonymous-namespace-in-header>`
keeping initial check as an alias to the new one.
@@ -604,6 +604,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 cfe-commits
mailing list