1// RUN: %check_clang_tidy %s performance-move-const-arg %t \
2// RUN: -config='{CheckOptions: \
3// RUN: {performance-move-const-arg.CheckTriviallyCopyableMove: false}}'
4
5namespace std {
6
7template <typename> struct remove_reference;
8template <typename _Tp> struct remove_reference { typedef _Tp type; };
9template <typename _Tp> struct remove_reference<_Tp &> { typedef _Tp type; };
10template <typename _Tp> struct remove_reference<_Tp &&> { typedef _Tp type; };
11
12template <typename _Tp>
13constexpr typename std::remove_reference<_Tp>::type &&move(_Tp &&__t) {
14 return static_cast<typename std::remove_reference<_Tp>::type &&>(__t);
15}
16
17template <typename _Tp>
18constexpr _Tp &&
19forward(typename remove_reference<_Tp>::type &__t) noexcept {
20 return static_cast<_Tp &&>(__t);
21}
22
23} // namespace std
24
25class NoMoveSemantics {
26 public:
27 NoMoveSemantics();
28 NoMoveSemantics(const NoMoveSemantics &);
29
30 NoMoveSemantics &operator=(const NoMoveSemantics &);
31};
32
33void callByConstRef(const NoMoveSemantics &);
34void callByConstRef(int i, const NoMoveSemantics &);
35
36void moveToConstReferencePositives() {
37 NoMoveSemantics obj;
38
39 // Basic case. It is here just to have a single "detected and fixed" case.
40 callByConstRef(std::move(obj));
41 // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: passing result of std::move() as a const reference argument; no move will actually happen [performance-move-const-arg]
42 // CHECK-FIXES: callByConstRef(obj);
43}
44
45struct TriviallyCopyable {
46 int i;
47};
48
49void f(TriviallyCopyable) {}
50
51void g() {
52 TriviallyCopyable obj;
53 f(std::move(obj));
54}
55
56class MoveSemantics {
57 public:
58 MoveSemantics();
59 MoveSemantics(MoveSemantics &&);
60
61 MoveSemantics &operator=(MoveSemantics &&);
62};
63
64void fmovable(MoveSemantics);
65
66void lambda1() {
67 auto f = [](MoveSemantics m) {
68 fmovable(std::move(m));
69 };
70 f(MoveSemantics());
71}
72
73template<class T> struct function {};
74
75template<typename Result, typename... Args>
76class function<Result(Args...)> {
77public:
78 function() = default;
79 void operator()(Args... args) const {
80 fmovable(std::forward<Args>(args)...);
81 }
82};
83
84void functionInvocation() {
85 function<void(MoveSemantics)> callback;
86 MoveSemantics m;
87 callback(std::move(m));
88}
89
90void lambda2() {
91 function<void(MoveSemantics)> callback;
92
93 auto f = [callback = std::move(callback)](MoveSemantics m) mutable {
94 callback(std::move(m));
95 };
96 f(MoveSemantics());
97}
98

source code of clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg-trivially-copyable.cpp