Skip to content

Commit f94ffad

Browse files
authored
Merge pull request MicrosoftDocs#5055 from Xazax-hun/doc-updates
Various documentation updates
2 parents beb5236 + 17d474d commit f94ffad

File tree

7 files changed

+122
-39
lines changed

7 files changed

+122
-39
lines changed

docs/code-quality/c26478.md

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: "Learn more about: Warning C26478: Don't use std::move on constant variables. (es.56)"
33
title: Warning C26478
4-
ms.date: 07/15/2019
4+
ms.date: 10/12/2023
55
f1_keywords: ["C26478", "NO_MOVE_OP_ON_CONST"]
66
helpviewer_keywords: ["C26478"]
77
---
@@ -13,11 +13,11 @@ helpviewer_keywords: ["C26478"]
1313

1414
This warning is to indicate that the use of `std::move` not consistent with how `std::move` is intended to be used.
1515

16-
When called on a `const` object, `std::move` returns a copy of the object, which is likely not the developer's intent.
16+
Because `const` objects can't be moved, calling `std::move` on them has no effect. This pattern can result in unintended copies.
1717

1818
Code analysis name: `NO_MOVE_OP_ON_CONST`
1919

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

2222
```cpp
2323
struct node
@@ -33,23 +33,8 @@ void foo(const node& n)
3333
}
3434
```
3535
36-
An assignment operator or using the passed in parameter will prevent this warning from being issued and will adequately serve the developer's use case.
37-
38-
## Example 2
39-
40-
```cpp
41-
struct s;
42-
43-
template <typename T>
44-
void bar(T t){};
45-
46-
void foo()
47-
{
48-
const s s1;
49-
bar(std::move(s1)); // C26478 reported here
50-
}
51-
```
36+
To fix the issue, remove the redundant `std::move`.
5237
5338
## See also
5439
55-
[ES.56 - Write std::move() only when you need to explicitly move an object to another scope](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es56-write-stdmove-only-when-you-need-to-explicitly-move-an-object-to-another-scope)
40+
[ES.56 - Write `std::move()` only when you need to explicitly move an object to another scope](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-move)

