[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / warn-self-move.cpp
blob5937bb705ed22726ae67811cb03efe09055b0a18
1 // RUN: %clang_cc1 -fsyntax-only -Wself-move -std=c++11 -verify %s
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; };
10 template <class T> typename remove_reference<T>::type &&move(T &&t);
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}}
19 x = static_cast<int&&>(x); // expected-warning{{explicitly moving}}
20 (x) = static_cast<int&&>(x); // expected-warning{{explicitly moving}}
22 using std::move;
23 x = move(x); // expected-warning{{explicitly moving}} \
24 expected-warning {{unqualified call to 'std::move}}
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}}
32 global = static_cast<int&&>(global); // expected-warning{{explicitly moving}}
33 (global) = static_cast<int&&>(global); // expected-warning{{explicitly moving}}
35 using std::move;
36 global = move(global); // expected-warning{{explicitly moving}} \
37 expected-warning {{unqualified call to 'std::move}}
40 class field_test {
41 int x;
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}}
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'?}}
58 struct A {};
59 struct B { A a; };
60 struct C { C() {}; ~C() {} };
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}}
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}}
72 C c;
73 c = std::move(c); // expected-warning{{explicitly moving}}
74 c = static_cast<C&&>(c); // expected-warning{{explicitly moving}}