xref: /llvm-project/clang/test/SemaCXX/warn-self-move.cpp (revision d9d9301eec6dfefcf53fd04b61324f140f273033)
1 // RUN: %clang_cc1 -fsyntax-only -Wself-move -std=c++11 -verify %s
2 
3 // definitions for std::move
4 namespace std {
5 inline namespace foo {
6 template <class T> struct remove_reference { typedef T type; };
7 template <class T> struct remove_reference<T&> { typedef T type; };
8 template <class T> struct remove_reference<T&&> { typedef T type; };
9 
10 template <class T> typename remove_reference<T>::type &&move(T &&t);
11 }
12 }
13 
int_test()14 void int_test() {
15   int x = 5;
16   x = std::move(x);  // expected-warning{{explicitly moving}}
17   (x) = std::move(x);  // expected-warning{{explicitly moving}}
18 
19   x = static_cast<int&&>(x);  // expected-warning{{explicitly moving}}
20   (x) = static_cast<int&&>(x);  // expected-warning{{explicitly moving}}
21 
22   using std::move;
23   x = move(x); // expected-warning{{explicitly moving}} \
24                    expected-warning {{unqualified call to 'std::move}}
25 }
26 
27 int global;
28 void global_int_test() {
29   global = std::move(global);  // expected-warning{{explicitly moving}}
30   (global) = std::move(global);  // expected-warning{{explicitly moving}}
31 
32   global = static_cast<int&&>(global);  // expected-warning{{explicitly moving}}
33   (global) = static_cast<int&&>(global);  // expected-warning{{explicitly moving}}
34 
35   using std::move;
36   global = move(global); // expected-warning{{explicitly moving}} \
37                              expected-warning {{unqualified call to 'std::move}}
38 }
39 
40 class field_test {
41   int x;
field_test(field_test && other)42   field_test(field_test&& other) {
43     x = std::move(x);  // expected-warning{{explicitly moving}}
44     x = static_cast<int&&>(x);  // expected-warning{{explicitly moving}}
45     x = std::move(other.x);
46     x = static_cast<int&&>(other.x);
47     other.x = std::move(x);
48     other.x = static_cast<int&&>(x);
49     other.x = std::move(other.x);  // expected-warning{{explicitly moving}}
50     other.x = static_cast<int&&>(other.x);  // expected-warning{{explicitly moving}}
51   }
withSuggest(int x)52   void withSuggest(int x) {
53     x = static_cast<int&&>(x); // expected-warning{{explicitly moving variable of type 'int' to itself; did you mean to move to member 'x'?}}
54     x = std::move(x); // expected-warning{{explicitly moving variable of type 'int' to itself; did you mean to move to member 'x'?}}
55   }
56 };
57 
58 struct A {};
59 struct B { A a; };
CC60 struct C { C() {}; ~C() {} };
struct_test()61 void struct_test() {
62   A a;
63   a = std::move(a);  // expected-warning{{explicitly moving}}
64   a = static_cast<A&&>(a);  // expected-warning{{explicitly moving}}
65 
66   B b;
67   b = std::move(b);  // expected-warning{{explicitly moving}}
68   b = static_cast<B&&>(b);  // expected-warning{{explicitly moving}}
69   b.a = std::move(b.a);  // expected-warning{{explicitly moving}}
70   b.a = static_cast<A&&>(b.a);  // expected-warning{{explicitly moving}}
71 
72   C c;
73   c = std::move(c);  // expected-warning{{explicitly moving}}
74   c = static_cast<C&&>(c);  // expected-warning{{explicitly moving}}
75 }
76