Skip to content

Commit 5f325f4

Browse files
authored
Merge pull request #4916 from Xazax-hun/cpp_core_check_updates_1
First batch of doc updates for CppCoreCheck
2 parents d2634ca + b84afde commit 5f325f4

File tree

15 files changed

+177
-174
lines changed

15 files changed

+177
-174
lines changed

docs/code-quality/c26441.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
2-
description: "Learn more about: Warning C26441 NO_UNNAMED_GUARDS"
32
title: Warning C26441
4-
ms.date: 2/7/2023
3+
description: "Learn more about: Warning C26441 NO_UNNAMED_GUARDS."
4+
ms.date: 5/11/2023
55
f1_keywords: ["C26441", "NO_UNNAMED_GUARDS"]
66
helpviewer_keywords: ["C26441"]
77
---
@@ -19,13 +19,23 @@ The standard library provides locks to help control concurrent access to resourc
1919

2020
This diagnostic only analyzes the standard lock types `std::scoped_lock`, `std::unique_lock`, and `std::lock_guard`. Warning [C26444](c26444.md) covers other unnamed RAII types.
2121

22-
The analyzer only analyzes simple calls to constructors. More complex initializer expressions may lead to inaccurate results in the form of missed warnings. Locks passed as arguments to function calls or returned as results of function calls are ignored because the analysis tool is unable to determine if those locks are deliberately trying to protect that function call or if they should be extended. To provide similar protection for types returned by a function call, annotate them with `[[nodiscard]]`. The analyzer ignores locks created as temporaries but assigned to named references to extend their lifetime.
22+
The analyzer only analyzes simple calls to constructors. More complex initializer expressions may lead to inaccurate results in the form of missed warnings. The analyzer ignores locks passed as arguments to function calls or returned from function calls. It's unable to determine if those locks are deliberately trying to protect that function call or if their [lifetime should be extended](https://abseil.io/tips/107). To provide similar protection for types returned by a function call, annotate them with `[[nodiscard]]`. You can also annotate constructors with `[[nodiscard]]` to avoid unnamed objects of that type:
23+
24+
```cpp
25+
struct X { [[nodiscard]] X(); };
26+
27+
void f() {
28+
X{}; // warning C4834
29+
}
30+
```
31+
32+
The analyzer ignores locks created as temporaries but assigned to named references to extend their lifetime.
2333
2434
Code analysis name: `NO_UNNAMED_GUARDS`
2535
2636
## Example
2737
28-
In this example the name of the scoped lock is missing.
38+
In this example, the name of the scoped lock is missing.
2939
3040
```cpp
3141
void print_diagnostic(std::string_view text)

docs/code-quality/c26444.md

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,49 @@
11
---
2-
description: "Learn more about: Warning C26444 NO_UNNAMED_RAII_OBJECTS"
32
title: Warning C26444
4-
ms.date: 01/18/2017
3+
description: "Learn more about: Warning C26444 NO_UNNAMED_RAII_OBJECTS."
4+
ms.date: 05/11/2023
55
f1_keywords: ["C26444", "NO_UNNAMED_RAII_OBJECTS"]
66
helpviewer_keywords: ["C26444"]
77
---
88
# Warning C26444
99

10-
> Avoid unnamed objects with custom construction and destruction.
10+
> Don't try to declare a local variable with no name (es.84).
1111
1212
## C++ Core Guidelines
1313

14-
[ES.84: Don't (try to) declare a local variable with no name](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Res-noname)
14+
[ES.84: Don't (try to) declare a local variable with no name](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-noname)
1515

16-
Unnamed (that is, temporary) objects with non-trivial behavior may point to either (a) inefficient code that allocates and immediately throws away resources or (b) to the code that unintentionally ignores non-primitive data. Sometimes it may also indicate plainly wrong declaration.
16+
An unnamed variable declaration creates a temporary object that is discarded at the end of the statement. Such temporary objects with nontrivial behavior may point to either inefficient code that allocates and immediately throws away resources or to the code that unintentionally ignores nonprimitive data. Sometimes it may also indicate plainly wrong declaration.
1717

18-
## Notes
18+
## Remarks
1919

20-
- This rule detects types with non-deleted destructors. Keep in mind that destructors can be compiler generated.
21-
- The warning can flag code that isn't compiler generated and that invokes either a non-default constructor of a RAII type or a function that returns an object of such type. This warning helps to detect ignored call results in addition to wrong declarations.
20+
- This rule detects types with a hand-written destructor or a compiler-generated destructor that transitively calls a hand-written destructor.
21+
- This rule can flag code that invokes a nontrivial constructor of a RAII type.
2222
- The logic skips temporaries if they're used in higher-level expressions. One example is temporaries that are passed as arguments or used to invoke a function.
23-
- The standard library implementation may have different versions of destruction logic for some types (for example, containers). This can produce noisy warnings on debug builds because it's customary to ignore iterators returned from calls like [`std::vector::insert`](../standard-library/vector-class.md#insert). While such warnings aren't actionable in most cases, they're legitimate in pointing to the place where some non-obvious work is done in temporary objects.
2423

2524
Code analysis name: `NO_UNNAMED_RAII_OBJECTS`
2625

27-
## Example: Ignored call result
26+
## Examples
2827

2928
```cpp
30-
std::string ToTraceMessage(State &state);
31-
void SaveState(State &state)
29+
struct A { A(int i); ~A(); };
30+
void Foo()
3231
{
33-
// ...
34-
ToTraceMessage(state); // C26444, should we do something with the result of this call?
32+
A{42}; // warning C26444: Don't try to declare a local variable with no name (es.84).
3533
}
34+
```
3635
37-
Example: Ignored call result - fixed.
38-
std::cerr << ToTraceMessage(state);
36+
To fix the issue, convert the temporary object to a local.
3937
40-
Example: Unexpected lifetime.
41-
void SplitCache()
38+
```cpp
39+
struct A { A(int i); ~A(); };
40+
void Foo()
4241
{
43-
gsl::finally([] { RestoreCache(); }); // C26444, RestoreCache is invoked immediately!
44-
//...
42+
A guard{42}; // OK.
4543
}
46-
47-
Example: Unexpected lifetime - fixed.
48-
const auto _ = gsl::finally([] { RestoreCache(); });
4944
```
5045

5146
## See also
5247

53-
[ES.84: Don't (try to) declare a local variable with no name](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md)
48+
[C26441](C26441.md)\
49+
[ES.84: Don't (try to) declare a local variable with no name](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-noname)

docs/code-quality/c26449.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
---
2-
description: "Learn more about: Warning C26449 NO_SPAN_FROM_TEMPORARY"
32
title: Warning C26449
4-
ms.date: 03/22/2018
3+
description: "Learn more about: Warning C26449 NO_SPAN_FROM_TEMPORARY."
4+
ms.date: 05/11/2023
55
f1_keywords: ["C26449", "NO_SPAN_FROM_TEMPORARY"]
66
helpviewer_keywords: ["C26449"]
77
---
88
# Warning C26449
99

1010
> `gsl::span` or `std::string_view` created from a temporary will be invalid when the temporary is invalidated (gsl.view)
1111
12-
C++ Core Guidelines: [GSL.view: Views](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#gslview-views).
12+
C++ Core Guidelines: [GSL.view: Views](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#SS-views).
1313

14-
Spans and views are convenient and lightweight types that allow you to reference memory buffers. But they must be used carefully: while their interface looks similar to standard containers, their behavior is more like the behavior of pointers and references. They don't own data and must never be constructed from temporary buffers. This check focuses on cases where source data is temporary, while a span or view isn't. There's another check that handles a slightly different scenario involving span references: [C26445 NO_SPAN_REF](c26445.md). Both rules can help to avoid subtle but dangerous mistakes made when legacy code gets modernized and adopts spans or views.
14+
Spans and views are convenient and lightweight types that allow you to reference memory buffers. But they must be used carefully: while their interface looks similar to standard containers, their behavior is more like the behavior of pointers and references. They don't own data and must never be constructed from temporary buffers. This check focuses on cases where source data is temporary, while a span or view isn't. This rule can help to avoid subtle but dangerous mistakes made when legacy code gets modernized and adopts spans or views. There's another check that handles a slightly different scenario involving span references: [C26445 NO_SPAN_REF](c26445.md).
15+
16+
Consider using [C26815](c26815.md) and [C26816](c26816.md). Those warnings are more general versions of this warning.
1517

1618
## Remarks
1719

@@ -38,7 +40,7 @@ Subtle difference in result types:
3840
gsl::span<const sequence_item> get_seed_sequence() noexcept;
3941

4042
// Returns a generated collection. Doesn't own new data.
41-
const std::vector<sequence_item> get_next_sequence(gsl::span<const sequence_item>);
43+
std::vector<sequence_item> get_next_sequence(gsl::span<const sequence_item>);
4244

4345
void run_batch()
4446
{
@@ -50,3 +52,9 @@ void run_batch()
5052
}
5153
}
5254
```
55+
56+
To fix the issue, make sure the view is created from an object that lives at least as long as the view itself. Sometimes a solution can be achieved by copying the data, other times some APIs need to be redesigned to share a reference to an object that lives long enough instead of returning a temporary copy.
57+
58+
## See also
59+
[C26815](c26815.md)\
60+
[C26816](c26816.md)

