xref: /llvm-project/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr-cxx20.cpp (revision 01c11569fc67b2a00403f64695fff6d2b4e78fe5)
1 // RUN: %check_clang_tidy -std=c++20 %s modernize-use-nullptr %t -- -- -DGCC
2 // RUN: %check_clang_tidy -std=c++20 %s modernize-use-nullptr %t -- -- -DCLANG
3 
4 namespace std {
5 class strong_ordering;
6 
7 // Mock how STD defined unspecified parameters for the operators below.
8 #ifdef CLANG
9 struct _CmpUnspecifiedParam {
10   consteval
_CmpUnspecifiedParamstd::_CmpUnspecifiedParam11   _CmpUnspecifiedParam(int _CmpUnspecifiedParam::*) noexcept {}
12 };
13 
14 #define UNSPECIFIED_TYPE _CmpUnspecifiedParam
15 #endif
16 
17 #ifdef GCC
18 namespace __cmp_cat {
19   struct __unspec {
__unspecstd::__cmp_cat::__unspec20     constexpr __unspec(__unspec*) noexcept { }
21   };
22 }
23 
24 #define UNSPECIFIED_TYPE __cmp_cat::__unspec
25 #endif
26 
27 struct strong_ordering {
28   signed char value;
29 
operator ==(strong_ordering v,UNSPECIFIED_TYPE)30   friend constexpr bool operator==(strong_ordering v,
31                                    UNSPECIFIED_TYPE) noexcept {
32     return v.value == 0;
33   }
operator <(strong_ordering v,UNSPECIFIED_TYPE)34   friend constexpr bool operator<(strong_ordering v,
35                                   UNSPECIFIED_TYPE) noexcept {
36     return v.value < 0;
37   }
operator >(strong_ordering v,UNSPECIFIED_TYPE)38   friend constexpr bool operator>(strong_ordering v,
39                                   UNSPECIFIED_TYPE) noexcept {
40     return v.value > 0;
41   }
operator >=(strong_ordering v,UNSPECIFIED_TYPE)42   friend constexpr bool operator>=(strong_ordering v,
43                                    UNSPECIFIED_TYPE) noexcept {
44     return v.value >= 0;
45   }
46   static const strong_ordering equal, greater, less;
47 };
48 
49 constexpr strong_ordering strong_ordering::equal = {0};
50 constexpr strong_ordering strong_ordering::greater = {1};
51 constexpr strong_ordering strong_ordering::less = {-1};
52 } // namespace std
53 
54 class A {
55   int a;
56 public:
57   auto operator<=>(const A &other) const = default;
58   // CHECK-FIXES: auto operator<=>(const A &other) const = default;
59 };
60 
test_cxx_rewritten_binary_ops()61 void test_cxx_rewritten_binary_ops() {
62   A a1, a2;
63   bool result;
64   // should not change next line to (a1 nullptr a2)
65   result = (a1 < a2);
66   // CHECK-FIXES: result = (a1 < a2);
67   // should not change next line to (a1 nullptr a2)
68   result = (a1 >= a2);
69   // CHECK-FIXES: result = (a1 >= a2);
70   int *ptr = 0;
71   // CHECK-FIXES: int *ptr = nullptr;
72   result = (a1 > (ptr == 0 ? a1 : a2));
73   // CHECK-FIXES: result = (a1 > (ptr == nullptr ? a1 : a2));
74   result = (a1 > ((a1 > (ptr == 0 ? a1 : a2)) ? a1 : a2));
75   // CHECK-FIXES: result = (a1 > ((a1 > (ptr == nullptr ? a1 : a2)) ? a1 : a2));
76 }
77 
testValidZero()78 void testValidZero() {
79   A a1, a2;
80   auto result = a1 <=> a2;
81   if (result < 0) {}
82   // CHECK-FIXES: if (result < 0) {}
83 }
84 
85 template<class T1, class T2>
86 struct P {
87   T1 x1;
88   T2 x2;
89   friend auto operator<=>(const P&, const P&) = default;
90   // CHECK-FIXES: friend auto operator<=>(const P&, const P&) = default;
91 };
92 
foo(P<int,int> x,P<int,int> y)93 bool foo(P<int,int> x, P<int, int> y) { return x < y; }
94 // CHECK-FIXES: bool foo(P<int,int> x, P<int, int> y) { return x < y; }
95