This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Warn for dangerous use of omitted middle operand in ?:


+/* Warn for A ?: C expressions (with B omitted) where A is a boolean
+   expression, because B will always be true. */
+
+void
+warn_for_omitted_condop (location_t location, tree cond)
+{
+  enum tree_code code = TREE_CODE (cond);
+
+  if (!warn_parentheses)
+    return;
+  if (TREE_CODE_CLASS (code) == tcc_comparison
+      || code == TRUTH_ANDIF_EXPR
+      || code == TRUTH_ORIF_EXPR
+      || code == TRUTH_AND_EXPR
+      || code == TRUTH_XOR_EXPR
+      || code == TRUTH_OR_EXPR
+      || code == TRUTH_NOT_EXPR)
+    {
+      warning_at (location, OPT_Wparentheses,
+		"the omitted middle operand in ?: will always be %<true%>, "
+		"suggest explicit middle operand");
+    }
+}

As a follow up, if the else-branch is a comparison too you could warn as


    "the omitted middle operand in ?: will always be %<true%>, "
    "suggest using || instead of ?:"

Paolo


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]