docs/code-quality/c26450.md

Lines changed: 11 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
---
2-
description: "Learn more about: Arithmetic overflow: '%operator%' operation causes overflow at compile time. Use a wider type to store the operands"
32
title: Warning C26450
4-
ms.date: 01/08/2017
3+
description: "Describes the causes of MSVC code analysis warning C26450 and how to fix it."
4+
ms.date: 05/11/2023
55
f1_keywords: ["C26450", "RESULT_OF_ARITHMETIC_OPERATION_PROVABLY_LOSSY"]
66
helpviewer_keywords: ["C26450"]
77
---
88
# Warning C26450
99

10-
>Arithmetic overflow: '*operator*' operation causes overflow at compile time. Use a wider type to store the operands (io.1)
10+
> Arithmetic overflow: '*operator*' operation causes overflow at compile time. Use a wider type to store the operands (io.1)
1111
1212
## Remarks
1313

1414
This warning indicates that an arithmetic operation was provably lossy at compile time. It can be asserted when the operands are all compile-time constants. Currently, we check left shift, multiplication, addition, and subtraction operations for such overflows.
1515

16-
Warning C4307 is a similar check in the Microsoft C++ compiler.
16+
Warning [C4307](../error-messages/compiler-warnings/compiler-warning-level-2-c4307.md) is a similar check in the Microsoft C++ compiler.
1717

