xbolva00 created this revision.
xbolva00 added reviewers: RKSimon, aaron.ballman.
Herald added subscribers: cfe-commits, jdoerfert.
Herald added a project: clang.

Follow up for (abandoned) patch https://reviews.llvm.org/D45401
Fixes https://bugs.llvm.org/show_bug.cgi?id=34180


Repository:
  rC Clang

https://reviews.llvm.org/D58878

Files:
  clang/lib/Sema/SemaExprCXX.cpp
  clang/test/Sema/assigment_in_bool_context.cpp


Index: clang/test/Sema/assigment_in_bool_context.cpp
===================================================================
--- /dev/null
+++ clang/test/Sema/assigment_in_bool_context.cpp
@@ -0,0 +1,57 @@
+// RUN: %clang_cc1 -Wparentheses -fsyntax-only -verify %s
+
+bool warn1(int x) {
+  return x = 0; // expected-warning {{using the result of an assignment as a 
condition without parentheses}}
+                // expected-note@-1 {{place parentheses around the assignment 
to silence this warning}}
+                // expected-note@-2 {{use '==' to turn this assignment into an 
equality comparison}}
+}
+
+bool warn2(int x, bool a, bool b) {
+  return x = 0 || (a && b); // expected-warning {{using the result of an 
assignment as a condition without parentheses}}
+                            // expected-note@-1 {{place parentheses around the 
assignment to silence this warning}}
+                            // expected-note@-2 {{use '==' to turn this 
assignment into an equality comparison}}
+}
+
+bool warn3(int x, bool a) {
+  return x = 0 || a; // expected-warning {{using the result of an assignment 
as a condition without parentheses}}
+                     // expected-note@-1 {{place parentheses around the 
assignment to silence this warning}}
+                     // expected-note@-2 {{use '==' to turn this assignment 
into an equality comparison}}
+}
+
+bool warn4(int x) {
+  return bool(x = 0); // expected-warning {{using the result of an assignment 
as a condition without parentheses}}
+                      // expected-note@-1 {{place parentheses around the 
assignment to silence this warning}}
+                      // expected-note@-2 {{use '==' to turn this assignment 
into an equality comparison}}
+}
+
+int warn5(int x) {
+  return bool(x = 0); // expected-warning {{using the result of an assignment 
as a condition without parentheses}}
+                      // expected-note@-1 {{place parentheses around the 
assignment to silence this warning}}
+                      // expected-note@-2 {{use '==' to turn this assignment 
into an equality comparison}}
+}
+
+bool warn6(int x, int a) {
+  return x = a; // expected-warning {{using the result of an assignment as a 
condition without parentheses}}
+                // expected-note@-1 {{place parentheses around the assignment 
to silence this warning}}
+                // expected-note@-2 {{use '==' to turn this assignment into an 
equality comparison}}
+}
+
+int nowarn1(int x) {
+  return (x = 0);
+}
+
+int nowarn2(int x) {
+  return x = 0;
+}
+
+int nowarn3(int x) {
+  return x == 0;
+}
+
+bool nowarn4(int x) {
+  return x == 0;
+}
+
+void nowarn5(int x) {
+  return void(x = 0);
+}
Index: clang/lib/Sema/SemaExprCXX.cpp
===================================================================
--- clang/lib/Sema/SemaExprCXX.cpp
+++ clang/lib/Sema/SemaExprCXX.cpp
@@ -4136,6 +4136,7 @@
   }
 
   case ICK_Boolean_Conversion:
+    DiagnoseAssignmentAsCondition(From->IgnoreImpCasts());
     // Perform half-to-boolean conversion via float.
     if (From->getType()->isHalfType()) {
       From = ImpCastExprToType(From, Context.FloatTy, CK_FloatingCast).get();


Index: clang/test/Sema/assigment_in_bool_context.cpp
===================================================================
--- /dev/null
+++ clang/test/Sema/assigment_in_bool_context.cpp
@@ -0,0 +1,57 @@
+// RUN: %clang_cc1 -Wparentheses -fsyntax-only -verify %s
+
+bool warn1(int x) {
+  return x = 0; // expected-warning {{using the result of an assignment as a condition without parentheses}}
+                // expected-note@-1 {{place parentheses around the assignment to silence this warning}}
+                // expected-note@-2 {{use '==' to turn this assignment into an equality comparison}}
+}
+
+bool warn2(int x, bool a, bool b) {
+  return x = 0 || (a && b); // expected-warning {{using the result of an assignment as a condition without parentheses}}
+                            // expected-note@-1 {{place parentheses around the assignment to silence this warning}}
+                            // expected-note@-2 {{use '==' to turn this assignment into an equality comparison}}
+}
+
+bool warn3(int x, bool a) {
+  return x = 0 || a; // expected-warning {{using the result of an assignment as a condition without parentheses}}
+                     // expected-note@-1 {{place parentheses around the assignment to silence this warning}}
+                     // expected-note@-2 {{use '==' to turn this assignment into an equality comparison}}
+}
+
+bool warn4(int x) {
+  return bool(x = 0); // expected-warning {{using the result of an assignment as a condition without parentheses}}
+                      // expected-note@-1 {{place parentheses around the assignment to silence this warning}}
+                      // expected-note@-2 {{use '==' to turn this assignment into an equality comparison}}
+}
+
+int warn5(int x) {
+  return bool(x = 0); // expected-warning {{using the result of an assignment as a condition without parentheses}}
+                      // expected-note@-1 {{place parentheses around the assignment to silence this warning}}
+                      // expected-note@-2 {{use '==' to turn this assignment into an equality comparison}}
+}
+
+bool warn6(int x, int a) {
+  return x = a; // expected-warning {{using the result of an assignment as a condition without parentheses}}
+                // expected-note@-1 {{place parentheses around the assignment to silence this warning}}
+                // expected-note@-2 {{use '==' to turn this assignment into an equality comparison}}
+}
+
+int nowarn1(int x) {
+  return (x = 0);
+}
+
+int nowarn2(int x) {
+  return x = 0;
+}
+
+int nowarn3(int x) {
+  return x == 0;
+}
+
+bool nowarn4(int x) {
+  return x == 0;
+}
+
+void nowarn5(int x) {
+  return void(x = 0);
+}
Index: clang/lib/Sema/SemaExprCXX.cpp
===================================================================
--- clang/lib/Sema/SemaExprCXX.cpp
+++ clang/lib/Sema/SemaExprCXX.cpp
@@ -4136,6 +4136,7 @@
   }
 
   case ICK_Boolean_Conversion:
+    DiagnoseAssignmentAsCondition(From->IgnoreImpCasts());
     // Perform half-to-boolean conversion via float.
     if (From->getType()->isHalfType()) {
       From = ImpCastExprToType(From, Context.FloatTy, CK_FloatingCast).get();
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to