[all-commits] [llvm/llvm-project] a3a60e: [clang-tidy] add new check: modernize-use-scoped-l...

Baranov Victor via All-commits all-commits at lists.llvm.org
Sun Jun 29 12:34:53 PDT 2025


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: a3a60e03e2bf7b79683517584a9a7b3e4c8cd297
      https://github.com/llvm/llvm-project/commit/a3a60e03e2bf7b79683517584a9a7b3e4c8cd297
  Author: Baranov Victor <bar.victor.2002 at gmail.com>
  Date:   2025-06-29 (Sun, 29 Jun 2025)

  Changed paths:
    M clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
    M clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
    A clang-tools-extra/clang-tidy/modernize/UseScopedLockCheck.cpp
    A clang-tools-extra/clang-tidy/modernize/UseScopedLockCheck.h
    M clang-tools-extra/docs/ReleaseNotes.rst
    M clang-tools-extra/docs/clang-tidy/checks/list.rst
    A clang-tools-extra/docs/clang-tidy/checks/modernize/use-scoped-lock.rst
    A clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/mutex
    A clang-tools-extra/test/clang-tidy/checkers/modernize/use-scope-lock-warn-on-using-and-typedef-false.cpp
    A clang-tools-extra/test/clang-tidy/checkers/modernize/use-scoped-lock-warn-on-single-locks-false.cpp
    A clang-tools-extra/test/clang-tidy/checkers/modernize/use-scoped-lock.cpp

  Log Message:
  -----------
  [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.



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list