Skip to content

Commit 25c6938

Browse files
committed
[clang-tidy] Fix modernize-use-scoped-lock crash on malformed code
1 parent a7f1910 commit 25c6938

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

clang-tools-extra/clang-tidy/modernize/UseScopedLockCheck.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ void UseScopedLockCheck::diagOnSingleLock(
217217

218218
// Create Fix-its only if we can find the constructor call to properly handle
219219
// 'std::lock_guard l(m, std::adopt_lock)' case.
220-
const auto *CtorCall = dyn_cast<CXXConstructExpr>(LockGuard->getInit());
220+
const auto *CtorCall =
221+
dyn_cast_if_present<CXXConstructExpr>(LockGuard->getInit());
221222
if (!CtorCall)
222223
return;
223224

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,11 @@ Changes in existing checks
364364
on Windows when the check was enabled with a 32-bit :program:`clang-tidy`
365365
binary.
366366

367+
- Improved :doc:`modernize-use-scoped-lock
368+
<clang-tidy/checks/modernize/use-scoped-lock>` check by fixing a crash
369+
on malformed code (common when using :program:`clang-tidy` through
370+
:program:`clangd`).
371+
367372
- Improved :doc:`modernize-use-std-format
368373
<clang-tidy/checks/modernize/use-std-format>` check to correctly match
369374
when the format string is converted to a different type by an implicit
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %check_clang_tidy -std=c++17-or-later -expect-clang-tidy-error %s modernize-use-scoped-lock %t -- -- -isystem %clang_tidy_headers
2+
3+
#include <mutex>
4+
5+
void f() {
6+
std::lock_guard<std::mutex> dont_crash {some_nonexistant_variable};
7+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use 'std::scoped_lock' instead of 'std::lock_guard' [modernize-use-scoped-lock]
8+
// CHECK-MESSAGES: :[[@LINE-2]]:43: error: use of undeclared identifier 'some_nonexistant_variable' [clang-diagnostic-error]
9+
}

0 commit comments

Comments
 (0)