[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / warn-self-assign-builtin.cpp
blob5211accce79d3ff248bc7a5836396374a48451b2
1 // RUN: %clang_cc1 -fsyntax-only -Wself-assign -verify %s
3 void f() {
4 int a = 42, b = 42;
5 a = a; // expected-warning{{explicitly assigning}}
6 b = b; // expected-warning{{explicitly assigning}}
7 a = b;
8 b = a = b;
9 a = a = a; // expected-warning{{explicitly assigning}}
10 a = b = b = a;
12 a *= a;
13 a /= a;
14 a %= a;
15 a += a;
16 a -= a;
17 a <<= a;
18 a >>= a;
19 a &= a; // expected-warning {{explicitly assigning}}
20 a |= a; // expected-warning {{explicitly assigning}}
21 a ^= a;
24 // Dummy type.
25 struct S {};
27 void false_positives() {
28 #define OP =
29 #define LHS a
30 #define RHS a
31 int a = 42;
32 // These shouldn't warn due to the use of the preprocessor.
33 a OP a;
34 LHS = a;
35 a = RHS;
36 LHS OP RHS;
37 #undef OP
38 #undef LHS
39 #undef RHS
41 // A way to silence the warning.
42 a = (int &)a;
44 // Volatile stores aren't side-effect free.
45 volatile int vol_a;
46 vol_a = vol_a;
47 volatile int &vol_a_ref = vol_a;
48 vol_a_ref = vol_a_ref;
51 // Do not diagnose self-assigment in an unevaluated context
52 void false_positives_unevaluated_ctx(int a) noexcept(noexcept(a = a)) // expected-warning {{expression with side effects has no effect in an unevaluated context}}
54 decltype(a = a) b = a; // expected-warning {{expression with side effects has no effect in an unevaluated context}}
55 static_assert(noexcept(a = a), ""); // expected-warning {{expression with side effects has no effect in an unevaluated context}}
56 static_assert(sizeof(a = a), ""); // expected-warning {{expression with side effects has no effect in an unevaluated context}}
59 template <typename T>
60 void g() {
61 T a;
62 a = a; // expected-warning{{explicitly assigning}}
64 void instantiate() {
65 g<int>();
66 g<S>();
69 struct Foo {
70 int A;
72 Foo(int A) : A(A) {}
74 void setA(int A) {
75 A = A; // expected-warning{{explicitly assigning value of variable of type 'int' to itself; did you mean to assign to member 'A'?}}
78 void setThroughLambda() {
79 [](int A) {
80 // To fix here we would need to insert an explicit capture 'this'
81 A = A; // expected-warning{{explicitly assigning}}
82 }(5);
84 [this](int A) {
85 this->A = 0;
86 // This fix would be possible by just adding this-> as above, but currently unsupported.
87 A = A; // expected-warning{{explicitly assigning}}
88 }(5);