Skip to content

Commit 1f06f41

Browse files
committed
PR44325 (and duplicates): don't issue -Wzero-as-null-pointer-constant
when rewriting 'a < b' as '(a <=> b) < 0'. It's pretty common for comparison category types to use a pointer or pointer-to-member type as their '0' parameter.
1 parent 7db390c commit 1f06f41

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

clang/lib/Sema/Sema.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,13 @@ void Sema::diagnoseZeroToNullptrConversion(CastKind Kind, const Expr* E) {
537537
if (E->IgnoreParenImpCasts()->getType()->isNullPtrType())
538538
return;
539539

540+
// Don't diagnose the conversion from a 0 literal to a null pointer argument
541+
// in a synthesized call to operator<=>.
542+
if (!CodeSynthesisContexts.empty() &&
543+
CodeSynthesisContexts.back().Kind ==
544+
CodeSynthesisContext::RewritingOperatorAsSpaceship)
545+
return;
546+
540547
// If it is a macro from system header, and if the macro name is not "NULL",
541548
// do not warn.
542549
SourceLocation MaybeMacroLoc = E->getBeginLoc();

clang/test/SemaCXX/cxx2a-three-way-comparison.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -std=c++2a -verify %s
1+
// RUN: %clang_cc1 -std=c++2a -verify %s -Wzero-as-null-pointer-constant
22

33
// Keep this test before any declarations of operator<=>.
44
namespace PR44786 {
@@ -40,3 +40,21 @@ namespace PR47893 {
4040
int &f(...);
4141
int &r = f(A(), A());
4242
}
43+
44+
namespace PR44325 {
45+
struct cmp_cat {};
46+
bool operator<(cmp_cat, void*);
47+
bool operator>(cmp_cat, int cmp_cat::*);
48+
49+
struct X {};
50+
cmp_cat operator<=>(X, X);
51+
52+
bool b1 = X() < X(); // no warning
53+
bool b2 = X() > X(); // no warning
54+
55+
// FIXME: It's not clear whether warning here is useful, but we can't really
56+
// tell that this is a comparison category in general. This is probably OK,
57+
// as comparisons against zero are only really intended for use in the
58+
// implicit rewrite rules, not for explicit use by programs.
59+
bool c = cmp_cat() < 0; // expected-warning {{zero as null pointer constant}}
60+
}

0 commit comments

Comments
 (0)