Skip to content

Commit 9a572fe

Browse files
authored
Merge pull request #5046 from TylerMSFT/linter
Linter
2 parents a36c4bb + 8d0fa45 commit 9a572fe

File tree

6 files changed

+143
-0
lines changed

6 files changed

+143
-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.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
title: lnt-make-member-function-const
3+
description: "Reference for Visual Studio C++ IntelliSense Linter check lnt-make-member-function-const."
4+
ms.date: 09/28/2023
5+
f1_keywords: ["lnt-make-member-function-const"]
6+
helpviewer_keywords: ["lnt-make-member-function-const"]
7+
monikerRange: ">=msvc-170"
8+
---
9+
# `lnt-make-member-function-const`
10+
11+
When a member function doesn’t modify the object's state, annotate it with the `const` keyword. 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+
The linter flags the following code twice because `getValue()` and `getRadius()` don't modify the object's state:
16+
17+
```cpp
18+
class MyClass
19+
{
20+
public:
21+
22+
int getValue() { return value; } // Flagged: ‘getValue’ doesn't modify the object's state.
23+
void setValue(int newValue) { value = newValue; } // OK: ‘setValue’ modifies the object's state.
24+
25+
private:
26+
27+
int value = 42;
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+
Mark member functions `const` when they don't modify the object's state. This lets readers of the code and the compiler know that the function is safe to call on a `const` object.
39+
40+
In the following example, `const` has been added to `getValue()` and `getRadius()`:
41+
42+
```cpp
43+
class MyClass
44+
{
45+
public:
46+
47+
int getValue() const { return value; } // Added const
48+
void setValue(int newValue) { value = newValue; } // OK: ‘setValue’ modifies the object's state.
49+
50+
private:
51+
52+
int value = 42;
53+
54+
};
55+
56+
double getRadius() const // added const
57+
{ // ‘getRadius’ doesn't modify the object's state.
58+
return radius;
59+
}
60+
```
61+
62+
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**:
63+
64+
:::image type="complex" source="media/lnt-make-member-function-const.png" alt-text="Screenshot of the editor suggesting to make member const." :::
65+
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 make the change.
66+
:::image-end:::
67+
68+
Make this change for all flagged member functions.
69+
70+
## Remarks
71+
72+
Introduced in Visual Studio 2022 17.8, this check focuses on `const` usage for member functions in C++ code. The C++ Core Guidelines recommends marking member functions as `const` when they don't modify the object's state.
73+
74+
The current implementation of this check allows you to add `const` to member functions after their declaration. It's a good practice to declare member functions as `const` from the beginning if they don't modify the object's state.
75+
76+
## See also
77+
78+
[IntelliSense code linter for C++ overview](cpp-linter-overview.md)

docs/ide/lnt-naming-convention.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
title: lnt-naming-convention
3+
description: "Reference for Visual Studio C++ IntelliSense Linter check lnt-naming-convention."
4+
ms.date: 09/28/2023
5+
f1_keywords: ["lnt-naming-convention"]
6+
helpviewer_keywords: ["lnt-naming-convention"]
7+
monikerRange: ">=msvc-170"
8+
---
9+
# `lnt-naming-convention`
10+
11+
Ensure that the naming convention for symbols aligns with your coding style, as specified in the project's `.editorconfig` file.
12+
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. As an example, the naming conventions for Unreal Engine projects are specified in an `.editorconfig` on [GitHub](https://raw.githubusercontent.com/microsoft/vc-ue-extensions/main/Source/.editorconfig).
14+
15+
Once you have the `.editorconfig` file in your project, turn on the `lnt-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+
17+
## Example
18+
19+
Suppose that you have an `.editorconfig` file that contains:
20+
21+
```
22+
cpp_naming_style.boolean_style.capitalization = pascal_case
23+
cpp_naming_style.boolean_style.required_prefix = b
24+
```
25+
26+
The linter flags the following code because it isn't prefixed with 'b' and because it isn't Pascal case, as specified in the `.editorconfig` file:
27+
28+
```cpp
29+
void example()
30+
{
31+
bool myFlag = true; // flagged because it doesn't follow the naming convention specified in the .editorconfig
32+
}
33+
```
34+
35+
## How to fix the issue
36+
37+
Change the naming to match the style specified in the `.editorconfig`:
38+
39+
```cpp
40+
void example()
41+
{
42+
bool bMyFlag = true; // fixed to follow the code style specified in the .editorconfig
43+
}
44+
```
45+
46+
The editor can make the change for you. Place the cursor on the flagged symbol. Choose **Show potential fixes** and then **Apply naming convention**:
47+
48+
:::image type="complex" source="media/lnt-apply-naming-convention.png" alt-text="Screenshot of the IDE suggesting applying naming convention.":::
49+
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.
50+
:::image-end:::
51+
52+
## Remarks
53+
54+
Introduced in Visual Studio 2022 17.7, the `lnt-naming-convention` linter check ensures that naming conventions align with those specified in the `.editorconfig` file. You can apply this check to any project that has an `.editorconfig` file. You can also customize your `.editorconfig` file to suit your project's coding style.
55+
56+
## See also
57+
58+
[Create portable, custom editor settings with EditorConfig](/visualstudio/ide/create-portable-custom-editor-options)\
59+
[IntelliSense code linter for C++ overview](cpp-linter-overview.md)
23.4 KB
Loading
Loading

docs/ide/toc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ 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-make-member-function-const
34+
href: ../ide/lnt-make-member-function-const.md
35+
- name: lnt-naming-convention
36+
href: ../ide/lnt-naming-convention.md
3337
- name: lnt-uninitialized-local
3438
href: ../ide/lnt-uninitialized-local.md
3539
- name: Change signature

0 commit comments

Comments
 (0)