1 // RUN: %check_clang_tidy %s performance-move-const-arg %t
2 
3 namespace std {
4 template <typename>
5 struct remove_reference;
6 
7 template <typename _Tp>
8 struct remove_reference {
9   typedef _Tp type;
10 };
11 
12 template <typename _Tp>
13 struct remove_reference<_Tp &> {
14   typedef _Tp type;
15 };
16 
17 template <typename _Tp>
18 struct remove_reference<_Tp &&> {
19   typedef _Tp type;
20 };
21 
22 template <typename _Tp>
move(_Tp && __t)23 constexpr typename std::remove_reference<_Tp>::type &&move(_Tp &&__t) {
24   return static_cast<typename std::remove_reference<_Tp>::type &&>(__t);
25 }
26 
27 template <typename _Tp>
28 constexpr _Tp &&
forward(typename remove_reference<_Tp>::type & __t)29 forward(typename remove_reference<_Tp>::type &__t) noexcept {
30   return static_cast<_Tp &&>(__t);
31 }
32 
33 } // namespace std
34 
35 class A {
36 public:
A()37   A() {}
A(const A & rhs)38   A(const A &rhs) {}
A(A && rhs)39   A(A &&rhs) {}
40 };
41 
42 struct TriviallyCopyable {
43   int i;
44 };
45 
f(TriviallyCopyable)46 void f(TriviallyCopyable) {}
47 
g()48 void g() {
49   TriviallyCopyable obj;
50   f(std::move(obj));
51   // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: std::move of the variable 'obj' of the trivially-copyable type 'TriviallyCopyable' has no effect; remove std::move() [performance-move-const-arg]
52   // CHECK-FIXES: f(obj);
53 }
54 
f1()55 int f1() {
56   return std::move(42);
57   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the expression of the trivially-copyable type 'int' has no effect; remove std::move() [performance-move-const-arg]
58   // CHECK-FIXES: return 42;
59 }
60 
f2(int x2)61 int f2(int x2) {
62   return std::move(x2);
63   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the variable 'x2' of the trivially-copyable type 'int'
64   // CHECK-FIXES: return x2;
65 }
66 
f3(int * x3)67 int *f3(int *x3) {
68   return std::move(x3);
69   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the variable 'x3' of the trivially-copyable type 'int *'
70   // CHECK-FIXES: return x3;
71 }
72 
f4(A x4)73 A f4(A x4) { return std::move(x4); }
74 
f5(const A x5)75 A f5(const A x5) {
76   return std::move(x5);
77   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the const variable 'x5' has no effect; remove std::move() or make the variable non-const [performance-move-const-arg]
78   // CHECK-FIXES: return x5;
79 }
80 
81 template <typename T>
f6(const T x6)82 T f6(const T x6) {
83   return std::move(x6);
84 }
85 
f7()86 void f7() { int a = f6(10); }
87 
88 #define M1(x) x
f8()89 void f8() {
90   const A a;
91   M1(A b = std::move(a);)
92   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: std::move of the const variable 'a' has no effect; remove std::move() or make the variable non-const
93   // CHECK-FIXES: M1(A b = a;)
94 }
95 
96 #define M2(x) std::move(x)
f9()97 int f9() { return M2(1); }
98 
99 template <typename T>
f10(const int x10)100 T f10(const int x10) {
101   return std::move(x10);
102   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the const variable 'x10' of the trivially-copyable type 'const int' has no effect; remove std::move() [performance-move-const-arg]
103   // CHECK-FIXES: return x10;
104 }
f11()105 void f11() {
106   f10<int>(1);
107   f10<double>(1);
108 }
109 
110 class NoMoveSemantics {
111 public:
112   NoMoveSemantics();
113   NoMoveSemantics(const NoMoveSemantics &);
114 
115   NoMoveSemantics &operator=(const NoMoveSemantics &);
116 };
117 
118 void callByConstRef(const NoMoveSemantics &);
119 void callByConstRef(int i, const NoMoveSemantics &);
120 
moveToConstReferencePositives()121 void moveToConstReferencePositives() {
122   NoMoveSemantics obj;
123 
124   // Basic case.
125   callByConstRef(std::move(obj));
126   // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: passing result of std::move() as
127   // CHECK-FIXES: callByConstRef(obj);
128 
129   // Also works for second argument.
130   callByConstRef(1, std::move(obj));
131   // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: passing result of std::move() as
132   // CHECK-FIXES: callByConstRef(1, obj);
133 
134   // Works if std::move() applied to a temporary.
135   callByConstRef(std::move(NoMoveSemantics()));
136   // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: passing result of std::move() as
137   // CHECK-FIXES: callByConstRef(NoMoveSemantics());
138 
139   // Works if calling a copy constructor.
140   NoMoveSemantics other(std::move(obj));
141   // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: passing result of std::move() as
142   // CHECK-FIXES: NoMoveSemantics other(obj);
143 
144   // Works if calling assignment operator.
145   other = std::move(obj);
146   // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: passing result of std::move() as
147   // CHECK-FIXES: other = obj;
148 }
149 
150 class MoveSemantics {
151 public:
152   MoveSemantics();
153   MoveSemantics(MoveSemantics &&);
154 
155   MoveSemantics &operator=(MoveSemantics &&);
156 };
157 
158 void callByValue(MoveSemantics);
159 
160 void callByRValueRef(MoveSemantics &&);
161 
162 template <class T>
templateFunction(T obj)163 void templateFunction(T obj) {
164   T other = std::move(obj);
165 }
166 
167 #define M3(T, obj)            \
168   do {                        \
169     T other = std::move(obj); \
170   } while (true)
171 
172 #define CALL(func) (func)()
173 
moveToConstReferenceNegatives()174 void moveToConstReferenceNegatives() {
175   // No warning when actual move takes place.
176   MoveSemantics move_semantics;
177   callByValue(std::move(move_semantics));
178   callByRValueRef(std::move(move_semantics));
179   MoveSemantics other(std::move(move_semantics));
180   other = std::move(move_semantics);
181 
182   // No warning if std::move() not used.
183   NoMoveSemantics no_move_semantics;
184   callByConstRef(no_move_semantics);
185 
186   // No warning if instantiating a template.
187   templateFunction(no_move_semantics);
188 
189   // No warning inside of macro expansions.
190   M3(NoMoveSemantics, no_move_semantics);
191 
192   // No warning inside of macro expansion, even if the macro expansion is inside
193   // a lambda that is, in turn, an argument to a macro.
194   CALL([no_move_semantics] { M3(NoMoveSemantics, no_move_semantics); });
195 
196   auto lambda = [] {};
197   auto lambda2 = std::move(lambda);
198 }
199 
200 class MoveOnly {
201 public:
202   MoveOnly(const MoveOnly &other) = delete;
203   MoveOnly &operator=(const MoveOnly &other) = delete;
204   MoveOnly(MoveOnly &&other) = default;
205   MoveOnly &operator=(MoveOnly &&other) = default;
206 };
207 template <class T>
208 void Q(T);
moveOnlyNegatives(MoveOnly val)209 void moveOnlyNegatives(MoveOnly val) {
210   Q(std::move(val));
211 }
212 
213 void fmovable(MoveSemantics);
214 
lambda1()215 void lambda1() {
216   auto f = [](MoveSemantics m) {
217     fmovable(std::move(m));
218   };
219   f(MoveSemantics());
220 }
221 
222 template<class T> struct function {};
223 
224 template<typename Result, typename... Args>
225 class function<Result(Args...)> {
226 public:
227   function() = default;
operator ()(Args...args) const228   void operator()(Args... args) const {
229     fmovable(std::forward<Args>(args)...);
230   }
231 };
232 
functionInvocation()233 void functionInvocation() {
234   function<void(MoveSemantics)> callback;
235   MoveSemantics m;
236   callback(std::move(m));
237 }
238 
lambda2()239 void lambda2() {
240   function<void(MoveSemantics)> callback;
241 
242   auto f = [callback = std::move(callback)](MoveSemantics m) mutable {
243     // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: std::move of the variable 'callback' of the trivially-copyable type 'function<void (MoveSemantics)>' has no effect; remove std::move()
244     // CHECK-FIXES: auto f = [callback = callback](MoveSemantics m) mutable {
245     callback(std::move(m));
246   };
247   f(MoveSemantics());
248 }
249