|
1 | 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" |
| 2 | +title: int-naming-convention |
| 3 | +description: "Reference for Visual Studio C++ IntelliSense Linter check int-naming-convention." |
| 4 | +ms.date: 09/27/2023 |
| 5 | +f1_keywords: ["int-naming-convention"] |
| 6 | +helpviewer_keywords: ["int-naming-convention"] |
| 7 | +monikerRange: ">=msvc-170" |
8 | 8 | ---
|
9 |
| -# `lnt-int-naming-convention` |
| 9 | +# `int-naming-convention` |
10 | 10 |
|
11 |
| -A copy is being made because `auto` doesn't deduce references. |
| 11 | +Ensure that the naming convention for symbols aligns with the coding style specified in the project's `.editorconfig` file. |
12 | 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. |
| 13 | +To enable this feature, add an `.editorconfig` file in the same directory as your project file. The `.editorconfig` specifies the naming conventions for symbols in your project. For an example `.editorconfig` that specifies the naming conventions for Unreal Engine projects, see [`.editorconfig` on ](https://raw.githubusercontent.com/microsoft/vc-ue-extensions/main/Source/.editorconfig). |
14 | 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). |
| 15 | +Once you have the `.editorconfig` file in your project, turn on the `int-naming-convention` check with the **Naming Convention** setting in the C/C++ Code Style options. For information about how to change this setting, see [Configure the linter](cpp-linter-overview.md#configure-the-linter). |
16 | 16 |
|
17 |
| -## Examples |
| 17 | +## Example |
| 18 | + |
| 19 | +The For example, an `editorconfig` file that contains: |
18 | 20 |
|
19 | 21 | ```cpp
|
20 |
| -#include <string> |
21 |
| -#include <vector> |
| 22 | +class MyClass |
| 23 | +{ |
22 | 24 |
|
23 |
| -std::string& return_by_ref(); |
| 25 | +public: |
24 | 26 |
|
25 |
| -int& return_int_by_ref(); |
| 27 | + int getValue() { return value; } // Flagged: ‘getValue’ doesn't modify the object's state. |
26 | 28 |
|
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. |
| 29 | + void setValue(int newValue) { value = newValue; } // OK: ‘setValue’ modifies the object's state. |
31 | 30 |
|
32 |
| - auto s = return_by_ref(); // Flagged: the function returns by-reference |
33 |
| - // but a copy is made to initialize 's'. |
| 31 | +private: |
34 | 32 |
|
35 |
| - auto i = return_int_by_ref(); // Not flagged because no copy constructor is called. |
36 |
| -} |
37 |
| -``` |
| 33 | + int value = 42; |
38 | 34 |
|
39 |
| -## How to fix the issue |
| 35 | +}; |
| 36 | +`````` |
40 | 37 |
|
41 |
| -The fix the linter suggests is to change `auto` to `auto&` on the declaration. |
| 38 | +flags the following code: |
42 | 39 |
|
43 | 40 | ```cpp
|
44 |
| -#include <string> |
45 |
| -
|
46 |
| -std::string& return_by_ref(); |
47 |
| -
|
48 |
| -void accidental_copy(std::vector<std::string>& strings) |
| 41 | +void example() |
49 | 42 | {
|
50 |
| - for (auto& s : strings) {} |
51 |
| -
|
52 |
| - auto& s = return_by_ref(); |
| 43 | + bool myFlag = true; // flagged because it doesn't follow the naming convention specified in the .editorconfig |
53 | 44 | }
|
54 | 45 | ```
|
55 | 46 |
|
56 |
| -## Remarks |
| 47 | +## How to fix the issue |
57 | 48 |
|
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. |
| 49 | +Change the naming based on the code style specified in the `.editorconfig`. For example: |
59 | 50 |
|
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`. |
| 51 | +```cpp |
| 52 | +void example() |
| 53 | +{ |
| 54 | + bool bMyFlag = true; // fixed to follow the code style specified in the .editorconfig |
| 55 | +} |
| 56 | +``` |
61 | 57 |
|
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. |
| 58 | +The editor can make the change for you. Place the cursor on the flagged symbol, and choose **Show potential fixes** and then **Apply naming convention**: |
63 | 59 |
|
64 |
| -```cpp |
65 |
| -void modifies_string(std::string& s); |
| 60 | +:::image type="content" source="media/int-naming-convention-apply-naming-convention.png" ::: |
| 61 | +The code editor shows bool myFlag = true. With the cursor on that line of code, **Show potential fixes** appeared and was chosen. Now **Apply naming convention** is visible and it shows bool my Flag = true in red and the suggested change, bool b My Flag, in green. You can now choose **Apply naming convention** to change the flagged code to bool b My Flag = true. |
| 62 | +:::image-end::: |
66 | 63 |
|
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 |
| -``` |
| 64 | +## Remarks |
| 65 | + |
| 66 | +The `int-naming-convention` linter check ensures that naming conventions align with naming conventions specified in the `editorconfig` file. This check check can be applied to any project that has an `editorconfig` file, and can customize your `.editorconfig` file to suit your project's coding style. |
75 | 67 |
|
76 | 68 | ## See also
|
77 | 69 |
|
78 |
| -[IntelliSense code linter for C++ overview](cpp-linter-overview.md) |
| 70 | +[Create portable, custom editor settings with EditorConfig](/visualstudio/ide/create-portable-custom-editor-options)\ |
| 71 | +[IntelliSense code linter for C++ overview](cpp-linter-overview.md) |
0 commit comments