Skip to content

Commit a716b0b

Browse files
committed
Merging changes synced from https://github.com/MicrosoftDocs/cpp-docs-pr (branch live)
2 parents db0584c + 589cc8f commit a716b0b

10 files changed

+449
-0
lines changed

docs/ide/cpp-linter-overview.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
description: "Learn more about: IntelliSense code linter for C++"
3+
title: IntelliSense code linter for C++ overview
4+
ms.date: 09/30/2021
5+
ms.topic: conceptual
6+
helpviewer_keywords:
7+
- "C/C++, linter"
8+
- "C++, linter"
9+
- "IDE, linter"
10+
- "IntelliSense, linter"
11+
- "lightbulbs, linter"
12+
- "suggested actions, linter"
13+
monikerRange: ">=msvc-160"
14+
---
15+
# IntelliSense code linter for C++ overview
16+
17+
The IntelliSense code linter for C++ helps developers find and fix common C++ problems right inside Visual Studio. It's based on the same engine that provides C++ IntelliSense, so problems are flagged as soon as you type them.
18+
19+
![Animation showing the C++ linter in action.](../ide/media/linter-demo-animation.gif)
20+
21+
## Find problems
22+
23+
::: moniker range=">=msvc-170"
24+
25+
In Visual Studio 2019, the C++ linter is available as an option. Starting in Visual Studio 2022, the C++ Linter is enabled by default. To use it, just open a source file in the editor. The linter shows any problems it finds by annotations in the editor window and in the Error List window.
26+
27+
::: moniker-end
28+
::: moniker range="msvc-160"
29+
30+
In Visual Studio 2019, the C++ linter is available as an option. To enable it, follow the instructions in [Configure the linter](#configure-the-linter). Then just open a source file in the editor. The linter shows any problems it finds by annotations in the editor window and in the Error List window.
31+
32+
::: moniker-end
33+
34+
## Fix problems
35+
36+
Most of the linter checks have suggestions for fixing the problem. Hover over the error squiggle and choose the light bulb that pops up to see the suggestions. A preview diff of the suggested change is shown, so you can confirm the change makes sense before you apply it.
37+
38+
## <a name="configure-the-linter"> Configure the linter
39+
40+
You can enable or disable the linter, or configure the severity level for each check, in the C++ Code Style options.
41+
42+
To change the linter options, on the menu bar, select **Tools** > **Options**. In the Options dialog, expand **Text Editor** > **C/C++** > **Code Style** > **Linter**.
43+
44+
By default, many of the checks have **Suggestion** severity so the Linter results aren't intrusive while you write code. You can set the severity to **Warning** or **Error**. Individual checks can be disabled by changing their severity to **None**.
45+
46+
When you change the check severity level, it changes how the problem is shown in the editor window and in the Error List window. Changes take effect for newly opened files.
47+
48+
::: moniker range=">=msvc-170"
49+
50+
![Screenshot that shows the C&#43;&#43; linter configuration.](../ide/media/linter-settings.png)
51+
52+
(The presentation in Visual Studio 2019 is slightly different, but the options are similar.)
53+
54+
::: moniker-end
55+
56+
## Known issues
57+
58+
::: moniker range=">=msvc-170"
59+
60+
- The **Comparison/Bitwise Precedence** check isn't available in the initial release of Visual Studio 2022, even though you can configure it in the Options dialog.
61+
62+
::: moniker-end
63+
::: moniker range="msvc-160"
64+
65+
- The **Comparison/Bitwise Mismatch** check isn't available in Visual Studio 2019, even though you can configure it in the Options dialog.
66+
67+
::: moniker-end
68+
69+
## See also
70+
71+
[C++ Team Blog - IntelliSense Code Linter for C++](https://devblogs.microsoft.com/cppblog/intellisense-code-linter-for-cpp/)

docs/ide/lnt-accidental-copy.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
title: lnt-accidental-copy
3+
description: "Reference for Visual Studio C++ IntelliSense Linter check lnt-accidental-copy."
4+
ms.date: 09/29/2021
5+
f1_keywords: ["lnt-accidental-copy"]
6+
helpviewer_keywords: ["lnt-accidental-copy"]
7+
monikerRange: ">=msvc-160"
8+
---
9+
# `lnt-accidental-copy`
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-accidental-copy` 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/lnt-arithmetic-overflow.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
title: lnt-arithmetic-overflow
3+
description: "Reference for Visual Studio C++ IntelliSense Linter check lnt-arithmetic-overflow."
4+
ms.date: 09/29/2021
5+
f1_keywords: ["lnt-arithmetic-overflow"]
6+
helpviewer_keywords: ["lnt-arithmetic-overflow"]
7+
monikerRange: ">=msvc-160"
8+
---
9+
# `lnt-arithmetic-overflow`
10+
11+
An arithmetic expression may overflow before being converted to a wider type.
12+
13+
In C and C++, arithmetic operations are evaluated using the widest type of the operands, not the width of the type assigned the result. When a result is converted to a wider type, it indicates the developer expects the operation may overflow the narrower types of the operands.
14+
15+
The `lnt-arithmetic-overflow` check is controlled by the **Arithmetic Overflow** 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+
void overflow(int a, int b) {
21+
int64_t mul = a * b; // Flagged: 32-bit operation may overflow.
22+
int64_t shift = a << 34; // Flagged: Shift would overflow.
23+
24+
int64_t mul2 = mul + b; // OK: 'mul' is 64-bit so the addition expression is
25+
// evaluated using 64-bit operations.
26+
}
27+
```
28+
29+
## How to fix this issue
30+
31+
The fix the linter suggests is to explicitly widen one of the operands. Then the entire expression is evaluated at the wider result type, as shown in this example:
32+
33+
```cpp
34+
void overflow(int a, int b) {
35+
int64_t mul = static_cast<int64_t>(a) * b;
36+
int64_t shift = static_cast<int64_t>(a) << 34;
37+
}
38+
```
39+
40+
## See also
41+
42+
[IntelliSense code linter for C++ overview](cpp-linter-overview.md)

docs/ide/lnt-assignment-equality.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
title: lnt-assignment-equality
3+
description: "Reference for Visual Studio C++ IntelliSense Linter check lnt-assignment-equality."
4+
ms.date: 09/29/2021
5+
f1_keywords: ["lnt-assignment-equality"]
6+
helpviewer_keywords: ["lnt-assignment-equality"]
7+
monikerRange: ">=msvc-160"
8+
---
9+
# `lnt-assignment-equality`
10+
11+
A variable is assigned a constant in a Boolean context.
12+
13+
An assignment expression that takes a constant always evaluates to the value of the constant. A comparison operation, such as `==` or `!=`, was probably intended instead.
14+
15+
::: moniker range=">=msvc-170"
16+
17+
In Visual Studio 2022, The `lnt-assignment-equality` check is controlled by the **Accidental Assignment** 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).
18+
19+
::: moniker-end
20+
::: moniker range="msvc-160"
21+
22+
In Visual Studio 2019, the `lnt-assignment-equality` check is controlled by the **Assignation Instead of Equality** 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).
23+
24+
::: moniker-end
25+
26+
## Examples
27+
28+
```cpp
29+
int read();
30+
31+
void accidental_assignment(int i)
32+
{
33+
if (i = 2) {} // Flagged: 'i' is being assigned to a constant.
34+
35+
if (i = read()) {} // OK: 'i' is being assigned the result of a function call.
36+
37+
while (i = 0) {} // Flagged.
38+
}
39+
```
40+
41+
## How to fix the issue
42+
43+
The fix the linter suggests is to change the assignment operator to an equality operator.
44+
45+
```cpp
46+
void accidental_assignment(int i)
47+
{
48+
if (i == 2) {}
49+
50+
while (i != 0) {}
51+
}
52+
```
53+
54+
## Remarks
55+
56+
This check only flags assignment from a constant or constant expression.
57+
58+
## Known Issues
59+
60+
```cpp
61+
void known_issues(bool b) {
62+
if (b = true) {} // Not flagged because there is no implicit conversion to bool.
63+
}
64+
```
65+
66+
## See also
67+
68+
[IntelliSense code linter for C++ overview](cpp-linter-overview.md)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
title: lnt-integer-float-division
3+
description: "Reference for Visual Studio C++ IntelliSense Linter check lnt-integer-float-division."
4+
ms.date: 09/29/2021
5+
f1_keywords: ["lnt-integer-float-division"]
6+
helpviewer_keywords: ["lnt-integer-float-division"]
7+
monikerRange: ">=msvc-160"
8+
---
9+
# `lnt-integer-float-division`
10+
11+
An integer division expression was implicitly cast to a floating-point type.
12+
13+
The division is carried out using integer operations, which truncates the fractional part before it's assigned to the floating-point result type. This check doesn't always indicate a bug, because sometimes the truncation is intentional.
14+
15+
::: moniker range=">=msvc-170"
16+
17+
In Visual Studio 2022, the `lnt-integer-float-division` check is controlled by the **Truncated Division Result** 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).
18+
19+
::: moniker-end
20+
::: moniker range="msvc-160"
21+
22+
In Visual Studio 2019, the `lnt-integer-float-division` check is controlled by the **Integer division converted to floating point** 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).
23+
24+
::: moniker-end
25+
26+
## Examples
27+
28+
```cpp
29+
float divide(int i, int j) {
30+
return i / j; // Flagged: The integer division result is implicitly cast to float.
31+
}
32+
33+
float half(int i) {
34+
return i / 2; // Flagged: An integer literal is used.
35+
}
36+
```
37+
38+
## How to fix the issue
39+
40+
The fix the linter suggests is to explicitly cast one of the division operands to a floating-point type, so the division result isn't truncated. You can also use a floating-point literal instead of a cast.
41+
42+
```cpp
43+
float divide(int i, int j) {
44+
return static_cast<float>(i) / j;
45+
}
46+
47+
float half(int i) {
48+
return i / 2.0;
49+
}
50+
```
51+
52+
## Remarks
53+
54+
If the truncation is intentional, you can add an explicit cast to prevent the warning.
55+
56+
```cpp
57+
float int_divide(int i, int j) {
58+
return static_cast<float>(i / j); // Not flagged because of the explicit cast.
59+
}
60+
```
61+
62+
## See also
63+
64+
[IntelliSense code linter for C++ overview](cpp-linter-overview.md)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
title: lnt-logical-bitwise-mismatch
3+
description: "Reference for Visual Studio C++ IntelliSense Linter check lnt-logical-bitwise-mismatch."
4+
ms.date: 09/29/2021
5+
f1_keywords: ["lnt-logical-bitwise-mismatch"]
6+
helpviewer_keywords: ["lnt-logical-bitwise-mismatch"]
7+
monikerRange: ">=msvc-160"
8+
---
9+
# `lnt-logical-bitwise-mismatch`
10+
11+
A logical operator was used with integer values or a bitwise operator was used with Boolean values.
12+
13+
While the flagged code still compiles, it's considered bad practice to use operators incorrectly: it makes the code harder to read or modify, and may cause runtime errors.
14+
15+
The `lnt-logical-bitwise-mismatch` check is controlled by the **Logical/Bitwise Mismatch** 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+
Only use logical operators on Boolean values.
20+
21+
```cpp
22+
void example(bool a, bool b) {
23+
bool c = a & b; // Flagged: Bitwise AND operator used with Boolean variables.
24+
bool d = a || b; // OK: Logical OR operator used with Boolean variables.
25+
}
26+
27+
```
28+
29+
Only use bitwise operators on integer values.
30+
31+
```cpp
32+
void example(int i, int j) {
33+
int k = i && j; // Flagged: Logical AND operator used with integer variables.
34+
// The runtime behavior will be incorrect in almost all cases.
35+
bool l = i ^ j; // OK: Bitwise XOR operator used with integer variables.
36+
}
37+
```
38+
39+
## How to fix the issue
40+
41+
The fix the linter suggests is to use the correct operator for the operand type.
42+
43+
## Known issues
44+
45+
Alternative tokens such as `and` or `bitor` are flagged by the check, but the suggested fix is incorrect.
46+
47+
## See also
48+
49+
[IntelliSense code linter for C++ overview](cpp-linter-overview.md)

0 commit comments

Comments
 (0)