Skip to content

Commit cc3b7c2

Browse files
authored
Merge pull request #3958 from kylereedmsft/linter_updates
Add docs for new linter check
2 parents 68d28df + c51b7e3 commit cc3b7c2

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

docs/ide/cpp-linter-overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ When you change the check severity level, it changes how the problem is shown in
4646

4747
## Known issues
4848

49-
::: moniker range=">=msvc-170"
49+
::: moniker range="msvc-170"
5050

5151
- 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.
5252

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
title: lnt-comparison-bitwise-precedence
3+
description: "Reference for Visual Studio C++ IntelliSense Linter check lnt-comparison-bitwise-precedence."
4+
ms.date: 09/29/2021
5+
f1_keywords: ["lnt-comparison-bitwise-precedence"]
6+
helpviewer_keywords: ["lnt-comparison-bitwise-precedence"]
7+
monikerRange: ">=msvc-160"
8+
---
9+
# `lnt-comparison-bitwise-precedence`
10+
11+
The comparison operator has a higher precedence than the bitwise operator.
12+
13+
The comparison operator will be evaluated first. The result will be implicitly cast to an integer and used as an operand in the bitwise operation. Parentheses are needed to force the expected order of operations.
14+
15+
The `lnt-comparison-bitwise-precedence` check is controlled by the **Comparison/Bitwise Precedence** 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+
bool is_flag_set(unsigned value, unsigned flag)
21+
{
22+
return value & flag == flag; // Flagged: `flag == flag` is evaluated first.
23+
// Then `value & (int)true` is evaluated which
24+
// returns an incorrect result in most cases.
25+
}
26+
```
27+
28+
```cpp
29+
bool is_flag_set(unsigned value, unsigned flag)
30+
{
31+
return (value & flag) == flag; // Correct
32+
}
33+
```
34+
35+
## How to fix the issue
36+
37+
The fix the linter suggests is to add parentheses around the bitwise operation so it will be evaluated first.
38+
39+
## See also
40+
41+
[IntelliSense code linter for C++ overview](cpp-linter-overview.md)

docs/ide/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ items:
2424
href: ../ide/lnt-arithmetic-overflow.md
2525
- name: lnt-assignment-equality
2626
href: ../ide/lnt-assignment-equality.md
27+
- name: lnt-comparison-bitwise-precedence
28+
href: ../ide/lnt-comparison-bitwise-precedence.md
2729
- name: lnt-integer-float-division
2830
href: ../ide/lnt-integer-float-division.md
2931
- name: lnt-logical-bitwise-mismatch

0 commit comments

Comments
 (0)