blob: 3b9f3066a16be7bfea05703d00b98fa609746bff [file] [log] [blame]
John McCall5a881bb2009-10-12 21:59:07 +00001// RUN: clang-cc -fsyntax-only -Wparentheses -verify %s
2
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.
14 if (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}}
15 if ((x = 7)) {}
16 do {
17 } while (x = 7); // expected-warning {{using the result of an assignment as a condition without parentheses}}
18 do {
19 } while ((x = 7));
20 while (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}}
21 while ((x = 7)) {}
22 for (; x = 7; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}}
23 for (; (x = 7); ) {}
24
25 if (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}}
26 if ((p = p)) {}
27 do {
28 } while (p = p); // expected-warning {{using the result of an assignment as a condition without parentheses}}
29 do {
30 } while ((p = p));
31 while (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}}
32 while ((p = p)) {}
33 for (; p = p; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}}
34 for (; (p = p); ) {}
35
36 // Initializing variables (shouldn't warn).
37 if (int y = x) {}
38 while (int y = x) {}
39 if (A y = a) {}
40 while (A y = a) {}
41
42 // With temporaries.
43 if (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}}
44 if ((x = (b+b).foo())) {}
45 do {
46 } while (x = (b+b).foo()); // expected-warning {{using the result of an assignment as a condition without parentheses}}
47 do {
48 } while ((x = (b+b).foo()));
49 while (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}}
50 while ((x = (b+b).foo())) {}
51 for (; x = (b+b).foo(); ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}}
52 for (; (x = (b+b).foo()); ) {}
53
54 // With a user-defined operator.
55 if (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}}
56 if ((a = b + b)) {}
57 do {
58 } while (a = b + b); // expected-warning {{using the result of an assignment as a condition without parentheses}}
59 do {
60 } while ((a = b + b));
61 while (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}}
62 while ((a = b + b)) {}
63 for (; a = b + b; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}}
64 for (; (a = b + b); ) {}
65}