]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: -Wzero-as-null-pointer-constant and <=> [PR100903]
authorJason Merrill <jason@redhat.com>
Tue, 12 May 2026 15:27:29 +0000 (11:27 -0400)
committerJason Merrill <jason@redhat.com>
Tue, 12 May 2026 16:20:06 +0000 (12:20 -0400)
(x <=> y) < 0 is the pattern suggested by the standard, we shouldn't warn
about it because of our implementation strategy.

We already disable the warning in <compare> to avoid warnings on the uses of
this pattern within the header, so no library changes are needed.

PR c++/100903

gcc/cp/ChangeLog:

* call.cc (build_over_call): Avoid -Wzero-as-null-pointer-constant
if the warning is disabled around the called function.

gcc/testsuite/ChangeLog:

* g++.dg/cpp2a/spaceship-warn1.C: New test.

gcc/cp/call.cc
gcc/testsuite/g++.dg/cpp2a/spaceship-warn1.C [new file with mode: 0644]

index 92c3a23ab8d9c5bedf195d685d9b95e6bb086eab..97cb0d6f01249a6d20bede3e8d48e0ec626027e9 100644 (file)
@@ -10715,10 +10715,18 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
              func(NULL);
            }
       */
-      bool const conversion_warning = !(null_node_p (current_arg)
-                                       && DECL_TEMPLATE_INFO (fn)
-                                       && cand->template_decl
-                                       && !cand->explicit_targs);
+      bool conversion_warning = !(null_node_p (current_arg)
+                                 && DECL_TEMPLATE_INFO (fn)
+                                 && cand->template_decl
+                                 && !cand->explicit_targs);
+
+      /* Also don't warn about (x <=> y) < 0 (c++/100903).  */
+      if (conversion_warning
+         && integer_zerop (current_arg)
+         && warn_zero_as_null_pointer_constant
+         && !warning_enabled_at (DECL_SOURCE_LOCATION (fn),
+                                 OPT_Wzero_as_null_pointer_constant))
+       conversion_warning = false;
 
       tsubst_flags_t const arg_complain
        = conversion_warning ? complain : complain & ~tf_warning;
diff --git a/gcc/testsuite/g++.dg/cpp2a/spaceship-warn1.C b/gcc/testsuite/g++.dg/cpp2a/spaceship-warn1.C
new file mode 100644 (file)
index 0000000..998abef
--- /dev/null
@@ -0,0 +1,10 @@
+// PR c++/100903
+// { dg-additional-options -Wzero-as-null-pointer-constant }
+// { dg-do compile { target c++20 } }
+
+#include <compare>
+
+int main()
+{
+  return (1.0 <=> 2.0) < 0;
+}