brintos

brintos / llvm-project-archived public Read only

0
0
Text · 5.0 KiB · 494e83d Raw
128 lines · cpp
1// RUN: %check_clang_tidy %s bugprone-unique-ptr-array-mismatch %t2 3namespace std {4 5template<class T> struct default_delete {};6template<class T> struct default_delete<T[]> {};7 8template<class T, class Deleter = std::default_delete<T>>9class unique_ptr {10public:11  explicit unique_ptr(T* p) noexcept;12  unique_ptr(T* p, Deleter d1 ) noexcept;13};14 15template <class T, class Deleter>16class unique_ptr<T[], Deleter> {17public: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 std25 26struct A {};27 28using PtrT = std::unique_ptr<A>;29using PtrTArr = std::unique_ptr<A[]>;30 31void 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 off37  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 on41  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 59void 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 67void 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 73struct 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 83void f_parm(std::unique_ptr<int>);84 85void 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 90std::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 95template <class T>96void 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 102void f5() {103  f_tmpl<char>();104}105 106template <class T>107void 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 119void 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