[JITLink][LoongArch] Support R_LARCH_ALIGN relaxation (#122259)
[llvm-project.git] / clang-tools-extra / docs / clang-tidy / checks / bugprone / shared-ptr-array-mismatch.rst
blob0833195edbb793925e6b40cec51d9b2f866edbe3
1 .. title:: clang-tidy - bugprone-shared-ptr-array-mismatch
3 bugprone-shared-ptr-array-mismatch
4 ==================================
6 Finds initializations of C++ shared pointers to non-array type that are
7 initialized with an array.
9 If a shared pointer ``std::shared_ptr<T>`` is initialized with a new-expression
10 ``new T[]`` the memory is not deallocated correctly. The pointer uses plain
11 ``delete`` in this case to deallocate the target memory. Instead a ``delete[]``
12 call is needed. A ``std::shared_ptr<T[]>`` calls the correct delete operator.
14 The check offers replacement of ``shared_ptr<T>`` to ``shared_ptr<T[]>`` if it
15 is used at a single variable declaration (one variable in one statement).
17 Example:
19 .. code-block:: c++
21   std::shared_ptr<Foo> x(new Foo[10]); // -> std::shared_ptr<Foo[]> x(new Foo[10]);
22   //                     ^ warning: shared pointer to non-array is initialized with array [bugprone-shared-ptr-array-mismatch]
23   std::shared_ptr<Foo> x1(new Foo), x2(new Foo[10]); // no replacement
24   //                                   ^ warning: shared pointer to non-array is initialized with array [bugprone-shared-ptr-array-mismatch]
26   std::shared_ptr<Foo> x3(new Foo[10], [](const Foo *ptr) { delete[] ptr; }); // no warning
28   struct S {
29     std::shared_ptr<Foo> x(new Foo[10]); // no replacement in this case
30     //                     ^ warning: shared pointer to non-array is initialized with array [bugprone-shared-ptr-array-mismatch]
31   };
33 This check partially covers the CERT C++ Coding Standard rule
34 `MEM51-CPP. Properly deallocate dynamically allocated resources
35 <https://wiki.sei.cmu.edu/confluence/display/cplusplus/MEM51-CPP.+Properly+deallocate+dynamically+allocated+resources>`_
36 However, only the ``std::shared_ptr`` case is detected by this check.