| 1 | // RUN: %check_clang_tidy %s bugprone-unique-ptr-array-mismatch %t |
| 2 | |
| 3 | namespace std { |
| 4 | |
| 5 | template<class T> struct default_delete {}; |
| 6 | template<class T> struct default_delete<T[]> {}; |
| 7 | |
| 8 | template<class T, class Deleter = std::default_delete<T>> |
| 9 | class unique_ptr { |
| 10 | public: |
| 11 | explicit unique_ptr(T* p) noexcept; |
| 12 | unique_ptr(T* p, Deleter d1 ) noexcept; |
| 13 | }; |
| 14 | |
| 15 | template <class T, class Deleter> |
| 16 | class unique_ptr<T[], Deleter> { |
| 17 | public: |
| 18 | template<class U> |
| 19 | explicit unique_ptr(U p) noexcept; |
| 20 | template<class U> |
| 21 | unique_ptr(U p, Deleter d1) noexcept; |
| 22 | }; |
| 23 | |
| 24 | } // namespace std |
| 25 | |
| 26 | struct A {}; |
| 27 | |
| 28 | using PtrT = std::unique_ptr<A>; |
| 29 | using PtrTArr = std::unique_ptr<A[]>; |
| 30 | |
| 31 | void f1() { |
| 32 | std::unique_ptr<int> P1{new int}; |
| 33 | std::unique_ptr<int> P2{new int[10]}; |
| 34 | // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: unique pointer to non-array is initialized with array [bugprone-unique-ptr-array-mismatch] |
| 35 | // CHECK-FIXES: std::unique_ptr<int[]> P2{new int[10]}; |
| 36 | // clang-format off |
| 37 | std::unique_ptr< int > P3{new int[10]}; |
| 38 | // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: unique pointer to non-array is initialized with array [bugprone-unique-ptr-array-mismatch] |
| 39 | // CHECK-FIXES: std::unique_ptr< int[] > P3{new int[10]}; |
| 40 | // clang-format on |
| 41 | std::unique_ptr<int> P4(new int[10]); |
| 42 | // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: unique pointer to non-array is initialized with array [bugprone-unique-ptr-array-mismatch] |
| 43 | // CHECK-FIXES: std::unique_ptr<int[]> P4(new int[10]); |
| 44 | new std::unique_ptr<int>(new int[10]); |
| 45 | // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: unique pointer to non-array is initialized with array [bugprone-unique-ptr-array-mismatch] |
| 46 | std::unique_ptr<int[]> P5(new int[10]); |
| 47 | |
| 48 | A deleter; |
| 49 | std::unique_ptr<int, A> P6(new int[10], deleter); |
| 50 | std::unique_ptr<int, A> P7(new int[10]); |
| 51 | std::default_delete<int[]> def_del; |
| 52 | std::unique_ptr<int, std::default_delete<int[]>> P8(new int[10], def_del); |
| 53 | |
| 54 | new PtrT(new A[10]); |
| 55 | // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: unique pointer to non-array is initialized with array [bugprone-unique-ptr-array-mismatch] |
| 56 | new PtrTArr(new A[10]); |
| 57 | } |
| 58 | |
| 59 | void f2() { |
| 60 | std::unique_ptr<A> P1(new A); |
| 61 | std::unique_ptr<A> P2(new A[10]); |
| 62 | // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: unique pointer to non-array is initialized with array [bugprone-unique-ptr-array-mismatch] |
| 63 | // CHECK-FIXES: std::unique_ptr<A[]> P2(new A[10]); |
| 64 | std::unique_ptr<A[]> P3(new A[10]); |
| 65 | } |
| 66 | |
| 67 | void f3() { |
| 68 | std::unique_ptr<int> P1{new int}, P2{new int[10]}, P3{new int[10]}; |
| 69 | // CHECK-MESSAGES: :[[@LINE-1]]:40: warning: unique pointer to non-array is initialized with array [bugprone-unique-ptr-array-mismatch] |
| 70 | // CHECK-MESSAGES: :[[@LINE-2]]:57: warning: unique pointer to non-array is initialized with array [bugprone-unique-ptr-array-mismatch] |
| 71 | } |
| 72 | |
| 73 | struct S { |
| 74 | std::unique_ptr<int> P1; |
| 75 | std::unique_ptr<int> P2{new int[10]}; |
| 76 | // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: unique pointer to non-array is initialized with array [bugprone-unique-ptr-array-mismatch] |
| 77 | std::unique_ptr<int> P3{new int}, P4{new int[10]}; |
| 78 | // CHECK-MESSAGES: :[[@LINE-1]]:40: warning: unique pointer to non-array is initialized with array [bugprone-unique-ptr-array-mismatch] |
| 79 | S() : P1{new int[10]} {} |
| 80 | // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: unique pointer to non-array is initialized with array [bugprone-unique-ptr-array-mismatch] |
| 81 | }; |
| 82 | |
| 83 | void f_parm(std::unique_ptr<int>); |
| 84 | |
| 85 | void f4() { |
| 86 | f_parm(std::unique_ptr<int>{new int[10]}); |
| 87 | // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: unique pointer to non-array is initialized with array [bugprone-unique-ptr-array-mismatch] |
| 88 | } |
| 89 | |
| 90 | std::unique_ptr<int> f_ret() { |
| 91 | return std::unique_ptr<int>(new int[10]); |
| 92 | // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: unique pointer to non-array is initialized with array [bugprone-unique-ptr-array-mismatch] |
| 93 | } |
| 94 | |
| 95 | template <class T> |
| 96 | void f_tmpl() { |
| 97 | std::unique_ptr<T> P1{new T[10]}; |
| 98 | // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: unique pointer to non-array is initialized with array [bugprone-unique-ptr-array-mismatch] |
| 99 | // CHECK-FIXES: std::unique_ptr<T[]> P1{new T[10]}; |
| 100 | } |
| 101 | |
| 102 | void f5() { |
| 103 | f_tmpl<char>(); |
| 104 | } |
| 105 | |
| 106 | template <class T> |
| 107 | void f_tmpl_1() { |
| 108 | std::unique_ptr<T> P1{new T[10]}; |
| 109 | // FIXME_CHECK-MESSAGES: :[[@LINE-1]]:25: warning: unique pointer to non-array is initialized with array [bugprone-unique-ptr-array-mismatch] |
| 110 | // FIXME_CHECK-FIXES: std::unique_ptr<T[]> P1{new T[10]}; |
| 111 | } |
| 112 | |
| 113 | #define CHAR_PTR_TYPE std::unique_ptr<char> |
| 114 | #define CHAR_PTR_VAR(X) \ |
| 115 | X { new char[10] } |
| 116 | #define CHAR_PTR_INIT(X, Y) \ |
| 117 | std::unique_ptr<char> X { Y } |
| 118 | |
| 119 | void f6() { |
| 120 | CHAR_PTR_TYPE P1{new char[10]}; |
| 121 | // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: unique pointer to non-array is initialized with array [bugprone-unique-ptr-array-mismatch] |
| 122 | std::unique_ptr<char> CHAR_PTR_VAR(P2); |
| 123 | // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: unique pointer to non-array is initialized with array [bugprone-unique-ptr-array-mismatch] |
| 124 | // CHECK-FIXES: std::unique_ptr<char[]> CHAR_PTR_VAR(P2); |
| 125 | CHAR_PTR_INIT(P3, new char[10]); |
| 126 | // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: unique pointer to non-array is initialized with array [bugprone-unique-ptr-array-mismatch] |
| 127 | } |
| 128 | |