[clang-tools-extra] [clang-tidy] Add flag to specify an alternative to std::move in cppcoreguidelines-rvalue-reference-param-not-moved (PR #138757)
Dimitrije Dobrota via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 30 10:18:02 PDT 2025
https://github.com/DimitrijeDobrota updated https://github.com/llvm/llvm-project/pull/138757
>From 28723cca6270be1cb2ebaddc23e47b3a7b38e6a4 Mon Sep 17 00:00:00 2001
From: Dimitrije Dobrota <mail at dimitrijedobrota.com>
Date: Tue, 6 May 2025 22:42:04 +0200
Subject: [PATCH 1/3] Add flag to specify an alternative to std::move
Since std::move is nothing more than a cast, part of STL and not the
language itself, it's easy to provide a custom implementation if one
wishes not to include the entirety of <utility>.
Added flag (MoveFunction) provides a way to continue using this
essential check even with the custom implementation of moving.
---
.../RvalueReferenceParamNotMovedCheck.cpp | 8 +++++---
.../cppcoreguidelines/RvalueReferenceParamNotMovedCheck.h | 1 +
clang-tools-extra/docs/ReleaseNotes.rst | 5 +++++
.../rvalue-reference-param-not-moved.rst | 5 +++++
4 files changed, 16 insertions(+), 3 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/RvalueReferenceParamNotMovedCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/RvalueReferenceParamNotMovedCheck.cpp
index 8c386d5bc7945..272152644d7dd 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/RvalueReferenceParamNotMovedCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/RvalueReferenceParamNotMovedCheck.cpp
@@ -42,9 +42,9 @@ void RvalueReferenceParamNotMovedCheck::registerMatchers(MatchFinder *Finder) {
StatementMatcher MoveCallMatcher =
callExpr(
argumentCountIs(1),
- anyOf(callee(functionDecl(hasName("::std::move"))),
+ anyOf(callee(functionDecl(hasName(MoveFunction))),
callee(unresolvedLookupExpr(hasAnyDeclaration(
- namedDecl(hasUnderlyingDecl(hasName("::std::move"))))))),
+ namedDecl(hasUnderlyingDecl(hasName(MoveFunction))))))),
hasArgument(
0, argumentOf(
AllowPartialMove,
@@ -122,7 +122,8 @@ RvalueReferenceParamNotMovedCheck::RvalueReferenceParamNotMovedCheck(
AllowPartialMove(Options.get("AllowPartialMove", false)),
IgnoreUnnamedParams(Options.get("IgnoreUnnamedParams", false)),
IgnoreNonDeducedTemplateTypes(
- Options.get("IgnoreNonDeducedTemplateTypes", false)) {}
+ Options.get("IgnoreNonDeducedTemplateTypes", false)),
+ MoveFunction(Options.get("MoveFunction", "::std::move")) {}
void RvalueReferenceParamNotMovedCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
@@ -130,6 +131,7 @@ void RvalueReferenceParamNotMovedCheck::storeOptions(
Options.store(Opts, "IgnoreUnnamedParams", IgnoreUnnamedParams);
Options.store(Opts, "IgnoreNonDeducedTemplateTypes",
IgnoreNonDeducedTemplateTypes);
+ Options.store(Opts, "MoveFunction", MoveFunction);
}
} // namespace clang::tidy::cppcoreguidelines
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/RvalueReferenceParamNotMovedCheck.h b/clang-tools-extra/clang-tidy/cppcoreguidelines/RvalueReferenceParamNotMovedCheck.h
index d8c3d2bd4ba0e..950c0206745d7 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/RvalueReferenceParamNotMovedCheck.h
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/RvalueReferenceParamNotMovedCheck.h
@@ -32,6 +32,7 @@ class RvalueReferenceParamNotMovedCheck : public ClangTidyCheck {
const bool AllowPartialMove;
const bool IgnoreUnnamedParams;
const bool IgnoreNonDeducedTemplateTypes;
+ const StringRef MoveFunction;
};
} // namespace clang::tidy::cppcoreguidelines
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index e50d40b76e8c4..832a2c402a563 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -327,6 +327,11 @@ Changes in existing checks
<clang-tidy/checks/readability/redundant-smartptr-get>` check by fixing
some false positives involving smart pointers to arrays.
+- Improved :doc:`cppcoreguidelines-rvalue-reference-param-not-moved
+ <clang-tidy/checks/cppcoreguidelines/rvalue-reference-param-not-moved>` check
+ by adding a flag to specify the function used for moving instead of
+ ``std::move``.
+
Removed checks
^^^^^^^^^^^^^^
diff --git a/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/rvalue-reference-param-not-moved.rst b/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/rvalue-reference-param-not-moved.rst
index ffa3a9d61e48e..aa2bed9e888a4 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/rvalue-reference-param-not-moved.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/rvalue-reference-param-not-moved.rst
@@ -79,6 +79,11 @@ Options
T other = std::forward<T>(t);
}
+.. option:: MoveFunction
+
+ Specify the function used for moving.
+ Default is `::std::move`.
+
This check implements `F.18
<http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#f18-for-will-move-from-parameters-pass-by-x-and-stdmove-the-parameter>`_
from the C++ Core Guidelines.
>From 648995c897f09604ec066915ab9aa50844474aef Mon Sep 17 00:00:00 2001
From: Dimitrije Dobrota <mail at dimitrijedobrota.com>
Date: Mon, 30 Jun 2025 00:02:43 +0200
Subject: [PATCH 2/3] Fix alphabetical order in ReleaseNotes
---
clang-tools-extra/docs/ReleaseNotes.rst | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 832a2c402a563..ff5f7bdfbdba5 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -212,6 +212,11 @@ Changes in existing checks
<clang-tidy/checks/cppcoreguidelines/avoid-goto>` check by adding the option
`IgnoreMacros` to ignore ``goto`` labels defined in macros.
+- Improved :doc:`cppcoreguidelines-rvalue-reference-param-not-moved
+ <clang-tidy/checks/cppcoreguidelines/rvalue-reference-param-not-moved>` check
+ by adding a flag to specify the function used for moving instead of
+ ``std::move``.
+
- Improved :doc:`cppcoreguidelines-special-member-functions
<clang-tidy/checks/cppcoreguidelines/special-member-functions>` check by
adding the option `IgnoreMacros` to ignore classes defined in macros.
@@ -327,11 +332,6 @@ Changes in existing checks
<clang-tidy/checks/readability/redundant-smartptr-get>` check by fixing
some false positives involving smart pointers to arrays.
-- Improved :doc:`cppcoreguidelines-rvalue-reference-param-not-moved
- <clang-tidy/checks/cppcoreguidelines/rvalue-reference-param-not-moved>` check
- by adding a flag to specify the function used for moving instead of
- ``std::move``.
-
Removed checks
^^^^^^^^^^^^^^
>From 07873f2eb13d54fe5da95100539c2fe2a76388cb Mon Sep 17 00:00:00 2001
From: Dimitrije Dobrota <mail at dimitrijedobrota.com>
Date: Sun, 29 Jun 2025 23:25:19 +0200
Subject: [PATCH 3/3] Add test
---
...erence-param-not-moved-custom-function.cpp | 47 +++++++++++++++++++
1 file changed, 47 insertions(+)
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/rvalue-reference-param-not-moved-custom-function.cpp
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/rvalue-reference-param-not-moved-custom-function.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/rvalue-reference-param-not-moved-custom-function.cpp
new file mode 100644
index 0000000000000..4ed29824496fc
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/rvalue-reference-param-not-moved-custom-function.cpp
@@ -0,0 +1,47 @@
+// RUN: %check_clang_tidy -std=c++11 %s cppcoreguidelines-rvalue-reference-param-not-moved %t -- \
+// RUN: -config="{CheckOptions: {cppcoreguidelines-rvalue-reference-param-not-moved.AllowPartialMove: true, cppcoreguidelines-rvalue-reference-param-not-moved.IgnoreUnnamedParams: true, cppcoreguidelines-rvalue-reference-param-not-moved.IgnoreNonDeducedTemplateTypes: true, cppcoreguidelines-rvalue-reference-param-not-moved.MoveFunction: custom_move}}" -- -fno-delayed-template-parsing
+
+// NOLINTBEGIN
+namespace std {
+template <typename>
+struct remove_reference;
+
+template <typename _Tp> struct remove_reference { typedef _Tp type; };
+template <typename _Tp> struct remove_reference<_Tp&> { typedef _Tp type; };
+template <typename _Tp> struct remove_reference<_Tp&&> { typedef _Tp type; };
+
+template <typename _Tp>
+constexpr typename std::remove_reference<_Tp>::type &&move(_Tp &&__t) noexcept;
+
+template <typename _Tp>
+constexpr _Tp &&
+forward(typename remove_reference<_Tp>::type &__t) noexcept;
+
+}
+// NOLINTEND
+
+
+struct Obj {
+ Obj();
+ Obj(const Obj&);
+ Obj& operator=(const Obj&);
+ Obj(Obj&&);
+ Obj& operator=(Obj&&);
+ void member() const;
+};
+
+template<class T>
+constexpr typename std::remove_reference<T>::type&& custom_move(T&& x) noexcept
+{
+ return static_cast<typename std::remove_reference<T>::type&&>(x);
+}
+
+void move_with_std(Obj&& o) {
+ // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: rvalue reference parameter 'o' is never moved from inside the function body [cppcoreguidelines-rvalue-reference-param-not-moved]
+ Obj other{std::move(o)};
+}
+
+void move_with_custom(Obj&& o) {
+ Obj other{custom_move(o)};
+}
+
More information about the cfe-commits
mailing list