blob: ce16a68a77efcee29a35a2b5a77d75c742a84028 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -Wparentheses -verify %s
John McCall5a881bb2009-10-12 21:59:07 +00002
3struct A {
4 int foo();
5 friend A operator+(const A&, const A&);
6 operator bool();
7};
8
9void test() {
10 int x, *p;
11 A a, b;
12
13 // With scalars.
Douglas Gregor827feec2010-01-08 00:20:23 +000014 if (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
15 // expected-note{{use '==' to turn this assignment into an equality comparison}}
John McCall5a881bb2009-10-12 21:59:07 +000016 if ((x = 7)) {}
17 do {
Douglas Gregor827feec2010-01-08 00:20:23 +000018 } while (x = 7); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
19 // expected-note{{use '==' to turn this assignment into an equality comparison}}
John McCall5a881bb2009-10-12 21:59:07 +000020 do {
21 } while ((x = 7));
Douglas Gregor827feec2010-01-08 00:20:23 +000022 while (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
23 // expected-note{{use '==' to turn this assignment into an equality comparison}}
John McCall5a881bb2009-10-12 21:59:07 +000024 while ((x = 7)) {}
Douglas Gregor827feec2010-01-08 00:20:23 +000025 for (; x = 7; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
26 // expected-note{{use '==' to turn this assignment into an equality comparison}}
John McCall5a881bb2009-10-12 21:59:07 +000027 for (; (x = 7); ) {}
28
Douglas Gregor827feec2010-01-08 00:20:23 +000029 if (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
30 // expected-note{{use '==' to turn this assignment into an equality comparison}}
John McCall5a881bb2009-10-12 21:59:07 +000031 if ((p = p)) {}
32 do {
Douglas Gregor827feec2010-01-08 00:20:23 +000033 } while (p = p); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
34 // expected-note{{use '==' to turn this assignment into an equality comparison}}
John McCall5a881bb2009-10-12 21:59:07 +000035 do {
36 } while ((p = p));
Douglas Gregor827feec2010-01-08 00:20:23 +000037 while (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
38 // expected-note{{use '==' to turn this assignment into an equality comparison}}
John McCall5a881bb2009-10-12 21:59:07 +000039 while ((p = p)) {}
Douglas Gregor827feec2010-01-08 00:20:23 +000040 for (; p = p; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
41 // expected-note{{use '==' to turn this assignment into an equality comparison}}
John McCall5a881bb2009-10-12 21:59:07 +000042 for (; (p = p); ) {}
43
44 // Initializing variables (shouldn't warn).
45 if (int y = x) {}
46 while (int y = x) {}
47 if (A y = a) {}
48 while (A y = a) {}
49
50 // With temporaries.
Douglas Gregor827feec2010-01-08 00:20:23 +000051 if (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
52 // expected-note{{use '==' to turn this assignment into an equality comparison}}
John McCall5a881bb2009-10-12 21:59:07 +000053 if ((x = (b+b).foo())) {}
54 do {
Douglas Gregor827feec2010-01-08 00:20:23 +000055 } while (x = (b+b).foo()); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
56 // expected-note{{use '==' to turn this assignment into an equality comparison}}
John McCall5a881bb2009-10-12 21:59:07 +000057 do {
58 } while ((x = (b+b).foo()));
Douglas Gregor827feec2010-01-08 00:20:23 +000059 while (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
60 // expected-note{{use '==' to turn this assignment into an equality comparison}}
John McCall5a881bb2009-10-12 21:59:07 +000061 while ((x = (b+b).foo())) {}
Douglas Gregor827feec2010-01-08 00:20:23 +000062 for (; x = (b+b).foo(); ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
63 // expected-note{{use '==' to turn this assignment into an equality comparison}}
John McCall5a881bb2009-10-12 21:59:07 +000064 for (; (x = (b+b).foo()); ) {}
65
66 // With a user-defined operator.
Douglas Gregor827feec2010-01-08 00:20:23 +000067 if (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
68 // expected-note{{use '==' to turn this assignment into an equality comparison}}
John McCall5a881bb2009-10-12 21:59:07 +000069 if ((a = b + b)) {}
70 do {
Douglas Gregor827feec2010-01-08 00:20:23 +000071 } while (a = b + b); // expected-warning {{using the result of an assignment as a condition without parentheses}} \
72 // expected-note{{use '==' to turn this assignment into an equality comparison}}
John McCall5a881bb2009-10-12 21:59:07 +000073 do {
74 } while ((a = b + b));
Douglas Gregor827feec2010-01-08 00:20:23 +000075 while (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
76 // expected-note{{use '==' to turn this assignment into an equality comparison}}
John McCall5a881bb2009-10-12 21:59:07 +000077 while ((a = b + b)) {}
Douglas Gregor827feec2010-01-08 00:20:23 +000078 for (; a = b + b; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}} \
79 // expected-note{{use '==' to turn this assignment into an equality comparison}}
John McCall5a881bb2009-10-12 21:59:07 +000080 for (; (a = b + b); ) {}
81}