Debian Codegraph @

gcc/testsuite/g++.dg/warn/Wself-move1.C

raw
1 // PR c++/81159
2 // { dg-do compile { target c++11 } }
3 // { dg-options "-Wself-move" }
4
5 // Define std::move.
6 namespace std {
7 template<typename _Tp>
8 struct remove_reference
9 { typedef _Tp type; };
10
11 template<typename _Tp>
12 struct remove_reference<_Tp&>
13 { typedef _Tp type; };
14
15 template<typename _Tp>
16 struct remove_reference<_Tp&&>
17 { typedef _Tp type; };
18
19 template<typename _Tp>
20 constexpr typename std::remove_reference<_Tp>::type&&
21 move(_Tp&& __t) noexcept
22 { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); }
23 }
24
25 int g;
26
27 struct S {
28 int x;
29 S(S&& o) {
30 x = std::move (x); // { dg-warning "moving '\[^\n\r]*S::x' of type .int. to itself" }
31 x = std::move (o.x);
32 o.x = std::move (x);
33 o.x = std::move (o.x); // { dg-warning "moving 'o.S::x' of type .int. to itself" }
34 }
35 void foo (int x) {
36 x = std::move (x); // { dg-warning "moving 'x' of type .int. to itself" }
37 }
38 };
39
40 struct X {
41 int x;
42 X(int x) : x(std::move (x)) { }
43 };
44
45 struct A {};
46 struct B { A a; };
47 struct C { C(); ~C(); };
48 struct D { D(); D(const D&); D(D&&); D& operator=(const D&); };
49
50 void
51 test ()
52 {
53 int i = 42;
54 i = std::move (i); // { dg-warning "moving 'i' of type .int. to itself" }
55 (i) = std::move (i); // { dg-warning "moving 'i' of type .int. to itself" }
56
57 g = std::move (g); // { dg-warning "moving 'g' of type .int. to itself" }
58 (g) = std::move (g); // { dg-warning "moving 'g' of type .int. to itself" }
59
60 A a;
61 a = std::move (a); // { dg-warning "moving 'a' of type .A. to itself" }
62 (a) = std::move (a); // { dg-warning "moving 'a' of type .A. to itself" }
63
64 B b;
65 b = std::move (b); // { dg-warning "moving 'b' of type .B. to itself" }
66 (b) = std::move (b); // { dg-warning "moving 'b' of type .B. to itself" }
67 b.a = std::move (b.a); // { dg-warning "moving 'b.B::a' of type .A. to itself" }
68 (b.a) = std::move (b.a); // { dg-warning "moving 'b.B::a' of type .A. to itself" }
69
70 C c;
71 c = std::move (c); // { dg-warning "moving 'c' of type .C. to itself" }
72 D d;
73 d = std::move (d); // { dg-warning "moving 'd' of type .D. to itself" }
74 }
75
76 template<typename T>
77 void ttest ()
78 {
79 T t;
80 t = std::move (t); // { dg-warning "moving 't' of type .A. to itself" }
81 }
82
83 template void ttest<A>();
84
85 void
86 testref (int &r, int &&rr)
87 {
88 r = std::move (r); // { dg-warning "moving 'r' of type .int. to itself" }
89 rr = std::move (rr); // { dg-warning "moving 'rr' of type .int. to itself" }
90 }
91
92 // Test various other arguments to std::move.
93 template<typename T>
94 void
95 testargs (T *Tptr, T **Tpptr, T& Tref, T&& Trref, const T *Tcptr)
96 {
97 Tptr = std::move (Tptr); // { dg-warning "moving 'Tptr' of type 'int\\*' to itself" }
98 *Tptr = std::move (*Tptr); // { dg-warning "moving '\\* Tptr' of type 'int' to itself" }
99 *Tptr = std::move (*(Tptr)); // { dg-warning "moving '\\* Tptr' of type 'int' to itself" }
100 *(Tptr) = std::move (*Tptr); // { dg-warning "moving '\\* Tptr' of type 'int' to itself" }
101 *(Tptr + 1) = std::move (*(Tptr + 1)); // { dg-warning "moving '\[^\n\r]*Tptr\[^\n\r]*' of type 'int' to itself" }
102 *(Tptr + 1) = std::move (*(Tptr + 2));
103 (*(Tptr)) = std::move (*Tptr); // { dg-warning "moving '\\* Tptr' of type 'int' to itself" }
104 *Tpptr = std::move (*Tpptr); // { dg-warning "moving '\\* Tpptr' of type 'int\\*' to itself" }
105 **Tpptr = std::move (**Tpptr); // { dg-warning "moving '\\* \\* Tpptr' of type 'int' to itself" }
106 Tref = std::move (Tref); // { dg-warning "moving 'Tref' of type 'int' to itself" }
107 Trref = std::move (Trref); // { dg-warning "moving 'Trref' of type 'int' to itself" }
108 Tcptr = std::move (Tcptr); // { dg-warning "moving 'Tcptr' of type 'const int\\*' to itself" }
109 (Tptr) = std::move (Tptr); // { dg-warning "moving 'Tptr' of type 'int\\*' to itself" }
110 (*Tptr) = std::move (*Tptr); // { dg-warning "moving '\\* Tptr' of type 'int' to itself" }
111 (*Tpptr) = std::move (*Tpptr); // { dg-warning "moving '\\* Tpptr' of type 'int\\*' to itself" }
112 (**Tpptr) = std::move (**Tpptr); // { dg-warning "moving '\\* \\* Tpptr' of type 'int' to itself" }
113 (*(*(Tpptr))) = std::move (**Tpptr); // { dg-warning "moving '\\* \\* Tpptr' of type 'int' to itself" }
114 (Tref) = std::move (Tref); // { dg-warning "moving 'Tref' of type 'int' to itself" }
115 (Trref) = std::move (Trref); // { dg-warning "moving 'Trref' of type 'int' to itself" }
116 (Tcptr) = std::move (Tcptr); // { dg-warning "moving 'Tcptr' of type 'const int\\*' to itself" }
117 }
118
119 void
120 call_testargs ()
121 {
122 int i = 42;
123 int *p = &i;
124 testargs<int>(&i, &p, i, 42, &i);
125 }