brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.0 KiB · f8df02d Raw
63 lines · plain
1.. title:: clang-tidy - readability-ambiguous-smartptr-reset-call2 3readability-ambiguous-smartptr-reset-call4=========================================5 6Finds potentially erroneous calls to ``reset`` method on smart pointers when7the pointee type also has a ``reset`` method. Having a ``reset`` method in8both classes makes it easy to accidentally make the pointer null when9intending to reset the underlying object.10 11.. code-block:: c++12 13  struct Resettable {14    void reset() { /* Own reset logic */ }15  };16 17  auto ptr = std::make_unique<Resettable>();18 19  ptr->reset();  // Calls underlying reset method20  ptr.reset();   // Makes the pointer null21 22Both calls are valid C++ code, but the second one might not be what the23developer intended, as it destroys the pointed-to object rather than resetting24its state. It's easy to make such a typo because the difference between25``.`` and ``->`` is really small.26 27The recommended approach is to make the intent explicit by using either member28access or direct assignment:29 30.. code-block:: c++31 32  std::unique_ptr<Resettable> ptr = std::make_unique<Resettable>();33 34  (*ptr).reset();  // Clearly calls underlying reset method35  ptr = nullptr;   // Clearly makes the pointer null36 37The default smart pointers and classes that are considered are38``std::unique_ptr``, ``std::shared_ptr``, ``boost::shared_ptr``. To specify39other smart pointers or other classes use the :option:`SmartPointers` option.40 41 42.. note::43 44  The check may emit invalid fix-its and misleading warning messages when45  specifying custom smart pointers or other classes in the46  :option:`SmartPointers` option. For example, ``boost::scoped_ptr`` does not47  have an ``operator=`` which makes fix-its invalid.48 49.. note::50 51  Automatic fix-its are enabled only if :program:`clang-tidy` is invoked with52  the `--fix-notes` option.53 54 55Options56-------57 58.. option:: SmartPointers59 60    Semicolon-separated list of fully qualified class names of custom smart61    pointers. Default value is `::std::unique_ptr;::std::shared_ptr;62    ::boost::shared_ptr`.63