Skip to content

Commit 71dbeed

Browse files
TylerMSFTTylerMSFT
authored andcommitted
draft
1 parent 5038920 commit 71dbeed

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

docs/ide/lnt-arithmetic-overflow.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ The `lnt-arithmetic-overflow` check is controlled by the **Arithmetic Overflow**
1717
## Examples
1818

1919
```cpp
20+
#include <cstdint>
21+
2022
void overflow(int a, int b) {
2123
int64_t mul = a * b; // Flagged: 32-bit operation may overflow.
2224
int64_t shift = a << 34; // Flagged: Shift would overflow.

docs/ide/lnt-int-naming-convention.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
title: lnt-int-naming-convention
3+
description: "Reference for Visual Studio C++ IntelliSense Linter check lnt-int-naming-convention."
4+
ms.date: 09/29/2021
5+
f1_keywords: ["lnt-int-naming-convention"]
6+
helpviewer_keywords: ["lnt-int-naming-convention"]
7+
monikerRange: ">=msvc-160"
8+
---
9+
# `lnt-int-naming-convention`
10+
11+
A copy is being made because `auto` doesn't deduce references.
12+
13+
Variables declared by using `auto` are never deduced to be type references. If you initialize an `auto` variable from the result of a function that returns by reference, it results in a copy. Sometimes this effect is desirable, but in many cases it causes an unintentional copy.
14+
15+
The `lnt-int-naming-convention` check is controlled by the **Accidental Copy** setting in the C/C++ Code Style options. For information on how to change this setting, see [Configure the linter](cpp-linter-overview.md#configure-the-linter).
16+
17+
## Examples
18+
19+
```cpp
20+
#include <string>
21+
#include <vector>
22+
23+
std::string& return_by_ref();
24+
25+
int& return_int_by_ref();
26+
27+
void accidental_copy(std::vector<std::string>& strings)
28+
{
29+
for (auto s : strings) {} // Flagged: A new copy of each string is
30+
// made when the vector is iterated.
31+
32+
auto s = return_by_ref(); // Flagged: the function returns by-reference
33+
// but a copy is made to initialize 's'.
34+
35+
auto i = return_int_by_ref(); // Not flagged because no copy constructor is called.
36+
}
37+
```
38+
39+
## How to fix the issue
40+
41+
The fix the linter suggests is to change `auto` to `auto&` on the declaration.
42+
43+
```cpp
44+
#include <string>
45+
46+
std::string& return_by_ref();
47+
48+
void accidental_copy(std::vector<std::string>& strings)
49+
{
50+
for (auto& s : strings) {}
51+
52+
auto& s = return_by_ref();
53+
}
54+
```
55+
56+
## Remarks
57+
58+
The suggested fix isn't safe to apply in all cases. The fix may cause a compilation error or change the behavior of the code. It's important to understand how the suggested fix affects the code before applying it.
59+
60+
In cases where a temporary is returned, `const auto&` is necessary to prevent a compilation error. In this case, it may be preferable to continue to use `auto`.
61+
62+
Sometimes a copy is intentional, such as when you want to modify the copy without affecting the source instance, as shown in this example.
63+
64+
```cpp
65+
void modifies_string(std::string& s);
66+
67+
void example(std::vector<std::string>& strings)
68+
{
69+
for (auto s : strings) {
70+
modifies_string(s); // In this case, the copy may be intended so that
71+
// the original strings are not modified.
72+
}
73+
}
74+
```
75+
76+
## See also
77+
78+
[IntelliSense code linter for C++ overview](cpp-linter-overview.md)

docs/ide/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ items:
3030
href: ../ide/lnt-integer-float-division.md
3131
- name: lnt-logical-bitwise-mismatch
3232
href: ../ide/lnt-logical-bitwise-mismatch.md
33+
- name: lnt-naming-convention
34+
href: ../ide/lnt-naming-convention.md
3335
- name: lnt-uninitialized-local
3436
href: ../ide/lnt-uninitialized-local.md
3537
- name: Change signature

0 commit comments

Comments
 (0)