https://github.com/localspook updated 
https://github.com/llvm/llvm-project/pull/165127

>From 25c6938282df50df7749b822be0458009dc31e4a Mon Sep 17 00:00:00 2001
From: Victor Chernyakin <[email protected]>
Date: Sat, 25 Oct 2025 18:48:13 -0700
Subject: [PATCH] [clang-tidy] Fix `modernize-use-scoped-lock` crash on
 malformed code

---
 .../clang-tidy/modernize/UseScopedLockCheck.cpp          | 3 ++-
 clang-tools-extra/docs/ReleaseNotes.rst                  | 5 +++++
 .../checkers/modernize/use-scoped-lock-no-crash.cpp      | 9 +++++++++
 3 files changed, 16 insertions(+), 1 deletion(-)
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/modernize/use-scoped-lock-no-crash.cpp

diff --git a/clang-tools-extra/clang-tidy/modernize/UseScopedLockCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/UseScopedLockCheck.cpp
index aa1ee6db8917a..5c03c1a3aae9e 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseScopedLockCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseScopedLockCheck.cpp
@@ -217,7 +217,8 @@ void UseScopedLockCheck::diagOnSingleLock(
 
   // Create Fix-its only if we can find the constructor call to properly handle
   // 'std::lock_guard l(m, std::adopt_lock)' case.
-  const auto *CtorCall = dyn_cast<CXXConstructExpr>(LockGuard->getInit());
+  const auto *CtorCall =
+      dyn_cast_if_present<CXXConstructExpr>(LockGuard->getInit());
   if (!CtorCall)
     return;
 
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 33cc401bcb78f..e8cfbf8ec594c 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -364,6 +364,11 @@ Changes in existing checks
   on Windows when the check was enabled with a 32-bit :program:`clang-tidy`
   binary.
 
+- Improved :doc:`modernize-use-scoped-lock
+  <clang-tidy/checks/modernize/use-scoped-lock>` check by fixing a crash
+  on malformed code (common when using :program:`clang-tidy` through
+  :program:`clangd`).
+
 - Improved :doc:`modernize-use-std-format
   <clang-tidy/checks/modernize/use-std-format>` check to correctly match
   when the format string is converted to a different type by an implicit
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-scoped-lock-no-crash.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-scoped-lock-no-crash.cpp
new file mode 100644
index 0000000000000..587dbe2707873
--- /dev/null
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-scoped-lock-no-crash.cpp
@@ -0,0 +1,9 @@
+// RUN: %check_clang_tidy -std=c++17-or-later -expect-clang-tidy-error %s 
modernize-use-scoped-lock %t -- -- -isystem %clang_tidy_headers
+
+#include <mutex>
+
+void f() {
+  std::lock_guard<std::mutex> dont_crash {some_nonexistant_variable};
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use 'std::scoped_lock' instead 
of 'std::lock_guard' [modernize-use-scoped-lock]
+  // CHECK-MESSAGES: :[[@LINE-2]]:43: error: use of undeclared identifier 
'some_nonexistant_variable' [clang-diagnostic-error]
+}

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to