Skip to content

Commit 78aa1e1

Browse files
committed
more diagnostic updates
1 parent ce55874 commit 78aa1e1

1 file changed

Lines changed: 13 additions & 15 deletions

File tree

docs/code-quality/c26441.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ title: Warning C26441
44
ms.date: 11/15/2017
55
f1_keywords: ["C26441", "NO_UNNAMED_GUARDS"]
66
helpviewer_keywords: ["C26441"]
7-
ms.assetid: f923c422-ed01-4644-b40b-93f15fc5bb93
87
---
98
# Warning C26441
109

@@ -13,48 +12,47 @@ ms.assetid: f923c422-ed01-4644-b40b-93f15fc5bb93
1312
**C++ Core Guidelines**:
1413
[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
1514

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-
1815
## Remarks
1916

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.
2318

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).
2520

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.
2722

2823
Code analysis name: `NO_UNNAMED_GUARDS`
2924

3025
## Example
3126

32-
Missing scoped variable:
27+
This example is missing the name of the scoped lock.
3328

3429
```cpp
35-
void print_diagnostic(gsl::string_span<> text)
30+
void print_diagnostic(std::string_view text)
3631
{
3732
auto stream = get_diagnostic_stream();
3833
if (stream)
3934
{
40-
std::lock_guard<std::mutex>{ diagnostic_mutex_ }; // C26441
35+
std::lock_guard<std::mutex>{ diagnostic_mutex_ };
4136
write_line(stream, text);
42-
// ...
4337
}
4438
}
4539
```
4640
47-
Missing scoped variable, corrected:
41+
To fix the error, we give a name to the lock, thus extending its lifetime.
4842
4943
```cpp
50-
void print_diagnostic(gsl::string_span<> text)
44+
void print_diagnostic(std::string_view text)
5145
{
5246
auto stream = get_diagnostic_stream();
5347
if (stream)
5448
{
5549
std::lock_guard<std::mutex> lock{ diagnostic_mutex_ };
5650
write_line(stream, text);
57-
// ...
5851
}
5952
}
6053
```
54+
55+
## See also
56+
57+
[C26444](C26444.md)
58+
<!-- currently undocumented C4858 -->

0 commit comments

Comments
 (0)