Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -122,14 +122,16 @@ 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) {
Options.store(Opts, "AllowPartialMove", AllowPartialMove);
Options.store(Opts, "IgnoreUnnamedParams", IgnoreUnnamedParams);
Options.store(Opts, "IgnoreNonDeducedTemplateTypes",
IgnoreNonDeducedTemplateTypes);
Options.store(Opts, "MoveFunction", MoveFunction);
}

} // namespace clang::tidy::cppcoreguidelines
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class RvalueReferenceParamNotMovedCheck : public ClangTidyCheck {
const bool AllowPartialMove;
const bool IgnoreUnnamedParams;
const bool IgnoreNonDeducedTemplateTypes;
const StringRef MoveFunction;
};

} // namespace clang::tidy::cppcoreguidelines
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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)};
}