[PATCH] D44241: Add a check for constructing non-trivial types without assigning to a variable

Paul Fultz II via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 7 18:04:11 PST 2018


pfultz2 created this revision.
pfultz2 added reviewers: clang-tools-extra, aaron.ballman.
pfultz2 added a project: clang-tools-extra.
Herald added a subscriber: mgorny.

Diagnoses when a non-trivial type is not assigned to a variable. This is useful to check for problems like unnamed lock guards.

For example, if you had code written like this:

  int g_i = 0;
  std::mutex g_i_mutex;  // protects g_i
   
  void safe_increment()
  {
    std::lock_guard<std::mutex>{g_i_mutex};
    // The lock is locked and then unlocked before reaching here
    ++g_i;
  }

This will warn that a variable should be created instead:

  int g_i = 0;
  std::mutex g_i_mutex;  // protects g_i
   
  void safe_increment()
  {
    std::lock_guard<std::mutex> lock{g_i_mutex};
    // The lock is locked and then will be unlocked when exiting scope
    ++g_i;
  }


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D44241

Files:
  clang-tidy/misc/CMakeLists.txt
  clang-tidy/misc/MiscTidyModule.cpp
  clang-tidy/misc/OwnerlessResourceCheck.cpp
  clang-tidy/misc/OwnerlessResourceCheck.h
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/misc-ownerless-resource.rst
  test/clang-tidy/misc-ownerless-resource.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D44241.137528.patch
Type: text/x-patch
Size: 7232 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180308/c20fd716/attachment.bin>


More information about the cfe-commits mailing list