[PATCH] D95725: clang-extra: fix incorrect use of std::lock_guard by adding variable name (identified by MSVC [[nodiscard]] error)

Conrad Poelman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 29 20:31:28 PST 2021


poelmanc created this revision.
poelmanc added reviewers: sammccall, ilya-biryukov.
poelmanc added a project: clang-tools-extra.
Herald added subscribers: usaxena95, kadircet, arphaman.
poelmanc requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

`std::lock_guard` is an RAII class that needs a variable name whose scope determines the guard's lifetime. This particular usage lacked a variable name, meaning the guard could be destroyed before the line that it was indented to protect.

This line was identified by building clang with the latest MSVC preview release, which declares the std::lock_guard constructor to be `[[nodiscard]]` to draw attention to such issues.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D95725

Files:
  clang-tools-extra/clangd/support/Function.h


Index: clang-tools-extra/clangd/support/Function.h
===================================================================
--- clang-tools-extra/clangd/support/Function.h
+++ clang-tools-extra/clangd/support/Function.h
@@ -51,7 +51,7 @@
     Subscription &operator=(Subscription &&Other) {
       // If *this is active, unsubscribe.
       if (Parent) {
-        std::lock_guard<std::recursive_mutex>(Parent->ListenersMu);
+        std::lock_guard<std::recursive_mutex> Guard(Parent->ListenersMu);
         llvm::erase_if(Parent->Listeners,
                        [&](const std::pair<Listener, unsigned> &P) {
                          return P.second == ListenerID;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D95725.320273.patch
Type: text/x-patch
Size: 664 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210130/a434533e/attachment.bin>


More information about the cfe-commits mailing list