diff --git a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp --- a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp @@ -62,7 +62,9 @@ ImplicitCastToNull, hasAncestor(cxxRewrittenBinaryOperator().bind( "checkBinopOperands"))) - .bind(CastSequence)))))); + .bind(CastSequence))), + // Skip defaulted comparison operators. + unless(hasAncestor(functionDecl(isDefaulted())))))); } bool isReplaceableRange(SourceLocation StartLoc, SourceLocation EndLoc, diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr-cxx20.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr-cxx20.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr-cxx20.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr-cxx20.cpp @@ -1,9 +1,33 @@ // RUN: %check_clang_tidy -std=c++20 %s modernize-use-nullptr %t namespace std { +class strong_ordering; + +// Mock how STD defined unspecified parameters for the operators below. +struct _CmpUnspecifiedParam { + consteval + _CmpUnspecifiedParam(int _CmpUnspecifiedParam::*) noexcept {} +}; + struct strong_ordering { - int n; - constexpr operator int() const { return n; } + signed char value; + + friend constexpr bool operator==(strong_ordering v, + _CmpUnspecifiedParam) noexcept { + return v.value == 0; + } + friend constexpr bool operator<(strong_ordering v, + _CmpUnspecifiedParam) noexcept { + return v.value < 0; + } + friend constexpr bool operator>(strong_ordering v, + _CmpUnspecifiedParam) noexcept { + return v.value > 0; + } + friend constexpr bool operator>=(strong_ordering v, + _CmpUnspecifiedParam) noexcept { + return v.value >= 0; + } static const strong_ordering equal, greater, less; }; constexpr strong_ordering strong_ordering::equal = {0}; @@ -12,8 +36,10 @@ } // namespace std class A { + int a; public: auto operator<=>(const A &other) const = default; + // CHECK-FIXES: auto operator<=>(const A &other) const = default; }; void test_cxx_rewritten_binary_ops() { @@ -32,3 +58,14 @@ result = (a1 > ((a1 > (ptr == 0 ? a1 : a2)) ? a1 : a2)); // CHECK-FIXES: result = (a1 > ((a1 > (ptr == nullptr ? a1 : a2)) ? a1 : a2)); } + +template +struct P { + T1 x1; + T2 x2; + friend auto operator<=>(const P&, const P&) = default; + // CHECK-FIXES: friend auto operator<=>(const P&, const P&) = default; +}; + +bool foo(P x, P y) { return x < y; } +// CHECK-FIXES: bool foo(P x, P y) { return x < y; }