|
| 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) |
0 commit comments