Files
clang-p2996/clang-tools-extra/docs/clang-tidy/checks/modernize/use-scoped-lock.rst
Baranov Victor a3a60e03e2 [clang-tidy] add new check: modernize-use-scoped-lock (#126434)
Add new clang-tidy check that finds uses of `std::lock_guard` and suggests
replacing them with C++17's more flexible and safer alternative
`std::scoped_lock`.

Here is a small description of how it works for better understanding of
the code:
Two separate AST matchers are registered:

- The first one matches declarations of `std::lock_guard` that are
single in their scope (only one `std::lock_guard` in `CompoundStmt`).
It's an easy case, we can emit warning right away.

- The second one matches `CompoundStmt`'s that have multiple
`std::lock_guard` declarations, which means that we may have consecutive
declarations of `std::lock_guard` that can be replaced by a single
`std::scoped_lock`. In order to ensure that declarations are
consecutive, we need to loop over `Stmt`'s in `CompoundStmt`. Here is a
small example:
```cpp
{
  std::mutex m1, m2;
  std::lock(m1, m2);
  std::lock_guard<std::mutex> l1(m, std::adopt_lock); // first declaration of 'std::lock_guard'
  std::lock_guard<std::mutex> l2(m, std::adopt_lock); // second declaration of 'std::lock_guard' that can be merged with first using 'scoped_lock'
}
```

This PR closes https://github.com/llvm/llvm-project/issues/107839.
2025-06-29 22:34:32 +03:00

101 lines
2.5 KiB
ReStructuredText

.. title:: clang-tidy - modernize-use-scoped-lock
modernize-use-scoped-lock
=========================
Finds uses of ``std::lock_guard`` and suggests replacing them with C++17's
alternative ``std::scoped_lock``.
Fix-its are provided for single declarations of ``std::lock_guard`` and warning
is emitted for multiple declarations of ``std::lock_guard`` that can be
replaced with a single declaration of ``std::scoped_lock``.
Examples
--------
Single ``std::lock_guard`` declaration:
.. code-block:: c++
std::mutex M;
std::lock_guard<std::mutex> L(M);
Transforms to:
.. code-block:: c++
std::mutex M;
std::scoped_lock L(M);
Single ``std::lock_guard`` declaration with ``std::adopt_lock``:
.. code-block:: c++
std::mutex M;
std::lock(M);
std::lock_guard<std::mutex> L(M, std::adopt_lock);
Transforms to:
.. code-block:: c++
std::mutex M;
std::lock(M);
std::scoped_lock L(std::adopt_lock, M);
Multiple ``std::lock_guard`` declarations only emit warnings:
.. code-block:: c++
std::mutex M1, M2;
std::lock(M1, M2);
std::lock_guard Lock1(M, std::adopt_lock); // warning: use single 'std::scoped_lock' instead of multiple 'std::lock_guard'
std::lock_guard Lock2(M, std::adopt_lock); // note: additional 'std::lock_guard' declared here
Limitations
-----------
The check will not emit warnings if ``std::lock_guard`` is used implicitly via
``template`` parameter:
.. code-block:: c++
template <template <typename> typename Lock>
void TemplatedLock() {
std::mutex M;
Lock<std::mutex> L(M); // no warning
}
void instantiate() {
TemplatedLock<std::lock_guard>();
}
Options
-------
.. option:: WarnOnSingleLocks
When `true`, the check will warn on single ``std::lock_guard`` declarations.
Set this option to `false` if you want to get warnings only on multiple
``std::lock_guard`` declarations that can be replaced with a single
``std::scoped_lock``. Default is `true`.
.. option:: WarnOnUsingAndTypedef
When `true`, the check will emit warnings if ``std::lock_guard`` is used
in ``using`` or ``typedef`` context. Default is `true`.
.. code-block:: c++
template <typename T>
using Lock = std::lock_guard<T>; // warning: use 'std::scoped_lock' instead of 'std::lock_guard'
using LockMutex = std::lock_guard<std::mutex>; // warning: use 'std::scoped_lock' instead of 'std::lock_guard'
typedef std::lock_guard<std::mutex> LockDef; // warning: use 'std::scoped_lock' instead of 'std::lock_guard'
using std::lock_guard; // warning: use 'std::scoped_lock' instead of 'std::lock_guard'