1 // RUN: %check_clang_tidy %s bugprone-shared-ptr-array-mismatch %t 2 3 namespace std { 4 5 template <typename T> 6 struct shared_ptr { 7 template <class Y> 8 explicit shared_ptr(Y *) {} 9 template <class Y, class Deleter> 10 shared_ptr(Y *, Deleter) {} 11 }; 12 13 } // namespace std 14 15 struct A {}; 16 17 void f1() { 18 std::shared_ptr<int> P1{new int}; 19 std::shared_ptr<int> P2{new int[10]}; 20 // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: shared pointer to non-array is initialized with array [bugprone-shared-ptr-array-mismatch] 21 // CHECK-FIXES: std::shared_ptr<int[]> P2{new int[10]}; 22 // clang-format off 23 std::shared_ptr< int > P3{new int[10]}; 24 // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: shared pointer to non-array is initialized with array [bugprone-shared-ptr-array-mismatch] 25 // CHECK-FIXES: std::shared_ptr< int[] > P3{new int[10]}; 26 // clang-format on 27 std::shared_ptr<int> P4(new int[10]); 28 // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: shared pointer to non-array is initialized with array [bugprone-shared-ptr-array-mismatch] 29 // CHECK-FIXES: std::shared_ptr<int[]> P4(new int[10]); 30 new std::shared_ptr<int>(new int[10]); 31 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: shared pointer to non-array is initialized with array [bugprone-shared-ptr-array-mismatch] 32 std::shared_ptr<int[]> P5(new int[10]); 33 std::shared_ptr<int> P6(new int[10], [](const int *Ptr) {}); 34 } 35 36 void f2() { 37 std::shared_ptr<A> P1(new A); 38 std::shared_ptr<A> P2(new A[10]); 39 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: shared pointer to non-array is initialized with array [bugprone-shared-ptr-array-mismatch] 40 // CHECK-FIXES: std::shared_ptr<A[]> P2(new A[10]); 41 std::shared_ptr<A[]> P3(new A[10]); 42 } 43 44 void f3() { 45 std::shared_ptr<int> P1{new int}, P2{new int[10]}, P3{new int[10]}; 46 // CHECK-MESSAGES: :[[@LINE-1]]:40: warning: shared pointer to non-array is initialized with array [bugprone-shared-ptr-array-mismatch] 47 // CHECK-MESSAGES: :[[@LINE-2]]:57: warning: shared pointer to non-array is initialized with array [bugprone-shared-ptr-array-mismatch] 48 } 49 50 struct S { 51 std::shared_ptr<int> P1; 52 std::shared_ptr<int> P2{new int[10]}; 53 // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: shared pointer to non-array is initialized with array [bugprone-shared-ptr-array-mismatch] 54 std::shared_ptr<int> P3{new int}, P4{new int[10]}; 55 // CHECK-MESSAGES: :[[@LINE-1]]:40: warning: shared pointer to non-array is initialized with array [bugprone-shared-ptr-array-mismatch] 56 S() : P1{new int[10]} {} 57 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: shared pointer to non-array is initialized with array [bugprone-shared-ptr-array-mismatch] 58 }; 59 60 void f_parm(std::shared_ptr<int>); 61 62 void f4() { 63 f_parm(std::shared_ptr<int>{new int[10]}); 64 // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: shared pointer to non-array is initialized with array [bugprone-shared-ptr-array-mismatch] 65 } 66 67 std::shared_ptr<int> f_ret() { 68 return std::shared_ptr<int>(new int[10]); 69 // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: shared pointer to non-array is initialized with array [bugprone-shared-ptr-array-mismatch] 70 } 71 72 template <class T> 73 void f_tmpl() { 74 std::shared_ptr<T> P1{new T[10]}; 75 } 76 77 void f5() { 78 f_tmpl<char>(); 79 } 80 81 #define CHAR_PTR_TYPE std::shared_ptr<char> 82 #define CHAR_PTR_VAR(X) \ 83 X { new char[10] } 84 #define CHAR_PTR_INIT(X, Y) \ 85 std::shared_ptr<char> X { Y } 86 87 void f6() { 88 CHAR_PTR_TYPE P1{new char[10]}; 89 // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: shared pointer to non-array is initialized with array [bugprone-shared-ptr-array-mismatch] 90 std::shared_ptr<char> CHAR_PTR_VAR(P2); 91 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: shared pointer to non-array is initialized with array [bugprone-shared-ptr-array-mismatch] 92 // CHECK-FIXES: std::shared_ptr<char[]> CHAR_PTR_VAR(P2); 93 CHAR_PTR_INIT(P3, new char[10]); 94 // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: shared pointer to non-array is initialized with array [bugprone-shared-ptr-array-mismatch] 95 } 96