You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[CP.44](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#cp44-remember-to-name-your-lock_guards-and-unique_locks): Remember to name your lock_guards and unique_locks
15
14
16
-
The standard library provides a few useful classes that help to control concurrent access to resources. Objects of such types lock exclusive access during their lifetime. Lifetime management implies that every lock object must be named. That is, it must have a clearly defined lifetime that spans through the period in which access operations are executed. So, failing to assign a lock object to a variable is a mistake that effectively disables the locking mechanism (because temporary variables are transient). This rule tries to catch simple cases of such unintended behavior.
17
-
18
15
## Remarks
19
16
20
-
- Only standard lock types are tracked: `std::scoped_lock`, `std::unique_lock`, and `std::lock_quard`.
21
-
22
-
- Only simple calls to constructors are analyzed. More complex initializer expressions may lead to inaccurate results, but it's an unusual scenario.
17
+
The standard library provides locks to help control concurrent access to resources during their lifetime. Attempting to declare a lock object without a name, creates a temporary object that is immediately destructed rather than living to the end of the enclosing scope. So, failing to assign a lock object to a variable is a mistake that effectively disables the locking mechanism (because temporary variables are transient). This rule tries to catch simple cases of such unintended behavior.
23
18
24
-
- Locks passed as arguments to function calls or returned as results of function calls are ignored.
19
+
This diagnostic will only fire for the standard lock types `std::scoped_lock`, `std::unique_lock`, and `std::lock_guard`. Other unnamed RAII types are covered by [C26444](c26444.md).
25
20
26
-
- Locks created as temporaries but assigned to named references to extend their lifetime are ignored.
21
+
Only simple calls to constructors are analyzed. More complex initializer expressions may lead to inaccurate results, but it's an unusual scenario. Locks passed as arguments to function calls or returned as results of function calls are ignored. To provide similar protection for types returned by a function call, annotate with `[[nodiscard]]`. Locks created as temporaries but assigned to named references to extend their lifetime are ignored.
27
22
28
23
Code analysis name: `NO_UNNAMED_GUARDS`
29
24
30
25
## Example
31
26
32
-
Missing scoped variable:
27
+
This example is missing the name of the scoped lock.
0 commit comments