blob: abfb001fa8e6b5320710cfe9e5cdea73e5676f5a [file] [log] [blame]
Richard Trieuac3eca52015-04-29 01:52:17 +00001// RUN: %clang_cc1 -fsyntax-only -Wredundant-move -std=c++11 -verify %s
Richard Trieu1993dc82015-07-29 23:47:19 +00002// RUN: %clang_cc1 -fsyntax-only -Wredundant-move -std=c++11 -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
3// RUN: %clang_cc1 -fsyntax-only -std=c++11 %s -ast-dump | FileCheck %s --check-prefix=CHECK-AST
Richard Trieuac3eca52015-04-29 01:52:17 +00004
5// definitions for std::move
6namespace std {
7inline namespace foo {
8template <class T> struct remove_reference { typedef T type; };
9template <class T> struct remove_reference<T&> { typedef T type; };
10template <class T> struct remove_reference<T&&> { typedef T type; };
11
12template <class T> typename remove_reference<T>::type &&move(T &&t);
13}
14}
15
Richard Trieu1993dc82015-07-29 23:47:19 +000016// test1 and test2 should not warn until after implementation of DR1579.
Richard Trieuac3eca52015-04-29 01:52:17 +000017struct A {};
18struct B : public A {};
19
20A test1(B b1) {
21 B b2;
Richard Trieu1d4911bc2015-05-18 19:54:08 +000022 return b1;
23 return b2;
Richard Trieuac3eca52015-04-29 01:52:17 +000024 return std::move(b1);
Richard Trieuac3eca52015-04-29 01:52:17 +000025 return std::move(b2);
Richard Trieuac3eca52015-04-29 01:52:17 +000026}
27
28struct C {
29 C() {}
30 C(A) {}
31};
32
33C test2(A a1, B b1) {
34 A a2;
35 B b2;
36
37 return a1;
38 return a2;
39 return b1;
40 return b2;
41
42 return std::move(a1);
Richard Trieuac3eca52015-04-29 01:52:17 +000043 return std::move(a2);
Richard Trieuac3eca52015-04-29 01:52:17 +000044 return std::move(b1);
Richard Trieuac3eca52015-04-29 01:52:17 +000045 return std::move(b2);
Richard Trieuac3eca52015-04-29 01:52:17 +000046}
Richard Trieu1d4911bc2015-05-18 19:54:08 +000047
48// Copy of tests above with types changed to reference types.
49A test3(B& b1) {
50 B& b2 = b1;
51 return b1;
52 return b2;
53 return std::move(b1);
54 return std::move(b2);
55}
56
57C test4(A& a1, B& b1) {
58 A& a2 = a1;
59 B& b2 = b1;
60
61 return a1;
62 return a2;
63 return b1;
64 return b2;
65
66 return std::move(a1);
67 return std::move(a2);
68 return std::move(b1);
69 return std::move(b2);
70}
Davide Italiano7842c3f2015-07-18 01:15:19 +000071
Richard Trieu159e5ed2015-07-21 23:38:30 +000072// PR23819, case 2
73struct D {};
74D test5(D d) {
75 return d;
Richard Trieu1993dc82015-07-29 23:47:19 +000076 // Verify the implicit move from the AST dump
77 // CHECK-AST: ReturnStmt{{.*}}line:[[@LINE-2]]
78 // CHECK-AST-NEXT: CXXConstructExpr{{.*}}struct D{{.*}}void (struct D &&)
79 // CHECK-AST-NEXT: ImplicitCastExpr
80 // CHECK-AST-NEXT: DeclRefExpr{{.*}}ParmVar{{.*}}'d'
Richard Trieu159e5ed2015-07-21 23:38:30 +000081
82 return std::move(d);
83 // expected-warning@-1{{redundant move in return statement}}
84 // expected-note@-2{{remove std::move call here}}
85 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:20}:""
86 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:21-[[@LINE-4]]:22}:""
87}
Richard Trieu8d4006a2015-07-28 19:06:16 +000088
Richard Trieu6093d142015-07-29 17:03:34 +000089namespace templates {
90 struct A {};
91 struct B { B(A); };
Richard Trieu6093d142015-07-29 17:03:34 +000092
93 // Warn once here since the type is not dependent.
94 template <typename T>
Richard Trieu1993dc82015-07-29 23:47:19 +000095 A test1(A a) {
Richard Trieu6093d142015-07-29 17:03:34 +000096 return std::move(a);
97 // expected-warning@-1{{redundant move in return statement}}
98 // expected-note@-2{{remove std::move call here}}
99 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:12-[[@LINE-3]]:22}:""
100 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:23-[[@LINE-4]]:24}:""
101 }
102 void run_test1() {
Richard Trieu1993dc82015-07-29 23:47:19 +0000103 test1<A>(A());
104 test1<B>(A());
Richard Trieu6093d142015-07-29 17:03:34 +0000105 }
106
Richard Trieu1993dc82015-07-29 23:47:19 +0000107 // T1 and T2 may not be the same, the warning may not always apply.
Richard Trieu6093d142015-07-29 17:03:34 +0000108 template <typename T1, typename T2>
Richard Trieu1993dc82015-07-29 23:47:19 +0000109 T1 test2(T2 t) {
Richard Trieu6093d142015-07-29 17:03:34 +0000110 return std::move(t);
111 }
112 void run_test2() {
Richard Trieu1993dc82015-07-29 23:47:19 +0000113 test2<A, A>(A());
114 test2<B, A>(A());
Richard Trieu6093d142015-07-29 17:03:34 +0000115 }
116}