1// RUN: %check_clang_tidy %s cppcoreguidelines-owning-memory %t
2
3namespace gsl {
4template <typename T>
5using owner = T;
6}
7
8namespace std {
9
10// Not actually a vector, but more a dynamic, fixed size array. Just to demonstrate
11// functionality or the lack of the same.
12template <typename T>
13class vector {
14public:
15 vector(unsigned long size, T val) : data{new T[size]}, size{size} {
16 for (unsigned long i = 0ul; i < size; ++i) {
17 data[i] = val;
18 }
19 }
20
21 T *begin() { return data; }
22 T *end() { return &data[size]; }
23 T &operator[](unsigned long index) { return data[index]; }
24
25private:
26 T *data;
27 unsigned long size;
28};
29
30} // namespace std
31
32// All of the following codesnippets should be valid with appropriate 'owner<>' analysis,
33// but currently the type information of 'gsl::owner<>' gets lost in typededuction.
34int main() {
35 std::vector<gsl::owner<int *>> OwnerStdVector(100, nullptr);
36
37 // Rangebased looping in resource vector.
38 for (auto *Element : OwnerStdVector) {
39 Element = new int(42);
40 // CHECK-NOTES: [[@LINE-1]]:5: warning: assigning newly created 'gsl::owner<>' to non-owner 'int *'
41 }
42 for (auto *Element : OwnerStdVector) {
43 delete Element;
44 // CHECK-NOTES: [[@LINE-1]]:5: warning: deleting a pointer through a type that is not marked 'gsl::owner<>'; consider using a smart pointer instead
45 // CHECK-NOTES: [[@LINE-3]]:8: note: variable declared here
46 }
47
48 // Indexbased looping in resource vector.
49 for (int i = 0; i < 100; ++i) {
50 OwnerStdVector[i] = new int(42);
51 // CHECK-NOTES: [[@LINE-1]]:5: warning: assigning newly created 'gsl::owner<>' to non-owner 'int *'
52 }
53 for (int i = 0; i < 100; ++i) {
54 delete OwnerStdVector[i];
55 // CHECK-NOTES: [[@LINE-1]]:5: warning: deleting a pointer through a type that is not marked 'gsl::owner<>'; consider using a smart pointer instead
56 // CHECK-NOTES: [[@LINE-21]]:3: note: variable declared here
57 // A note gets emitted here pointing to the return value of the operator[] from the
58 // vector implementation. Maybe this is considered misleading.
59 }
60
61 return 0;
62}
63

source code of clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/owning-memory-containers.cpp