1818
Code analysis name: `RESULT_OF_ARITHMETIC_OPERATION_PROVABLY_LOSSY`
1919

20-
## Example 1
20+
## Examples
2121

2222
```cpp
2323
int multiply()
@@ -36,59 +36,15 @@ long long multiply()
3636
{
3737
const int a = INT_MAX;
3838
const int b = 2;
39-
long long c = (long long)a * b; // OK
40-
return c;
41-
}
42-
```
43-
44-
## Example 2
45-
46-
```cpp
47-
int add()
48-
{
49-
const int a = INT_MAX;
50-
const int b = 2;
51-
int c = a + b; // C26450 reported here
52-
return c;
53-
}
54-
```
55-
56-
To correct this warning, use the following code:
57-
58-
```cpp
59-
long long add()
60-
{
61-
const int a = INT_MAX;
62-
const int b = 2;
63-
long long c = (long long)a + b; // OK
64-
return c;
65-
}
66-
```
67-
68-
## Example 3
69-
70-
```cpp
71-
int subtract()
72-
{
73-
const int a = -INT_MAX;
74-
const int b = 2;
75-
int c = a - b; // C26450 reported here
76-
return c;
77-
}
78-
```
79-
80-
To correct this warning, use the following code.
81-
82-
```cpp
83-
long long subtract()
84-
{
85-
const int a = -INT_MAX;
86-
const int b = 2;
87-
long long c = (long long)a - b; // OK
39+
long long c = static_cast<long long>(a) * b; // OK
8840
return c;
8941
}
9042
```
9143

9244
## See also
9345

