1 // RUN: %clang_cc1 -fsyntax-only -Wself-assign-field -DDUMMY -verify %s
2 // RUN: %clang_cc1 -fsyntax-only -Wself-assign-field -DV0 -verify %s
3 // RUN: %clang_cc1 -fsyntax-only -Wself-assign-field -DV1 -verify %s
4 // RUN: %clang_cc1 -fsyntax-only -Wself-assign-field -DV2 -verify %s
5 // RUN: %clang_cc1 -fsyntax-only -Wself-assign-field -DV3 -verify %s
6 // RUN: %clang_cc1 -fsyntax-only -Wself-assign-field -DV4 -verify %s
15 S
&operator=(const S
&) = default;
17 S
&operator=(S
&) = default;
19 S
&operator=(const S
&);
23 #error Define something!
25 S
&operator*=(const S
&);
26 S
&operator/=(const S
&);
27 S
&operator%=(const S
&);
28 S
&operator+=(const S
&);
29 S
&operator-=(const S
&);
30 S
&operator<<=(const S
&);
31 S
&operator>>=(const S
&);
32 S
&operator&=(const S
&);
33 S
&operator|=(const S
&);
34 S
&operator^=(const S
&);
35 S
&operator=(const volatile S
&) volatile;
43 a
= a
; // expected-warning {{assigning field to itself}}
44 b
= b
; // expected-warning {{assigning field to itself}}
47 this->a
= a
; // expected-warning {{assigning field to itself}}
48 this->b
= b
; // expected-warning {{assigning field to itself}}
49 a
= this->a
; // expected-warning {{assigning field to itself}}
50 b
= this->b
; // expected-warning {{assigning field to itself}}
51 this->a
= this->a
; // expected-warning {{assigning field to itself}}
52 this->b
= this->b
; // expected-warning {{assigning field to itself}}
61 a
/= a
; // expected-warning {{assigning field to itself}}
62 a
%= a
; // expected-warning {{assigning field to itself}}
64 a
-= a
; // expected-warning {{assigning field to itself}}
67 a
&= a
; // expected-warning {{assigning field to itself}}
68 a
|= a
; // expected-warning {{assigning field to itself}}
69 a
^= a
; // expected-warning {{assigning field to itself}}
73 void false_positives() {
77 // These shouldn't warn due to the use of the preprocessor.
86 // Ways to silence the warning.
89 a
= static_cast<decltype(a
) &>(a
);
95 // Volatile stores aren't side-effect free.
97 volatile S
&vol_a_ref
= vol_a
;
98 vol_a_ref
= vol_a_ref
;
103 // Do not diagnose self-assigment in an unevaluated context
105 SNoExcept() = default;
106 SNoExcept
&operator=(const SNoExcept
&) noexcept
;
108 struct false_positives_unevaluated_ctx_class
{
111 void false_positives_unevaluated_ctx(SNoExcept a
) noexcept(noexcept(a
= a
)) {
112 decltype(a
= a
) b
= a
;
113 static_assert(noexcept(a
= a
), "");
114 static_assert(sizeof(a
= a
), "");
118 template <typename T
>
119 struct TemplateClass
{
122 var
= var
; // expected-warning {{assigning field to itself}}
127 TemplateClass
<int> c
;
136 // It may make sense not to warn on the rest of the tests.
137 // It may be a valid use-case to self-assign to tell the compiler that
138 // it is ok to vectorize the store.
140 void f0(C
*s
, C
*t
) {
145 void f1(C
&s
, C
&t
) {
154 void f2(T
*t
, T
*t2
) {
159 void f3(T
&t
, T
&t2
) {