Skip to content

Commit 73a8974

Browse files
TylerMSFTTylerMSFT
authored andcommitted
draft new linter topics
1 parent 71dbeed commit 73a8974

File tree

5 files changed

+120
-51
lines changed

5 files changed

+120
-51
lines changed

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

Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,71 @@
11
---
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"
88
---
9-
# `lnt-int-naming-convention`
9+
# `int-naming-convention`
1010

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.
1212

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).
1414

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).
1616

17-
## Examples
17+
## Example
18+
19+
The For example, an `editorconfig` file that contains:
1820

1921
```cpp
20-
#include <string>
21-
#include <vector>
22+
class MyClass
23+
{
2224

23-
std::string& return_by_ref();
25+
public:
2426

25-
int& return_int_by_ref();
27+
int getValue() { return value; } // Flagged: ‘getValue’ doesn't modify the object's state.
2628

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.
3130

32-
auto s = return_by_ref(); // Flagged: the function returns by-reference
33-
// but a copy is made to initialize 's'.
31+
private:
3432

35-
auto i = return_int_by_ref(); // Not flagged because no copy constructor is called.
36-
}
37-
```
33+
int value = 42;
3834

39-
## How to fix the issue
35+
};
36+
``````
4037

41-
The fix the linter suggests is to change `auto` to `auto&` on the declaration.
38+
flags the following code:
4239

4340
```cpp
44-
#include <string>
45-
46-
std::string& return_by_ref();
47-
48-
void accidental_copy(std::vector<std::string>& strings)
41+
void example()
4942
{
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
5344
}
5445
```
5546

56-
## Remarks
47+
## How to fix the issue
5748

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:
5950

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+
```
6157

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**:
6359

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:::
6663

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.
7567

7668
## See also
7769

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)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
title: make-member-function-const
3+
description: "Reference for Visual Studio C++ IntelliSense Linter check make-member-function-const."
4+
ms.date: 09/27/2023
5+
f1_keywords: ["make-member-function-const"]
6+
helpviewer_keywords: ["make-member-function-const"]
7+
monikerRange: ">=msvc-170"
8+
---
9+
# `make-member-function-const`
10+
11+
When member functions don’t modify their object state, mark them as const. This guidance comes from the [C++ Core Guideline Con.2](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#con2-by-default-make-member-functions-const).
12+
13+
## Example
14+
15+
```cpp
16+
class MyClass
17+
{
18+
public:
19+
20+
int getValue() { return value; } // Flagged: ‘getValue’ doesn't modify the object's state.
21+
22+
void setValue(int newValue) { value = newValue; } // OK: ‘setValue’ modifies the object's state.
23+
24+
private:
25+
26+
int value = 42;
27+
28+
};
29+
30+
double getRadius()
31+
{ // Flagged: ‘getRadius’ doesn't modify the object's state.
32+
return radius;
33+
}
34+
```
35+
36+
## How to fix the issue
37+
38+
The solution proposed by the linter is to mark member functions `const` when they do not modify the object's state. This provides a clear indication to both developers and the compiler that the function is safe to call on `const` objects. In the following example, `const` has been added to `getValue()` and `getRadius()`:
39+
40+
```cpp
41+
class MyClass
42+
{
43+
public:
44+
45+
int getValue() const { return value; } // Added const
46+
void setValue(int newValue) { value = newValue; } // OK: ‘setValue’ modifies the object's state.
47+
48+
private:
49+
50+
int value = 42;
51+
52+
};
53+
54+
double getRadius() const // added const
55+
{ // ‘getRadius’ doesn't modify the object's state.
56+
return radius;
57+
}
58+
```
59+
60+
The editor can make this change for you. Place the cursor on the flagged symbol, and choose **Show potential fixes** and then **Make member const**:
61+
62+
:::image type="content" source="media/make-member-function-const.png" :::
63+
The cursor is on the line int getValue() and **Show potential fixes** appeared and was chosen. Now **Make member const** is visible and it shows the get value function with const added to it. You can now choose **Make member const** to change make the change.
64+
:::image-end:::
65+
66+
## Remarks
67+
68+
This check focuses on `const` usage for member functions in C++ code. The C++ Core Guidelines recommends marking member functions as `const` when they do not modify the object's state.
69+
70+
The current implementation of this check allows for the assignment of `const` to member functions after their declaration. However, it is a good practice to declare member functions as `const` from the beginning if they do not modify the object's state.
71+
72+
## See also
73+
74+
[IntelliSense code linter for C++ overview](cpp-linter-overview.md)
Loading
30.4 KB
Loading

docs/ide/toc.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ 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
33+
- name: lnt-int-naming-convention
34+
href: ../ide/lnt-int-naming-convention.md
3535
- name: lnt-uninitialized-local
3636
href: ../ide/lnt-uninitialized-local.md
37+
- name: lnt-make-member-function-const
38+
href: ../ide/lnt-make-member-function-const.md
3739
- name: Change signature
3840
href: ../ide/refactoring/change-signature.md
3941
- name: Convert to raw string literal

0 commit comments

Comments
 (0)