94-
[ES.103: Don't overflow](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Res-overflow)
46+
[26451](c26451.md)\
47+
[26452](c26452.md)\
48+
[26453](c26453.md)\
49+
[26454](c26454.md)\
50+
[ES.103: Don't overflow](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-overflow)

docs/code-quality/c26451.md

Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Warning C26451
33
description: "Describes the causes of MSVC code analysis warning C26451, and shows how to fix it."
4-
ms.date: 07/15/2020
4+
ms.date: 05/11/2023
55
f1_keywords: ["C26451", "RESULT_OF_ARITHMETIC_OPERATION_CAST_TO_LARGER_SIZE"]
66
helpviewer_keywords: ["C26451"]
77
---
@@ -17,14 +17,14 @@ Code analysis detects when an integral value gets shifted left, multiplied, adde
1717

1818
Code analysis name: `RESULT_OF_ARITHMETIC_OPERATION_CAST_TO_LARGER_SIZE`
1919

20-
## Example 1
20+
## Examples
2121

2222
The following code generates this warning:
2323

2424
```cpp
2525
void leftshift(int i) noexcept
2626
{
27-
unsigned __int64 x;
27+
unsigned long long x;
2828
x = i << 31; // C26451 reported here
2929

3030
// code
@@ -36,55 +36,17 @@ To correct this warning, use the following code:
3636
```cpp
3737
void leftshift(int i) noexcept
3838
{
39-
unsigned __int64 x;
40-
x = static_cast<unsigned __int64>(i) << 31; // OK
39+
unsigned long long x;
40+
x = static_cast<unsigned long long>(i) << 31; // OK
4141
4242
// code
4343
}
4444
```
4545

46-
## Example 2
47-
48-
```cpp
49-
void somefunc(__int64 /* y */) noexcept
50-
{}
51-
52-
void callsomefunc(int x) noexcept
53-
{
54-
somefunc(x * 2); // C26451 reported here
55-
}
56-
```
57-
58-
To correct this warning, use the following code:
59-
60-
```cpp
61-
void callsomefunc(int x) noexcept
62-
{
63-
somefunc(static_cast<__int64>(x) * 2); // OK
64-
}
65-
```
66-
67-
## Example 3
68-
69-
```cpp
70-
__int64 add(int x) noexcept
71-
{
72-
constexpr auto value = 2;
73-
return x += value; // C26451 reported here
74-
}
75-
```
76-
77-
To correct this warning, use the following code:
78-
79-
```cpp
80-
__int64 add(int x) noexcept
81-
{
82-
constexpr auto value = 2;
83-
const __int64 y = static_cast<__int64>(x) + value; // OK
84-
return y;
85-
}
86-
```
87-
8846
## See also
8947

90-
- [ES.103: Don't overflow](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Res-overflow)
48+
[26450](c26450.md)\
49+
[26452](c26452.md)\
50+
[26453](c26453.md)\
51+
[26454](c26454.md)\
52+
[ES.103: Don't overflow](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Res-overflow)

docs/code-quality/c26452.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ helpviewer_keywords: ["C26452"]
1313

1414
This warning indicates the shift count is negative, or greater than or equal to the number of bits in the shifted operand. Either case results in undefined behavior.
1515

16-
Warning C4293 is a similar check in the Microsoft C++ compiler.
16+
Warning [C4293](../error-messages/compiler-warnings/compiler-warning-level-1-c4293.md) is a similar check in the Microsoft C++ compiler.
1717

1818
Code analysis name: `SHIFT_COUNT_NEGATIVE_OR_TOO_BIG`
1919

2020
## Example
2121

2222
```cpp
23-
unsigned __int64 combine(unsigned lo, unsigned hi)
23+
unsigned long long combine(unsigned lo, unsigned hi)
2424
{
2525
return (hi << 32) | lo; // C26252 here
2626
}
@@ -29,12 +29,17 @@ unsigned __int64 combine(unsigned lo, unsigned hi)
2929
To correct this warning, use the following code:
3030
3131
```cpp
32-
unsigned __int64 combine(unsigned lo, unsigned hi)
32+
unsigned long long combine(unsigned lo, unsigned hi)
3333
{
3434
return (static_cast<unsigned __int64>(hi) << 32) | lo; // OK
3535
}
3636
```
3737

3838
## See also
3939

40-
[ES.102: Use signed types for arithmetic](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Res-unsigned)
40+
[26450](c26450.md)\
41+
[26451](c26451.md)\
42+
[26453](c26453.md)\
43+
[26454](c26454.md)\
44+
[ES.101: Use unsigned types for bit manipulation](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-unsigned)\
45+
[ES.102: Use signed types for arithmetic](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-signed)

0 commit comments

Comments
 (0)