summaryrefslogtreecommitdiffstats
path: root/clang/test/SemaCXX/warn-assignment-condition.cpp
diff options
context:
space:
mode:
authorJohn McCall <rjmccall@apple.com>2009-10-12 21:59:07 +0000
committerJohn McCall <rjmccall@apple.com>2009-10-12 21:59:07 +0000
commitd5707abdfdd5b07190d7473f9ef12e5f742cf5be (patch)
treef28c146eb502841ea8a42787c35b3a7a84e6a7eb /clang/test/SemaCXX/warn-assignment-condition.cpp
parent3250e7769fd6b39572c675a480a1e3cd04af4e4d (diff)
downloadbcm5719-llvm-d5707abdfdd5b07190d7473f9ef12e5f742cf5be.tar.gz
bcm5719-llvm-d5707abdfdd5b07190d7473f9ef12e5f742cf5be.zip
Implement -Wparentheses: warn about using assignments in contexts that require
conditions. Add a fixit to insert the parentheses. Also fix a very minor possible memory leak in 'for' conditions. Fixes PR 4876 and rdar://problem/7289172 llvm-svn: 83907
Diffstat (limited to 'clang/test/SemaCXX/warn-assignment-condition.cpp')
-rw-r--r--clang/test/SemaCXX/warn-assignment-condition.cpp65
1 files changed, 65 insertions, 0 deletions
diff --git a/clang/test/SemaCXX/warn-assignment-condition.cpp b/clang/test/SemaCXX/warn-assignment-condition.cpp
new file mode 100644
index 00000000000..3b9f3066a16
--- /dev/null
+++ b/clang/test/SemaCXX/warn-assignment-condition.cpp
@@ -0,0 +1,65 @@
+// RUN: clang-cc -fsyntax-only -Wparentheses -verify %s
+
+struct A {
+ int foo();
+ friend A operator+(const A&, const A&);
+ operator bool();
+};
+
+void test() {
+ int x, *p;
+ A a, b;
+
+ // With scalars.
+ if (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}}
+ if ((x = 7)) {}
+ do {
+ } while (x = 7); // expected-warning {{using the result of an assignment as a condition without parentheses}}
+ do {
+ } while ((x = 7));
+ while (x = 7) {} // expected-warning {{using the result of an assignment as a condition without parentheses}}
+ while ((x = 7)) {}
+ for (; x = 7; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}}
+ for (; (x = 7); ) {}
+
+ if (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}}
+ if ((p = p)) {}
+ do {
+ } while (p = p); // expected-warning {{using the result of an assignment as a condition without parentheses}}
+ do {
+ } while ((p = p));
+ while (p = p) {} // expected-warning {{using the result of an assignment as a condition without parentheses}}
+ while ((p = p)) {}
+ for (; p = p; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}}
+ for (; (p = p); ) {}
+
+ // Initializing variables (shouldn't warn).
+ if (int y = x) {}
+ while (int y = x) {}
+ if (A y = a) {}
+ while (A y = a) {}
+
+ // With temporaries.
+ if (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}}
+ if ((x = (b+b).foo())) {}
+ do {
+ } while (x = (b+b).foo()); // expected-warning {{using the result of an assignment as a condition without parentheses}}
+ do {
+ } while ((x = (b+b).foo()));
+ while (x = (b+b).foo()) {} // expected-warning {{using the result of an assignment as a condition without parentheses}}
+ while ((x = (b+b).foo())) {}
+ for (; x = (b+b).foo(); ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}}
+ for (; (x = (b+b).foo()); ) {}
+
+ // With a user-defined operator.
+ if (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}}
+ if ((a = b + b)) {}
+ do {
+ } while (a = b + b); // expected-warning {{using the result of an assignment as a condition without parentheses}}
+ do {
+ } while ((a = b + b));
+ while (a = b + b) {} // expected-warning {{using the result of an assignment as a condition without parentheses}}
+ while ((a = b + b)) {}
+ for (; a = b + b; ) {} // expected-warning {{using the result of an assignment as a condition without parentheses}}
+ for (; (a = b + b); ) {}
+}
OpenPOWER on IntegriCloud