docs/code-quality/c26479.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
description: "Learn more about: Warning C26479: Don't use std::move to return a local variable. (f.48)"
3+
title: Warning C26479
4+
ms.date: 10/12/2023
5+
f1_keywords: ["C26479", "NO_MOVE_RET_ON_LOCALS"]
6+
helpviewer_keywords: ["C26479"]
7+
---
8+
# Warning C26479
9+
10+
> Don't use std::move to return a local variable. (f.48)
11+
12+
## Remarks
13+
14+
The `return` statement is the last use of a local variable, so the compiler uses move semantics to return it whenever possible.
15+
Adding a `std::move` is redundant in this scenario. Moreover, redundant `std::move`s can prevent copy elision.
16+
17+
Code analysis name: `NO_MOVE_RET_ON_LOCALS`
18+
19+
## Example 1
20+
21+
```cpp
22+
S foo()
23+
{
24+
S local1{};
25+
return std::move(local1); // Warning: C26479
26+
}
27+
```
28+
29+
To fix this issue, remove the redundant `std::move`:
30+
31+
```cpp
32+
S foo()
33+
{
34+
S local1{};
35+
return local1; // No warning
36+
}
37+
```
38+
39+
## See also
40+
41+
[F.48 - Don't return `std::move(local)`](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-return-move-local)
42+
[ES.56 - Write `std::move()` only when you need to explicitly move an object to another scope](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-move)

docs/code-quality/c26817.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
---
22
title: Warning C26817
33
description: "Reference for Microsoft C++ Code Analysis warning C26817 in Visual Studio."
4-
ms.date: 02/24/2020
4+
ms.date: 10/12/2023
55
f1_keywords: ["C26817"]
66
helpviewer_keywords: ["C26817"]
77
---
88
# Warning C26817
99

1010
> Potentially expensive copy of variable *name* in range-for loop. Consider making it a const reference (es.71).
1111
12-
For more information, see [ES.71 notes](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#note-214) in the C++ Core Guidelines.
12+
For more information, see [ES.71 notes](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-for-range) in the C++ Core Guidelines.
1313

1414
## Example
1515

@@ -25,7 +25,7 @@ class MyComplexType {
2525

2626
void expensive_function(std::vector<MyComplexType>& complex_vector_ref)
2727
{
28-
for (auto item: complex_vector_ref)
28+
for (auto item: complex_vector_ref) // Warning: C26817
2929
{
3030
// At each iteration, item gets a copy of the next element
3131
// ...
@@ -38,9 +38,7 @@ void expensive_function(std::vector<MyComplexType>& complex_vector_ref)
3838
}
3939
```
4040
41-
This behavior is fine for scalars (pointers, arithmetic types, and so on), but for larger types, the copying may become expensive.
42-
43-
## Solution
41+
The warning is ignoring some types that are cheap to copy like for scalars (pointers, arithmetic types, and so on).
4442
4543
To fix this issue, if the loop variable isn't mutated anywhere in the loop, make it a const reference:
4644
@@ -67,4 +65,4 @@ void less_expensive_function(std::vector<MyComplexType>& complex_vector_ref)
6765
}
6866
```
6967

70-
The **`const`** keyword makes the loop variable immutable. Use of a non-const reference may cause potentially unwanted side effects in the original container elements. If you need to modify only the local loop variable, the potentially expensive copying is unavoidable.
68+
The **`const`** keyword makes the loop variable immutable. Use of a non-const reference makes it possible to inadvertently use the reference to modify the container's elements. If you need to modify only the local loop variable, the potentially expensive copying is unavoidable.

docs/code-quality/c26820.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
---
22
title: Warning C26820
33
description: "Reference for Microsoft C++ Code Analysis warning C26820 in Visual Studio."
4-
ms.date: 04/07/2020
4+
ms.date: 10/12/2023
55
f1_keywords: ["C26820"]
66
helpviewer_keywords: ["C26820"]
77
---
88
# Warning C26820
99

10-
> Assigning by value when a const-reference would suffice, use const auto& instead (p.9).
10+
> This is a potentially expensive copy operation. Consider using a reference unless a copy is required (p.9)
1111
12-
For more information, see [P.9: Don't waste time or space](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#p9-dont-waste-time-or-space) in the C++ Core Guidelines.
12+
For more information, see [P.9: Don't waste time or space](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rp-waste) in the C++ Core Guidelines.
1313

14-
This check covers non-obvious and easy-to-miss behavior when assigning a reference to a variable marked **`auto`**. The type of the **`auto`** variable is resolved to a value rather than a reference, and an implicit copy is made.
14+
This check covers nonobvious and easy-to-miss behavior when assigning a reference to a variable marked **`auto`**. The type of the **`auto`** variable is resolved to a value rather than a reference, and an implicit copy is made.
1515

1616
## Remarks
1717

docs/code-quality/c6395.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
title: Warning C6395
3+
description: "Describes the Microsoft C/C++ code analysis warning C6395, its causes, and how to address it."
4+
ms.date: 10/12/2023
5+
f1_keywords: ["C6395", "EVAL_ORDER_CHANGE"]
6+
helpviewer_keywords: ["C6395"]
7+
---
8+
# Warning C6395
9+
10+
> %variable% has unsequenced reads and/or writes before C++17; changing the language standard might change the behavior of the code.
11+
12+
## Remarks
13+
14+
C++17 made the evaluation order of certain expressions stricter. MSVC complied, which changed the evaluation order for some expressions. Thus, changing the language version might change the observable behavior of the program. This check diagnoses some of the cases where the meaning of the code changes due to switching language versions.
15+
16+
Code analysis name: `EVAL_ORDER_CHANGE`
17+
18+
## Example
19+
20+
```cpp
21+
void foo(int* a, int i)
22+
{
23+
a[++i] = i; // Warning: 'i' has unsequenced reads and/or writes before C++17; changing the language standard might change the behavior of the code
24+
}
25+
```
26+
27+
To solve this problem, separate the side effects from the rest of the expression to make the evaluation order well defined:
28+
29+
```cpp
30+
void foo(int* a, int i)
31+
{
32+
++i;
33+
a[i] = i; // No warning.
34+
}
35+
```

docs/code-quality/how-to-specify-additional-code-information-by-using-analysis-assume.md

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ helpviewer_keywords:
1010
---
1111
# How to specify additional code information by using `_Analysis_assume_`
1212

13-
You can provide hints to the code analysis tool for C/C++ code that will help the analysis process and reduce warnings. To provide additional information, use the following function macro:
13+
You can provide hints to the code analysis tool for C/C++ code that help the analysis process and reduce warnings. To provide additional information, use the following function macro:
1414

1515
`_Analysis_assume_( expr )`
1616

@@ -27,25 +27,44 @@ The following code uses `_Analysis_assume_` to correct the code analysis warning
2727

2828
```cpp
2929
#include <windows.h>
30-
#include <codeanalysis\sourceannotations.h>
30+
#include <sal.h>
3131

32-
using namespace vc_attributes;
32+
// Requires pc to be null.
33+
void f(_Pre_null_ char* pc);
3334

34-
//requires pc to be null
35-
void f([Pre(Null=Yes)] char* pc);
36-
37-
// calls free and sets ch to null
35+
// Calls free and sets ch to null.
3836
void FreeAndNull(char** ch);
3937

4038
void test()
4139
{
42-
char pc = (char)malloc(5);
40+
char* pc = (char*)malloc(5);
4341
FreeAndNull(&pc);
4442
_Analysis_assume_(pc == NULL);
4543
f(pc);
4644
}
4745
```
4846
47+
`_Analysis_assume_` should be used as a last resort. We should first try to make the contracts of the functions more precise. In this case we could improve the contract of `FreeAndNull` instead of using `_Analysis_assume_`:
48+
49+
```cpp
50+
#include <windows.h>
51+
#include <sal.h>
52+
53+
// Requires pc to be null.
54+
void f(_Pre_null_ char* pc);
55+
56+
// Calls free and sets ch to null.
57+
_At_(*ch, _Post_null_)
58+
void FreeAndNull(char** ch);
59+
60+
void test()
61+
{
62+
char* pc = (char*)malloc(5);
63+
FreeAndNull(&pc);
64+
f(pc);
65+
}
66+
```
67+
4968
## See also
5069

5170
- [__assume](../intrinsics/assume.md)

docs/code-quality/toc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ items:
172172
href: ../code-quality/c26477.md
173173
- name: Warning C26478
174174
href: ../code-quality/c26478.md
175+
- name: Warning C26479
176+
href: ../code-quality/c26479.md
175177
- name: Warning C26481
176178
href: ../code-quality/c26481.md
177179
- name: Warning C26482
@@ -479,6 +481,8 @@ items:
479481
href: ../code-quality/c6389.md
480482
- name: Warning C6390
481483
href: ../code-quality/c6390.md
484+
- name: Warning C6395
485+
href: ../code-quality/c6395.md
482486
- name: Warning C6400
483487
href: ../code-quality/c6400.md
484488
- name: Warning C6401

0 commit comments

Comments
 (0)