[PATCH] D58878: [Diagnostics] Warn for assignments in bool contexts

Dávid Bolvanský via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Mar 2 13:19:58 PST 2019


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 at -1 {{place parentheses around the assignment to silence this warning}}
+                // expected-note at -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 at -1 {{place parentheses around the assignment to silence this warning}}
+                            // expected-note at -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 at -1 {{place parentheses around the assignment to silence this warning}}
+                     // expected-note at -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 at -1 {{place parentheses around the assignment to silence this warning}}
+                      // expected-note at -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 at -1 {{place parentheses around the assignment to silence this warning}}
+                      // expected-note at -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 at -1 {{place parentheses around the assignment to silence this warning}}
+                // expected-note at -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();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D58878.189061.patch
Type: text/x-patch
Size: 3140 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190302/c0d52e76/attachment.bin>


More information about the cfe-commits mailing list