98 lines · cpp
1// RUN: %check_clang_tidy %s bugprone-shared-ptr-array-mismatch %t2 3namespace std {4 5template <typename T>6struct 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 std14 15struct A {};16 17void 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 off23 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 on27 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 36void 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 44void 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 50struct 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 60void f_parm(std::shared_ptr<int>);61 62void 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 67std::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 72template <class T>73void f_tmpl() {74 std::shared_ptr<T> P1{new T[10]};75 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: shared pointer to non-array is initialized with array [bugprone-shared-ptr-array-mismatch]76 // CHECK-FIXES: std::shared_ptr<T[]> P1{new T[10]};77}78 79void f5() {80 f_tmpl<char>();81}82 83#define CHAR_PTR_TYPE std::shared_ptr<char>84#define CHAR_PTR_VAR(X) \85 X { new char[10] }86#define CHAR_PTR_INIT(X, Y) \87 std::shared_ptr<char> X { Y }88 89void f6() {90 CHAR_PTR_TYPE P1{new char[10]};91 // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: shared pointer to non-array is initialized with array [bugprone-shared-ptr-array-mismatch]92 std::shared_ptr<char> CHAR_PTR_VAR(P2);93 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: shared pointer to non-array is initialized with array [bugprone-shared-ptr-array-mismatch]94 // CHECK-FIXES: std::shared_ptr<char[]> CHAR_PTR_VAR(P2);95 CHAR_PTR_INIT(P3, new char[10]);96 // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: shared pointer to non-array is initialized with array [bugprone-shared-ptr-array-mismatch]97}98