Debian Codegraph @

clang-tools-extra/test/clang-tidy/checkers/bugprone/use-after-move.cpp

raw
1 // RUN: %check_clang_tidy -std=c++11 -check-suffixes=,CXX11 %s bugprone-use-after-move %t -- -- -fno-delayed-template-parsing
2 // RUN: %check_clang_tidy -std=c++17-or-later %s bugprone-use-after-move %t -- -- -fno-delayed-template-parsing
3
4 typedef decltype(nullptr) nullptr_t;
5
6 namespace std {
7 typedef unsigned size_t;
8
9 template <typename T>
10 struct unique_ptr {
11 unique_ptr();
12 T *get() const;
13 explicit operator bool() const;
14 void reset(T *ptr);
15 T &operator*() const;
16 T *operator->() const;
17 T& operator[](size_t i) const;
18 };
19
20 template <typename T>
21 struct shared_ptr {
22 shared_ptr();
23 T *get() const;
24 explicit operator bool() const;
25 void reset(T *ptr);
26 T &operator*() const;
27 T *operator->() const;
28 };
29
30 template <typename T>
31 struct weak_ptr {
32 weak_ptr();
33 bool expired() const;
34 };
35
36 template <typename T1, typename T2>
37 struct pair {};
38
39 template <typename Key, typename T>
40 struct map {
41 struct iterator {};
42
43 map();
44 void clear();
45 bool empty();
46 template <class... Args>
47 pair<iterator, bool> try_emplace(const Key &key, Args &&...args);
48 };
49
50 template <typename Key, typename T>
51 struct unordered_map {
52 struct iterator {};
53
54 unordered_map();
55 void clear();
56 bool empty();
57 template <class... Args>
58 pair<iterator, bool> try_emplace(const Key &key, Args &&...args);
59 };
60
61 #define DECLARE_STANDARD_CONTAINER(name) \
62 template <typename T> \
63 struct name { \
64 name(); \
65 void clear(); \
66 bool empty(); \
67 }
68
69 #define DECLARE_STANDARD_CONTAINER_WITH_ASSIGN(name) \
70 template <typename T> \
71 struct name { \
72 name(); \
73 void clear(); \
74 bool empty(); \
75 void assign(size_t, const T &); \
76 }
77
78 DECLARE_STANDARD_CONTAINER_WITH_ASSIGN(basic_string);
79 DECLARE_STANDARD_CONTAINER_WITH_ASSIGN(vector);
80 DECLARE_STANDARD_CONTAINER_WITH_ASSIGN(deque);
81 DECLARE_STANDARD_CONTAINER_WITH_ASSIGN(forward_list);
82 DECLARE_STANDARD_CONTAINER_WITH_ASSIGN(list);
83 DECLARE_STANDARD_CONTAINER(set);
84 DECLARE_STANDARD_CONTAINER(multiset);
85 DECLARE_STANDARD_CONTAINER(multimap);
86 DECLARE_STANDARD_CONTAINER(unordered_set);
87 DECLARE_STANDARD_CONTAINER(unordered_multiset);
88 DECLARE_STANDARD_CONTAINER(unordered_multimap);
89
90 typedef basic_string<char> string;
91
92 template <typename>
93 struct remove_reference;
94
95 template <typename _Tp>
96 struct remove_reference {
97 typedef _Tp type;
98 };
99
100 template <typename _Tp>
101 struct remove_reference<_Tp &> {
102 typedef _Tp type;
103 };
104
105 template <typename _Tp>
106 struct remove_reference<_Tp &&> {
107 typedef _Tp type;
108 };
109
110 template <typename _Tp>
111 constexpr typename std::remove_reference<_Tp>::type &&move(_Tp &&__t) noexcept {
112 return static_cast<typename remove_reference<_Tp>::type &&>(__t);
113 }
114
115 template <class _Tp>
116 constexpr _Tp&&
117 forward(typename std::remove_reference<_Tp>::type& __t) noexcept {
118 return static_cast<_Tp&&>(__t);
119 }
120
121 template <class _Tp>
122 constexpr _Tp&&
123 forward(typename std::remove_reference<_Tp>::type&& __t) noexcept {
124 return static_cast<_Tp&&>(__t);
125 }
126
127 } // namespace std
128
129 class A {
130 public:
131 A();
132 A(const A &);
133 A(A &&);
134
135 A &operator=(const A &);
136 A &operator=(A &&);
137
138 void foo() const;
139 void bar(int i) const;
140 int getInt() const;
141
142 operator bool() const;
143
144 int i;
145 };
146
147 template <class T>
148 class AnnotatedContainer {
149 public:
150 AnnotatedContainer();
151
152 void foo() const;
153 [[clang::reinitializes]] void clear();
154 };
155
156 ////////////////////////////////////////////////////////////////////////////////
157 // General tests.
158
159 // Simple case.
160 void simple() {
161 A a;
162 a.foo();
163 A other_a = std::move(a);
164 a.foo();
165 // CHECK-NOTES: [[@LINE-1]]:3: warning: 'a' used after it was moved
166 // CHECK-NOTES: [[@LINE-3]]:15: note: move occurred here
167 }
168
169 // Don't flag a move-to-self.
170 void selfMove() {
171 A a;
172 a = std::move(a);
173 a.foo();
174 }
175
176 // A warning should only be emitted for one use-after-move.
177 void onlyFlagOneUseAfterMove() {
178 A a;
179 a.foo();
180 A other_a = std::move(a);
181 a.foo();
182 // CHECK-NOTES: [[@LINE-1]]:3: warning: 'a' used after it was moved
183 // CHECK-NOTES: [[@LINE-3]]:15: note: move occurred here
184 a.foo();
185 }
186
187 void moveAfterMove() {
188 // Move-after-move also counts as a use.
189 {
190 A a;
191 std::move(a);
192 std::move(a);
193 // CHECK-NOTES: [[@LINE-1]]:15: warning: 'a' used after it was moved
194 // CHECK-NOTES: [[@LINE-3]]:5: note: move occurred here
195 }
196 // This is also true if the move itself turns into the use on the second loop
197 // iteration.
198 {
199 A a;
200 for (int i = 0; i < 10; ++i) {
201 std::move(a);
202 // CHECK-NOTES: [[@LINE-1]]:17: warning: 'a' used after it was moved
203 // CHECK-NOTES: [[@LINE-2]]:7: note: move occurred here
204 // CHECK-NOTES: [[@LINE-3]]:17: note: the use happens in a later loop
205 }
206 }
207 }
208
209 // Checks also works on function parameters that have a use-after move.
210 void parameters(A a) {
211 std::move(a);
212 a.foo();
213 // CHECK-NOTES: [[@LINE-1]]:3: warning: 'a' used after it was moved
214 // CHECK-NOTES: [[@LINE-3]]:3: note: move occurred here
215 }
216
217 void standardSmartPtr() {
218 // std::unique_ptr<>, std::shared_ptr<> and std::weak_ptr<> are guaranteed to
219 // be null after a std::move. So the check only flags accesses that would
220 // dereference the pointer.
221 {
222 std::unique_ptr<A> ptr;
223 std::move(ptr);
224 ptr.get();
225 static_cast<bool>(ptr);
226 *ptr;
227 // CHECK-NOTES: [[@LINE-1]]:6: warning: 'ptr' used after it was moved
228 // CHECK-NOTES: [[@LINE-5]]:5: note: move occurred here
229 }
230 {
231 std::unique_ptr<A> ptr;
232 std::move(ptr);
233 ptr->foo();
234 // CHECK-NOTES: [[@LINE-1]]:5: warning: 'ptr' used after it was moved
235 // CHECK-NOTES: [[@LINE-3]]:5: note: move occurred here
236 }
237 {
238 std::unique_ptr<A> ptr;
239 std::move(ptr);
240 ptr[0];
241 // CHECK-NOTES: [[@LINE-1]]:5: warning: 'ptr' used after it was moved
242 // CHECK-NOTES: [[@LINE-3]]:5: note: move occurred here
243 }
244 {
245 std::shared_ptr<A> ptr;
246 std::move(ptr);
247 ptr.get();
248 static_cast<bool>(ptr);
249 *ptr;
250 // CHECK-NOTES: [[@LINE-1]]:6: warning: 'ptr' used after it was moved
251 // CHECK-NOTES: [[@LINE-5]]:5: note: move occurred here
252 }
253 {
254 std::shared_ptr<A> ptr;
255 std::move(ptr);
256 ptr->foo();
257 // CHECK-NOTES: [[@LINE-1]]:5: warning: 'ptr' used after it was moved
258 // CHECK-NOTES: [[@LINE-3]]:5: note: move occurred here
259 }
260 {
261 // std::weak_ptr<> cannot be dereferenced directly, so we only check that
262 // member functions may be called on it after a move.
263 std::weak_ptr<A> ptr;
264 std::move(ptr);
265 ptr.expired();
266 }
267 // Make sure we recognize std::unique_ptr<> or std::shared_ptr<> if they're
268 // wrapped in a typedef.
269 {
270 typedef std::unique_ptr<A> PtrToA;
271 PtrToA ptr;
272 std::move(ptr);
273 ptr.get();
274 }
275 {
276 typedef std::shared_ptr<A> PtrToA;
277 PtrToA ptr;
278 std::move(ptr);
279 ptr.get();
280 }
281 // And we don't get confused if the template argument is a little more
282 // involved.
283 {
284 struct B {
285 typedef A AnotherNameForA;
286 };
287 std::unique_ptr<B::AnotherNameForA> ptr;
288 std::move(ptr);
289 ptr.get();
290 }
291 // Make sure we treat references to smart pointers correctly.
292 {
293 std::unique_ptr<A> ptr;
294 std::unique_ptr<A>& ref_to_ptr = ptr;
295 std::move(ref_to_ptr);
296 ref_to_ptr.get();
297 }
298 {
299 std::unique_ptr<A> ptr;
300 std::unique_ptr<A>&& rvalue_ref_to_ptr = std::move(ptr);
301 std::move(rvalue_ref_to_ptr);
302 rvalue_ref_to_ptr.get();
303 }
304 // We don't give any special treatment to types that are called "unique_ptr"
305 // or "shared_ptr" but are not in the "::std" namespace.
306 {
307 struct unique_ptr {
308 void get();
309 } ptr;
310 std::move(ptr);
311 ptr.get();
312 // CHECK-NOTES: [[@LINE-1]]:5: warning: 'ptr' used after it was moved
313 // CHECK-NOTES: [[@LINE-3]]:5: note: move occurred here
314 }
315 }
316
317 // The check also works in member functions.
318 class Container {
319 void useAfterMoveInMemberFunction() {
320 A a;
321 std::move(a);
322 a.foo();
323 // CHECK-NOTES: [[@LINE-1]]:5: warning: 'a' used after it was moved
324 // CHECK-NOTES: [[@LINE-3]]:5: note: move occurred here
325 }
326 };
327
328 // We see the std::move() if it's inside a declaration.
329 void moveInDeclaration() {
330 A a;
331 A another_a(std::move(a));
332 a.foo();
333 // CHECK-NOTES: [[@LINE-1]]:3: warning: 'a' used after it was moved
334 // CHECK-NOTES: [[@LINE-3]]:5: note: move occurred here
335 }
336
337 // We see the std::move if it's inside an initializer list. Initializer lists
338 // are a special case because they cause ASTContext::getParents() to return
339 // multiple parents for certain nodes in their subtree. This is because
340 // RecursiveASTVisitor visits both the syntactic and semantic forms of
341 // InitListExpr, and the parent-child relationships are different between the
342 // two forms.
343 void moveInInitList() {
344 struct S {
345 A a;
346 };
347 A a;
348 S s{std::move(a)};
349 a.foo();
350 // CHECK-NOTES: [[@LINE-1]]:3: warning: 'a' used after it was moved
351 // CHECK-NOTES: [[@LINE-3]]:7: note: move occurred here
352 }
353
354 void lambdas() {
355 // Use-after-moves inside a lambda should be detected.
356 {
357 A a;
358 auto lambda = [a] {
359 std::move(a);
360 a.foo();
361 // CHECK-NOTES: [[@LINE-1]]:7: warning: 'a' used after it was moved
362 // CHECK-NOTES: [[@LINE-3]]:7: note: move occurred here
363 };
364 }
365 // This is just as true if the variable was declared inside the lambda.
366 {
367 auto lambda = [] {
368 A a;
369 std::move(a);
370 a.foo();
371 // CHECK-NOTES: [[@LINE-1]]:7: warning: 'a' used after it was moved
372 // CHECK-NOTES: [[@LINE-3]]:7: note: move occurred here
373 };
374 }
375 // But don't warn if the move happened inside the lambda but the use happened
376 // outside -- because
377 // - the 'a' inside the lambda is a copy, and
378 // - we don't know when the lambda will get called anyway
379 {
380 A a;
381 auto lambda = [a] {
382 std::move(a);
383 };
384 a.foo();
385 }
386 // Don't warn if 'a' is a copy inside a synchronous lambda
387 {
388 A a;
389 A copied{[a] mutable { return std::move(a); }()};
390 a.foo();
391 }
392 // False negative (should warn if 'a' is a ref inside a synchronous lambda)
393 {
394 A a;
395 A moved{[&a] mutable { return std::move(a); }()};
396 a.foo();
397 }
398 // Warn if the use consists of a capture that happens after a move.
399 {
400 A a;
401 std::move(a);
402 auto lambda = [a]() { a.foo(); };
403 // CHECK-NOTES: [[@LINE-1]]:20: warning: 'a' used after it was moved
404 // CHECK-NOTES: [[@LINE-3]]:5: note: move occurred here
405 }
406 // ...even if the capture was implicit.
407 {
408 A a;
409 std::move(a);
410 auto lambda = [=]() { a.foo(); };
411 // CHECK-NOTES: [[@LINE-1]]:20: warning: 'a' used after it was moved
412 // CHECK-NOTES: [[@LINE-3]]:5: note: move occurred here
413 }
414 // Same tests but for capture by reference.
415 {
416 A a;
417 std::move(a);
418 auto lambda = [&a]() { a.foo(); };
419 // CHECK-NOTES: [[@LINE-1]]:21: warning: 'a' used after it was moved
420 // CHECK-NOTES: [[@LINE-3]]:5: note: move occurred here
421 }
422 {
423 A a;
424 std::move(a);
425 auto lambda = [&]() { a.foo(); };
426 // CHECK-NOTES: [[@LINE-1]]:20: warning: 'a' used after it was moved
427 // CHECK-NOTES: [[@LINE-3]]:5: note: move occurred here
428 }
429 // But don't warn if the move happened after the capture.
430 {
431 A a;
432 auto lambda = [a]() { a.foo(); };
433 std::move(a);
434 }
435 // ...and again, same thing with an implicit move.
436 {
437 A a;
438 auto lambda = [=]() { a.foo(); };
439 std::move(a);
440 }
441 // Same tests but for capture by reference.
442 {
443 A a;
444 auto lambda = [&a]() { a.foo(); };
445 std::move(a);
446 }
447 {
448 A a;
449 auto lambda = [&]() { a.foo(); };
450 std::move(a);
451 }
452 {
453 A a;
454 auto lambda = [a = std::move(a)] { a.foo(); };
455 a.foo();
456 // CHECK-NOTES: [[@LINE-1]]:5: warning: 'a' used after it was moved
457 // CHECK-NOTES: [[@LINE-3]]:24: note: move occurred here
458 }
459 }
460
461 // Use-after-moves are detected in uninstantiated templates if the moved type
462 // is not a dependent type.
463 template <class T>
464 void movedTypeIsNotDependentType() {
465 T t;
466 A a;
467 std::move(a);
468 a.foo();
469 // CHECK-NOTES: [[@LINE-1]]:3: warning: 'a' used after it was moved
470 // CHECK-NOTES: [[@LINE-3]]:3: note: move occurred here
471 }
472
473 // And if the moved type is a dependent type, the use-after-move is detected if
474 // the template is instantiated.
475 template <class T>
476 void movedTypeIsDependentType() {
477 T t;
478 std::move(t);
479 t.foo();
480 // CHECK-NOTES: [[@LINE-1]]:3: warning: 't' used after it was moved
481 // CHECK-NOTES: [[@LINE-3]]:3: note: move occurred here
482 }
483 template void movedTypeIsDependentType<A>();
484
485 // We handle the case correctly where the move consists of an implicit call
486 // to a conversion operator.
487 void implicitConversionOperator() {
488 struct Convertible {
489 operator A() && { return A(); }
490 };
491 void takeA(A a);
492
493 Convertible convertible;
494 takeA(std::move(convertible));
495 convertible;
496 // CHECK-NOTES: [[@LINE-1]]:3: warning: 'convertible' used after it was moved
497 // CHECK-NOTES: [[@LINE-3]]:9: note: move occurred here
498 }
499
500 // Using decltype on an expression is not a use.
501 void decltypeIsNotUse() {
502 A a;
503 std::move(a);
504 decltype(a) other_a;
505 }
506
507 // Ignore moves or uses that occur as part of template arguments.
508 template <int>
509 class ClassTemplate {
510 public:
511 void foo(A a);
512 };
513 template <int>
514 void functionTemplate(A a);
515 void templateArgIsNotUse() {
516 {
517 // A pattern like this occurs in the EXPECT_EQ and ASSERT_EQ macros in
518 // Google Test.
519 A a;
520 ClassTemplate<sizeof(A(std::move(a)))>().foo(std::move(a));
521 }
522 {
523 A a;
524 functionTemplate<sizeof(A(std::move(a)))>(std::move(a));
525 }
526 }
527
528 // Ignore moves of global variables.
529 A global_a;
530 void ignoreGlobalVariables() {
531 std::move(global_a);
532 global_a.foo();
533 }
534
535 // Ignore moves of member variables.
536 class IgnoreMemberVariables {
537 A a;
538 static A static_a;
539
540 void f() {
541 std::move(a);
542 a.foo();
543
544 std::move(static_a);
545 static_a.foo();
546 }
547 };
548
549 // Ignore moves that happen in a try_emplace.
550 void ignoreMoveInTryEmplace() {
551 {
552 std::map<int, A> amap;
553 A a;
554 amap.try_emplace(1, std::move(a));
555 a.foo();
556 }
557 {
558 std::unordered_map<int, A> amap;
559 A a;
560 amap.try_emplace(1, std::move(a));
561 a.foo();
562 }
563 }
564
565 ////////////////////////////////////////////////////////////////////////////////
566 // Tests involving control flow.
567
568 void useAndMoveInLoop() {
569 // Warn about use-after-moves if they happen in a later loop iteration than
570 // the std::move().
571 {
572 A a;
573 for (int i = 0; i < 10; ++i) {
574 a.foo();
575 // CHECK-NOTES: [[@LINE-1]]:7: warning: 'a' used after it was moved
576 // CHECK-NOTES: [[@LINE+2]]:7: note: move occurred here
577 // CHECK-NOTES: [[@LINE-3]]:7: note: the use happens in a later loop
578 std::move(a);
579 }
580 }
581 // Same as above, but the use and the move are in different CFG blocks.
582 {
583 A a;
584 for (int i = 0; i < 10; ++i) {
585 if (i < 10)
586 a.foo();
587 // CHECK-NOTES: [[@LINE-1]]:9: warning: 'a' used after it was moved
588 // CHECK-NOTES: [[@LINE+3]]:9: note: move occurred here
589 // CHECK-NOTES: [[@LINE-3]]:9: note: the use happens in a later loop
590 if (i < 10)
591 std::move(a);
592 }
593 }
594 // However, this case shouldn't be flagged -- the scope of the declaration of
595 // 'a' is important.
596 {
597 for (int i = 0; i < 10; ++i) {
598 A a;
599 a.foo();
600 std::move(a);
601 }
602 }
603 // Same as above, except that we have an unrelated variable being declared in
604 // the same declaration as 'a'. This case is interesting because it tests that
605 // the synthetic DeclStmts generated by the CFG are sequenced correctly
606 // relative to the other statements.
607 {
608 for (int i = 0; i < 10; ++i) {
609 A a, other;
610 a.foo();
611 std::move(a);
612 }
613 }
614 // Don't warn if we return after the move.
615 {
616 A a;
617 for (int i = 0; i < 10; ++i) {
618 a.foo();
619 if (a.getInt() > 0) {
620 std::move(a);
621 return;
622 }
623 }
624 }
625 }
626
627 void differentBranches(int i) {
628 // Don't warn if the use is in a different branch from the move.
629 {
630 A a;
631 if (i > 0) {
632 std::move(a);
633 } else {
634 a.foo();
635 }
636 }
637 // Same thing, but with a ternary operator.
638 {
639 A a;
640 i > 0 ? (void)std::move(a) : a.foo();
641 }
642 // A variation on the theme above.
643 {
644 A a;
645 a.getInt() > 0 ? a.getInt() : A(std::move(a)).getInt();
646 }
647 // Same thing, but with a switch statement.
648 {
649 A a;
650 switch (i) {
651 case 1:
652 std::move(a);
653 break;
654 case 2:
655 a.foo();
656 break;
657 }
658 }
659 // However, if there's a fallthrough, we do warn.
660 {
661 A a;
662 switch (i) {
663 case 1:
664 std::move(a);
665 case 2:
666 a.foo();
667 // CHECK-NOTES: [[@LINE-1]]:7: warning: 'a' used after it was moved
668 // CHECK-NOTES: [[@LINE-4]]:7: note: move occurred here
669 break;
670 }
671 }
672 }
673
674 // False positive: A use-after-move is flagged even though the "if (b)" and
675 // "if (!b)" are mutually exclusive.
676 void mutuallyExclusiveBranchesFalsePositive(bool b) {
677 A a;
678 if (b) {
679 std::move(a);
680 }
681 if (!b) {
682 a.foo();
683 // CHECK-NOTES: [[@LINE-1]]:5: warning: 'a' used after it was moved
684 // CHECK-NOTES: [[@LINE-5]]:5: note: move occurred here
685 }
686 }
687
688 // Destructors marked [[noreturn]] are handled correctly in the control flow
689 // analysis. (These are used in some styles of assertion macros.)
690 class FailureLogger {
691 public:
692 FailureLogger();
693 [[noreturn]] ~FailureLogger();
694 void log(const char *);
695 };
696 #define ASSERT(x) \
697 while (x) \
698 FailureLogger().log(#x)
699 bool operationOnA(A);
700 void noreturnDestructor() {
701 A a;
702 // The while loop in the ASSERT() would ordinarily have the potential to cause
703 // a use-after-move because the second iteration of the loop would be using a
704 // variable that had been moved from in the first iteration. Check that the
705 // CFG knows that the second iteration of the loop is never reached because
706 // the FailureLogger destructor is marked [[noreturn]].
707 ASSERT(operationOnA(std::move(a)));
708 }
709 #undef ASSERT
710
711 ////////////////////////////////////////////////////////////////////////////////
712 // Tests for reinitializations
713
714 template <class T>
715 void swap(T &a, T &b) {
716 T tmp = std::move(a);
717 a = std::move(b);
718 b = std::move(tmp);
719 }
720 void assignments(int i) {
721 // Don't report a use-after-move if the variable was assigned to in the
722 // meantime.
723 {
724 A a;
725 std::move(a);
726 a = A();
727 a.foo();
728 }
729 // The assignment should also be recognized if move, assignment and use don't
730 // all happen in the same block (but the assignment is still guaranteed to
731 // prevent a use-after-move).
732 {
733 A a;
734 if (i == 1) {
735 std::move(a);
736 a = A();
737 }
738 if (i == 2) {
739 a.foo();
740 }
741 }
742 {
743 A a;
744 if (i == 1) {
745 std::move(a);
746 }
747 if (i == 2) {
748 a = A();
749 a.foo();
750 }
751 }
752 // The built-in assignment operator should also be recognized as a
753 // reinitialization. (std::move() may be called on built-in types in template
754 // code.)
755 {
756 int a1 = 1, a2 = 2;
757 swap(a1, a2);
758 }
759 // A std::move() after the assignment makes the variable invalid again.
760 {
761 A a;
762 std::move(a);
763 a = A();
764 std::move(a);
765 a.foo();
766 // CHECK-NOTES: [[@LINE-1]]:5: warning: 'a' used after it was moved
767 // CHECK-NOTES: [[@LINE-3]]:5: note: move occurred here
768 }
769 // Report a use-after-move if we can't be sure that the variable was assigned
770 // to.
771 {
772 A a;
773 std::move(a);
774 if (i < 10) {
775 a = A();
776 }
777 if (i > 5) {
778 a.foo();
779 // CHECK-NOTES: [[@LINE-1]]:7: warning: 'a' used after it was moved
780 // CHECK-NOTES: [[@LINE-7]]:5: note: move occurred here
781 }
782 }
783 }
784
785 // Passing the object to a function through a non-const pointer or reference
786 // counts as a re-initialization.
787 void passByNonConstPointer(A *);
788 void passByNonConstReference(A &);
789 void passByNonConstPointerIsReinit() {
790 {
791 A a;
792 std::move(a);
793 passByNonConstPointer(&a);
794 a.foo();
795 }
796 {
797 A a;
798 std::move(a);
799 passByNonConstReference(a);
800 a.foo();
801 }
802 }
803
804 // Passing the object through a const pointer or reference counts as a use --
805 // since the called function cannot reinitialize the object.
806 void passByConstPointer(const A *);
807 void passByConstReference(const A &);
808 void passByConstPointerIsUse() {
809 {
810 // Declaring 'a' as const so that no ImplicitCastExpr is inserted into the
811 // AST -- we wouldn't want the check to rely solely on that to detect a
812 // const pointer argument.
813 const A a;
814 std::move(a);
815 passByConstPointer(&a);
816 // CHECK-NOTES: [[@LINE-1]]:25: warning: 'a' used after it was moved
817 // CHECK-NOTES: [[@LINE-3]]:5: note: move occurred here
818 }
819 const A a;
820 std::move(a);
821 passByConstReference(a);
822 // CHECK-NOTES: [[@LINE-1]]:24: warning: 'a' used after it was moved
823 // CHECK-NOTES: [[@LINE-3]]:3: note: move occurred here
824 }
825
826 // Clearing a standard container using clear() is treated as a
827 // re-initialization.
828 void standardContainerClearIsReinit() {
829 {
830 std::string container;
831 std::move(container);
832 container.clear();
833 container.empty();
834 }
835 {
836 std::vector<int> container;
837 std::move(container);
838 container.clear();
839 container.empty();
840
841 auto container2 = container;
842 std::move(container2);
843 container2.clear();
844 container2.empty();
845 }
846 {
847 std::deque<int> container;
848 std::move(container);
849 container.clear();
850 container.empty();
851 }
852 {
853 std::forward_list<int> container;
854 std::move(container);
855 container.clear();
856 container.empty();
857 }
858 {
859 std::list<int> container;
860 std::move(container);
861 container.clear();
862 container.empty();
863 }
864 {
865 std::set<int> container;
866 std::move(container);
867 container.clear();
868 container.empty();
869 }
870 {
871 std::map<int, int> container;
872 std::move(container);
873 container.clear();
874 container.empty();
875 }
876 {
877 std::multiset<int> container;
878 std::move(container);
879 container.clear();
880 container.empty();
881 }
882 {
883 std::multimap<int> container;
884 std::move(container);
885 container.clear();
886 container.empty();
887 }
888 {
889 std::unordered_set<int> container;
890 std::move(container);
891 container.clear();
892 container.empty();
893 }
894 {
895 std::unordered_map<int, int> container;
896 std::move(container);
897 container.clear();
898 container.empty();
899 }
900 {
901 std::unordered_multiset<int> container;
902 std::move(container);
903 container.clear();
904 container.empty();
905 }
906 {
907 std::unordered_multimap<int> container;
908 std::move(container);
909 container.clear();
910 container.empty();
911 }
912 // This should also work for typedefs of standard containers.
913 {
914 typedef std::vector<int> IntVector;
915 IntVector container;
916 std::move(container);
917 container.clear();
918 container.empty();
919 }
920 // But it shouldn't work for non-standard containers.
921 {
922 // This might be called "vector", but it's not in namespace "std".
923 struct vector {
924 void clear() {}
925 } container;
926 std::move(container);
927 container.clear();
928 // CHECK-NOTES: [[@LINE-1]]:5: warning: 'container' used after it was
929 // CHECK-NOTES: [[@LINE-3]]:5: note: move occurred here
930 }
931 // An intervening clear() on a different container does not reinitialize.
932 {
933 std::vector<int> container1, container2;
934 std::move(container1);
935 container2.clear();
936 container1.empty();
937 // CHECK-NOTES: [[@LINE-1]]:5: warning: 'container1' used after it was
938 // CHECK-NOTES: [[@LINE-4]]:5: note: move occurred here
939 }
940 }
941
942 // Clearing a standard container using assign() is treated as a
943 // re-initialization.
944 void standardContainerAssignIsReinit() {
945 {
946 std::string container;
947 std::move(container);
948 container.assign(0, ' ');
949 container.empty();
950 }
951 {
952 std::vector<int> container;
953 std::move(container);
954 container.assign(0, 0);
955 container.empty();
956 }
957 {
958 std::deque<int> container;
959 std::move(container);
960 container.assign(0, 0);
961 container.empty();
962 }
963 {
964 std::forward_list<int> container;
965 std::move(container);
966 container.assign(0, 0);
967 container.empty();
968 }
969 {
970 std::list<int> container;
971 std::move(container);
972 container.clear();
973 container.empty();
974 }
975 // But it doesn't work for non-standard containers.
976 {
977 // This might be called "vector", but it's not in namespace "std".
978 struct vector {
979 void assign(std::size_t, int) {}
980 } container;
981 std::move(container);
982 container.assign(0, 0);
983 // CHECK-NOTES: [[@LINE-1]]:5: warning: 'container' used after it was
984 // CHECK-NOTES: [[@LINE-3]]:5: note: move occurred here
985 }
986 // An intervening assign() on a different container does not reinitialize.
987 {
988 std::vector<int> container1, container2;
989 std::move(container1);
990 container2.assign(0, 0);
991 container1.empty();
992 // CHECK-NOTES: [[@LINE-1]]:5: warning: 'container1' used after it was
993 // CHECK-NOTES: [[@LINE-4]]:5: note: move occurred here
994 }
995 }
996
997 // Resetting the standard smart pointer types using reset() is treated as a
998 // re-initialization. (We don't test std::weak_ptr<> because it can't be
999 // dereferenced directly.)
1000 void standardSmartPointerResetIsReinit() {
1001 {
1002 std::unique_ptr<A> ptr;
1003 std::move(ptr);
1004 ptr.reset(new A);
1005 *ptr;
1006 }
1007 {
1008 std::shared_ptr<A> ptr;
1009 std::move(ptr);
1010 ptr.reset(new A);
1011 *ptr;
1012 }
1013 }
1014
1015 void reinitAnnotation() {
1016 {
1017 AnnotatedContainer<int> obj;
1018 std::move(obj);
1019 obj.foo();
1020 // CHECK-NOTES: [[@LINE-1]]:5: warning: 'obj' used after it was
1021 // CHECK-NOTES: [[@LINE-3]]:5: note: move occurred here
1022 }
1023 {
1024 AnnotatedContainer<int> obj;
1025 std::move(obj);
1026 obj.clear();
1027 obj.foo();
1028 }
1029 {
1030 // Calling clear() on a different object to the one that was moved is not
1031 // considered a reinitialization.
1032 AnnotatedContainer<int> obj1, obj2;
1033 std::move(obj1);
1034 obj2.clear();
1035 obj1.foo();
1036 // CHECK-NOTES: [[@LINE-1]]:5: warning: 'obj1' used after it was
1037 // CHECK-NOTES: [[@LINE-4]]:5: note: move occurred here
1038 }
1039 }
1040
1041 ////////////////////////////////////////////////////////////////////////////////
1042 // Tests related to order of evaluation within expressions
1043
1044 // Relative sequencing of move and use.
1045 void passByRvalueReference(int i, A &&a);
1046 void passByValue(int i, A a);
1047 void passByValue(A a, int i);
1048 A g(A, A &&);
1049 int intFromA(A &&);
1050 int intFromInt(int);
1051 void sequencingOfMoveAndUse() {
1052 // This case is fine because the move only happens inside
1053 // passByRvalueReference(). At this point, a.getInt() is guaranteed to have
1054 // been evaluated.
1055 {
1056 A a;
1057 passByRvalueReference(a.getInt(), std::move(a));
1058 }
1059 // However, if we pass by value, the move happens when the move constructor is
1060 // called to create a temporary, and this happens before the call to
1061 // passByValue(). Because the order in which arguments are evaluated isn't
1062 // defined, the move may happen before the call to a.getInt().
1063 //
1064 // Check that we warn about a potential use-after move for both orderings of
1065 // a.getInt() and std::move(a), independent of the order in which the
1066 // arguments happen to get evaluated by the compiler.
1067 {
1068 A a;
1069 passByValue(a.getInt(), std::move(a));
1070 // CHECK-NOTES: [[@LINE-1]]:17: warning: 'a' used after it was moved
1071 // CHECK-NOTES: [[@LINE-2]]:29: note: move occurred here
1072 // CHECK-NOTES: [[@LINE-3]]:17: note: the use and move are unsequenced
1073 }
1074 {
1075 A a;
1076 passByValue(std::move(a), a.getInt());
1077 // CHECK-NOTES: [[@LINE-1]]:31: warning: 'a' used after it was moved
1078 // CHECK-NOTES: [[@LINE-2]]:17: note: move occurred here
1079 // CHECK-NOTES: [[@LINE-3]]:31: note: the use and move are unsequenced
1080 }
1081 // An even more convoluted example.
1082 {
1083 A a;
1084 g(g(a, std::move(a)), g(a, std::move(a)));
1085 // CHECK-NOTES: [[@LINE-1]]:9: warning: 'a' used after it was moved
1086 // CHECK-NOTES: [[@LINE-2]]:27: note: move occurred here
1087 // CHECK-NOTES: [[@LINE-3]]:9: note: the use and move are unsequenced
1088 // CHECK-NOTES: [[@LINE-4]]:29: warning: 'a' used after it was moved
1089 // CHECK-NOTES: [[@LINE-5]]:7: note: move occurred here
1090 // CHECK-NOTES: [[@LINE-6]]:29: note: the use and move are unsequenced
1091 }
1092 // This case is fine because the actual move only happens inside the call to
1093 // operator=(). a.getInt(), by necessity, is evaluated before that call.
1094 {
1095 A a;
1096 A vec[1];
1097 vec[a.getInt()] = std::move(a);
1098 }
1099 // However, in the following case, the move happens before the assignment, and
1100 // so the order of evaluation is not guaranteed.
1101 {
1102 A a;
1103 int v[3];
1104 v[a.getInt()] = intFromA(std::move(a));
1105 // CHECK-NOTES: [[@LINE-1]]:7: warning: 'a' used after it was moved
1106 // CHECK-NOTES: [[@LINE-2]]:21: note: move occurred here
1107 // CHECK-NOTES: [[@LINE-3]]:7: note: the use and move are unsequenced
1108 }
1109 {
1110 A a;
1111 int v[3];
1112 v[intFromA(std::move(a))] = intFromInt(a.i);
1113 // CHECK-NOTES: [[@LINE-1]]:44: warning: 'a' used after it was moved
1114 // CHECK-NOTES: [[@LINE-2]]:7: note: move occurred here
1115 // CHECK-NOTES: [[@LINE-3]]:44: note: the use and move are unsequenced
1116 }
1117 }
1118
1119 // Relative sequencing of move and reinitialization. If the two are unsequenced,
1120 // we conservatively assume that the move happens after the reinitialization,
1121 // i.e. the that object does not get reinitialized after the move.
1122 A MutateA(A a);
1123 void passByValue(A a1, A a2);
1124 void sequencingOfMoveAndReinit() {
1125 // Move and reinitialization as function arguments (which are indeterminately
1126 // sequenced). Again, check that we warn for both orderings.
1127 {
1128 A a;
1129 passByValue(std::move(a), (a = A()));
1130 a.foo();
1131 // CHECK-NOTES: [[@LINE-1]]:5: warning: 'a' used after it was moved
1132 // CHECK-NOTES: [[@LINE-3]]:17: note: move occurred here
1133 }
1134 {
1135 A a;
1136 passByValue((a = A()), std::move(a));
1137 a.foo();
1138 // CHECK-NOTES: [[@LINE-1]]:5: warning: 'a' used after it was moved
1139 // CHECK-NOTES: [[@LINE-3]]:28: note: move occurred here
1140 }
1141 // Common usage pattern: Move the object to a function that mutates it in some
1142 // way, then reassign the result to the object. This pattern is fine.
1143 {
1144 A a;
1145 a = MutateA(std::move(a));
1146 a.foo();
1147 }
1148 }
1149
1150 // Relative sequencing of reinitialization and use. If the two are unsequenced,
1151 // we conservatively assume that the reinitialization happens after the use,
1152 // i.e. that the object is not reinitialized at the point in time when it is
1153 // used.
1154 void sequencingOfReinitAndUse() {
1155 // Reinitialization and use in function arguments. Again, check both possible
1156 // orderings.
1157 {
1158 A a;
1159 std::move(a);
1160 passByValue(a.getInt(), (a = A()));
1161 // CHECK-NOTES: [[@LINE-1]]:17: warning: 'a' used after it was moved
1162 // CHECK-NOTES: [[@LINE-3]]:5: note: move occurred here
1163 }
1164 {
1165 A a;
1166 std::move(a);
1167 passByValue((a = A()), a.getInt());
1168 // CHECK-NOTES: [[@LINE-1]]:28: warning: 'a' used after it was moved
1169 // CHECK-NOTES: [[@LINE-3]]:5: note: move occurred here
1170 }
1171 }
1172
1173 // The comma operator sequences its operands.
1174 void commaOperatorSequences() {
1175 {
1176 A a;
1177 A(std::move(a))
1178 , (a = A());
1179 a.foo();
1180 }
1181 {
1182 A a;
1183 (a = A()), A(std::move(a));
1184 a.foo();
1185 // CHECK-NOTES: [[@LINE-1]]:5: warning: 'a' used after it was moved
1186 // CHECK-NOTES: [[@LINE-3]]:16: note: move occurred here
1187 }
1188 }
1189
1190 namespace InitializerListSequences {
1191
1192 struct S1 {
1193 int i;
1194 A a;
1195 };
1196
1197 struct S2 {
1198 A a;
1199 int i;
1200 };
1201
1202 struct S3 {
1203 S3() {}
1204 S3(int, A) {}
1205 S3(A, int) {}
1206 };
1207
1208 // An initializer list sequences its initialization clauses.
1209 void initializerListSequences() {
1210 {
1211 A a;
1212 S1 s1{a.getInt(), std::move(a)};
1213 }
1214 {
1215 A a;
1216 S1 s1{.i = a.getInt(), .a = std::move(a)};
1217 }
1218 {
1219 A a;
1220 S2 s2{std::move(a), a.getInt()};
1221 // CHECK-NOTES: [[@LINE-1]]:25: warning: 'a' used after it was moved
1222 // CHECK-NOTES: [[@LINE-2]]:11: note: move occurred here
1223 }
1224 {
1225 A a;
1226 S2 s2{.a = std::move(a), .i = a.getInt()};
1227 // CHECK-NOTES: [[@LINE-1]]:35: warning: 'a' used after it was moved
1228 // CHECK-NOTES: [[@LINE-2]]:11: note: move occurred here
1229 }
1230 {
1231 // Check the case where the constructed type has a constructor and the
1232 // initializer list therefore manifests as a `CXXConstructExpr` instead of
1233 // an `InitListExpr`.
1234 A a;
1235 S3 s3{a.getInt(), std::move(a)};
1236 }
1237 {
1238 A a;
1239 S3 s3{std::move(a), a.getInt()};
1240 // CHECK-NOTES: [[@LINE-1]]:25: warning: 'a' used after it was moved
1241 // CHECK-NOTES: [[@LINE-2]]:11: note: move occurred here
1242 }
1243 }
1244
1245 } // namespace InitializerListSequences
1246
1247 // A declaration statement containing multiple declarations sequences the
1248 // initializer expressions.
1249 void declarationSequences() {
1250 {
1251 A a;
1252 A a1 = a, a2 = std::move(a);
1253 }
1254 {
1255 A a;
1256 A a1 = std::move(a), a2 = a;
1257 // CHECK-NOTES: [[@LINE-1]]:31: warning: 'a' used after it was moved
1258 // CHECK-NOTES: [[@LINE-2]]:12: note: move occurred here
1259 }
1260 }
1261
1262 // The logical operators && and || sequence their operands.
1263 void logicalOperatorsSequence() {
1264 {
1265 A a;
1266 if (a.getInt() > 0 && A(std::move(a)).getInt() > 0) {
1267 A().foo();
1268 }
1269 }
1270 // A variation: Negate the result of the && (which pushes the && further down
1271 // into the AST).
1272 {
1273 A a;
1274 if (!(a.getInt() > 0 && A(std::move(a)).getInt() > 0)) {
1275 A().foo();
1276 }
1277 }
1278 {
1279 A a;
1280 if (A(std::move(a)).getInt() > 0 && a.getInt() > 0) {
1281 // CHECK-NOTES: [[@LINE-1]]:41: warning: 'a' used after it was moved
1282 // CHECK-NOTES: [[@LINE-2]]:9: note: move occurred here
1283 A().foo();
1284 }
1285 }
1286 {
1287 A a;
1288 if (a.getInt() > 0 || A(std::move(a)).getInt() > 0) {
1289 A().foo();
1290 }
1291 }
1292 {
1293 A a;
1294 if (A(std::move(a)).getInt() > 0 || a.getInt() > 0) {
1295 // CHECK-NOTES: [[@LINE-1]]:41: warning: 'a' used after it was moved
1296 // CHECK-NOTES: [[@LINE-2]]:9: note: move occurred here
1297 A().foo();
1298 }
1299 }
1300 }
1301
1302 // A range-based for sequences the loop variable declaration before the body.
1303 void forRangeSequences() {
1304 A v[2] = {A(), A()};
1305 for (A &a : v) {
1306 std::move(a);
1307 }
1308 }
1309
1310 // If a variable is declared in an if, while or switch statement, the init
1311 // statement (for if and switch) is sequenced before the variable declaration,
1312 // which in turn is sequenced before the evaluation of the condition. We place
1313 // all tests inside a for loop to ensure that the checker understands the
1314 // sequencing. If it didn't, then the loop would trigger the "moved twice"
1315 // logic.
1316 void ifWhileAndSwitchSequenceInitDeclAndCondition() {
1317 for (int i = 0; i < 10; ++i) {
1318 A a1;
1319 if (A a2 = std::move(a1)) {
1320 std::move(a2);
1321 }
1322 }
1323 for (int i = 0; i < 10; ++i) {
1324 A a1;
1325 if (A a2 = std::move(a1); a2) {
1326 std::move(a2);
1327 }
1328 }
1329 for (int i = 0; i < 10; ++i) {
1330 A a1;
1331 if (A a2 = std::move(a1); A a3 = std::move(a2)) {
1332 std::move(a3);
1333 }
1334 }
1335 for (int i = 0; i < 10; ++i) {
1336 // init followed by condition with move, but without variable declaration.
1337 if (A a1; A(std::move(a1)).getInt() > 0) {}
1338 }
1339 for (int i = 0; i < 10; ++i) {
1340 if (A a1; A(std::move(a1)).getInt() > a1.getInt()) {}
1341 // CHECK-NOTES: [[@LINE-1]]:43: warning: 'a1' used after it was moved
1342 // CHECK-NOTES: [[@LINE-2]]:15: note: move occurred here
1343 // CHECK-NOTES: [[@LINE-3]]:43: note: the use and move are unsequenced
1344 }
1345 for (int i = 0; i < 10; ++i) {
1346 A a1;
1347 if (A a2 = std::move(a1); A(a1) > 0) {}
1348 // CHECK-NOTES: [[@LINE-1]]:33: warning: 'a1' used after it was moved
1349 // CHECK-NOTES: [[@LINE-2]]:16: note: move occurred here
1350 }
1351 while (A a = A()) {
1352 std::move(a);
1353 }
1354 for (int i = 0; i < 10; ++i) {
1355 A a1;
1356 switch (A a2 = std::move(a1); a2) {
1357 case true:
1358 std::move(a2);
1359 }
1360 }
1361 for (int i = 0; i < 10; ++i) {
1362 A a1;
1363 switch (A a2 = a1; A a3 = std::move(a2)) {
1364 case true:
1365 std::move(a3);
1366 }
1367 }
1368 }
1369
1370 // In a function call, the expression that determines the callee is sequenced
1371 // before the arguments -- but only in C++17 and later.
1372 namespace CalleeSequencedBeforeArguments {
1373 int consumeA(std::unique_ptr<A> a);
1374 int consumeA(A &&a);
1375
1376 void calleeSequencedBeforeArguments() {
1377 {
1378 std::unique_ptr<A> a;
1379 a->bar(consumeA(std::move(a)));
1380 // CHECK-NOTES-CXX11: [[@LINE-1]]:5: warning: 'a' used after it was moved
1381 // CHECK-NOTES-CXX11: [[@LINE-2]]:21: note: move occurred here
1382 // CHECK-NOTES-CXX11: [[@LINE-3]]:5: note: the use and move are unsequenced
1383 }
1384 {
1385 std::unique_ptr<A> a;
1386 std::unique_ptr<A> getArg(std::unique_ptr<A> a);
1387 getArg(std::move(a))->bar(a->getInt());
1388 // CHECK-NOTES: [[@LINE-1]]:31: warning: 'a' used after it was moved
1389 // CHECK-NOTES: [[@LINE-2]]:12: note: move occurred here
1390 // CHECK-NOTES-CXX11: [[@LINE-3]]:31: note: the use and move are unsequenced
1391 }
1392 {
1393 A a;
1394 // Nominally, the callee `a.bar` is evaluated before the argument
1395 // `consumeA(std::move(a))`, but in effect `a` is only accessed after the
1396 // call to `A::bar()` happens, i.e. after the argument has been evaluted.
1397 a.bar(consumeA(std::move(a)));
1398 // CHECK-NOTES: [[@LINE-1]]:5: warning: 'a' used after it was moved
1399 // CHECK-NOTES: [[@LINE-2]]:11: note: move occurred here
1400 }
1401 }
1402 } // namespace CalleeSequencedBeforeArguments
1403
1404 // Some statements in templates (e.g. null, break and continue statements) may
1405 // be shared between the uninstantiated and instantiated versions of the
1406 // template and therefore have multiple parents. Make sure the sequencing code
1407 // handles this correctly.
1408 template <class> void nullStatementSequencesInTemplate() {
1409 int c = 0;
1410 (void)c;
1411 ;
1412 std::move(c);
1413 }
1414 template void nullStatementSequencesInTemplate<int>();
1415
1416 namespace PR33020 {
1417 class D {
1418 ~D();
1419 };
1420 struct A {
1421 D d;
1422 };
1423 class B {
1424 A a;
1425 };
1426 template <typename T>
1427 class C : T, B {
1428 void m_fn1() {
1429 int a;
1430 std::move(a);
1431 C c;
1432 }
1433 };
1434 } // namespace PR33020
1435
1436 namespace UnevalContext {
1437 struct Foo {};
1438 void noExcept() {
1439 Foo Bar;
1440 (void) noexcept(Foo{std::move(Bar)});
1441 Foo Other{std::move(Bar)};
1442 }
1443 void sizeOf() {
1444 Foo Bar;
1445 (void)sizeof(Foo{std::move(Bar)});
1446 Foo Other{std::move(Bar)};
1447 }
1448 void alignOf() {
1449 Foo Bar;
1450 #pragma clang diagnostic push
1451 #pragma clang diagnostic ignored "-Wgnu-alignof-expression"
1452 (void)alignof(Foo{std::move(Bar)});
1453 #pragma clang diagnostic pop
1454 Foo Other{std::move(Bar)};
1455 }
1456 void typeId() {
1457 Foo Bar;
1458 // error: you need to include <typeinfo> before using the 'typeid' operator
1459 // (void) typeid(Foo{std::move(Bar)}).name();
1460 Foo Other{std::move(Bar)};
1461 }
1462 } // namespace UnevalContext
1463
1464 class CtorInit {
1465 public:
1466 CtorInit(std::string val)
1467 : a{val.empty()}, // fine
1468 s{std::move(val)},
1469 b{val.empty()}
1470 // CHECK-NOTES: [[@LINE-1]]:11: warning: 'val' used after it was moved
1471 // CHECK-NOTES: [[@LINE-3]]:9: note: move occurred here
1472 {}
1473
1474 private:
1475 bool a;
1476 std::string s;
1477 bool b;
1478 };
1479
1480 class CtorInitLambda {
1481 public:
1482 CtorInitLambda(std::string val)
1483 : a{val.empty()}, // fine
1484 s{std::move(val)},
1485 b{[&] { return val.empty(); }()},
1486 // CHECK-NOTES: [[@LINE-1]]:12: warning: 'val' used after it was moved
1487 // CHECK-NOTES: [[@LINE-3]]:9: note: move occurred here
1488 c{[] {
1489 std::string str{};
1490 std::move(str);
1491 return str.empty();
1492 // CHECK-NOTES: [[@LINE-1]]:18: warning: 'str' used after it was moved
1493 // CHECK-NOTES: [[@LINE-3]]:11: note: move occurred here
1494 }()} {
1495 std::move(val);
1496 // CHECK-NOTES: [[@LINE-1]]:15: warning: 'val' used after it was moved
1497 // CHECK-NOTES: [[@LINE-13]]:9: note: move occurred here
1498 std::string val2{};
1499 std::move(val2);
1500 val2.empty();
1501 // CHECK-NOTES: [[@LINE-1]]:5: warning: 'val2' used after it was moved
1502 // CHECK-NOTES: [[@LINE-3]]:5: note: move occurred here
1503 }
1504
1505 private:
1506 bool a;
1507 std::string s;
1508 bool b;
1509 bool c;
1510 bool d{};
1511 };
1512
1513 class CtorInitOrder {
1514 public:
1515 CtorInitOrder(std::string val)
1516 : a{val.empty()}, // fine
1517 b{val.empty()},
1518 // CHECK-NOTES: [[@LINE-1]]:11: warning: 'val' used after it was moved
1519 s{std::move(val)} {} // wrong order
1520 // CHECK-NOTES: [[@LINE-1]]:9: note: move occurred here
1521
1522 private:
1523 bool a;
1524 std::string s;
1525 bool b;
1526 };
1527
1528 struct Obj {};
1529 struct CtorD {
1530 CtorD(Obj b);
1531 };
1532
1533 struct CtorC {
1534 CtorC(Obj b);
1535 };
1536
1537 struct CtorB {
1538 CtorB(Obj &b);
1539 };
1540
1541 struct CtorA : CtorB, CtorC, CtorD {
1542 CtorA(Obj b) : CtorB{b}, CtorC{std::move(b)}, CtorD{b} {}
1543 // CHECK-NOTES: [[@LINE-1]]:55: warning: 'b' used after it was moved
1544 // CHECK-NOTES: [[@LINE-2]]:34: note: move occurred here
1545 };
1546
1547 struct Base {
1548 Base(Obj b) : bb{std::move(b)} {}
1549 template <typename Call> Base(Call &&c) : bb{c()} {};
1550
1551 Obj bb;
1552 };
1553
1554 struct Derived : Base, CtorC {
1555 Derived(Obj b)
1556 : Base{[&] mutable { return std::move(b); }()},
1557 // False negative: The lambda/std::move was executed, so it should warn
1558 // below
1559 CtorC{b} {}
1560 };
1561
1562 struct Derived2 : Base, CtorC {
1563 Derived2(Obj b)
1564 : Base{[&] mutable { return std::move(b); }},
1565 // This was a move, but it doesn't warn below, because it can't know if
1566 // the lambda/std::move was actually called
1567 CtorC{b} {}
1568 };
1569
1570 struct Derived3 : Base, CtorC {
1571 Derived3(Obj b)
1572 : Base{[c = std::move(b)] mutable { return std::move(c); }}, CtorC{b} {}
1573 // CHECK-NOTES: [[@LINE-1]]:74: warning: 'b' used after it was moved
1574 // CHECK-NOTES: [[@LINE-2]]:19: note: move occurred here
1575 };
1576
1577 class PR38187 {
1578 public:
1579 PR38187(std::string val) : val_(std::move(val)) {
1580 val.empty();
1581 // CHECK-NOTES: [[@LINE-1]]:5: warning: 'val' used after it was moved
1582 // CHECK-NOTES: [[@LINE-3]]:30: note: move occurred here
1583 }
1584
1585 private:
1586 std::string val_;
1587 };
1588
1589 namespace issue82023
1590 {
1591
1592 struct S {
1593 S();
1594 S(S&&);
1595 };
1596
1597 void consume(S s);
1598
1599 template <typename T>
1600 void forward(T&& t) {
1601 consume(std::forward<T>(t));
1602 consume(std::forward<T>(t));
1603 // CHECK-NOTES: [[@LINE-1]]:27: warning: 't' used after it was forwarded
1604 // CHECK-NOTES: [[@LINE-3]]:11: note: forward occurred here
1605 }
1606
1607 void create() {
1608 S s;
1609 forward(std::move(s));
1610 }
1611
1612 } // namespace issue82023