Skip to content

Commit ade0d19

Browse files
authored
Merge pull request #4131 from Xazax-hun/dev/gahorvat/add_enum_checks
Add missing documentation to 3 new enum related checks.
2 parents 2d8cd25 + d37a405 commit ade0d19

File tree

4 files changed

+171
-0
lines changed

4 files changed

+171
-0
lines changed

docs/code-quality/c26813.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
description: "Learn more about: C26813"
3+
title: c26813
4+
keywords: c26813
5+
ms.date: 02/23/2022
6+
ms.topic: reference
7+
f1_keywords: ["C26813"]
8+
helpviewer_keywords: ["C26813"]
9+
dev_langs: ["C++"]
10+
---
11+
# C26813
12+
13+
> Warning C26813: Use 'bitwise and' to check if a flag is set
14+
15+
Most `enum`s with power of two member values are intended to be used with bit flags. As a result, we rarely want to equality compare these flags. We want to extract the bits we are interested in using bitwise operations instead.
16+
17+
## Example
18+
19+
```cpp
20+
enum BitWise
21+
{
22+
A = 1,
23+
B = 2,
24+
C = 4
25+
};
26+
27+
void useEqualsWithBitwiseEnum(BitWise a)
28+
{
29+
if (a == B) // Warning C26813: Use 'bitwise and' to check if a flag is set
30+
return;
31+
}
32+
```
33+
34+
To fix the warning, use bitwise operations:
35+
36+
```cpp
37+
void useEqualsWithBitwiseEnum(BitWise a)
38+
{
39+
if (a & B) // Fixed.
40+
return;
41+
}
42+
```
43+
44+
45+
## See also
46+
47+
[C26827](./c26827.md)
48+
[C26828](./c26828.md)

docs/code-quality/c26827.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
description: "Learn more about: C26827"
3+
title: c26827
4+
keywords: c26814
5+
ms.date: 02/23/2022
6+
ms.topic: reference
7+
f1_keywords: ["C26827"]
8+
helpviewer_keywords: ["C26827"]
9+
dev_langs: ["C++"]
10+
---
11+
# C26827
12+
13+
> Warning C26827: Did you forgot to initialize an enum or wanted to use another type?
14+
15+
Most `enum` types used in bitwise operations are expected to have members with values of powers of two. This warning attempts to find cases where we forgot to give a value to an enum constant explicitly or inadvertently used the wrong enum type.
16+
17+
## Example
18+
19+
```cpp
20+
enum class AlmostBitWise
21+
{
22+
A = 1,
23+
B = 2,
24+
C = 4,
25+
D
26+
};
27+
28+
int almostBitwiseEnums(AlmostBitWise a, bool cond)
29+
{
30+
return (int)a|(int)AlmostBitWise::A; // Warning C26827: Did you forgot to initialize an enum or wanted to use another type?
31+
}
32+
```
33+
34+
To fix the warning, initialize the enum constant to the correct value or use the correct enum type in the operation.
35+
36+
```cpp
37+
enum class AlmostBitWise
38+
{
39+
A = 1,
40+
B = 2,
41+
C = 4,
42+
D = 8
43+
};
44+
45+
int almostBitwiseEnums(AlmostBitWise a, bool cond)
46+
{
47+
return (int)a|(int)AlmostBitWise::A; // No warning.
48+
}
49+
```
50+
51+
## See also
52+
53+
[C26813](./c26813.md)
54+
[C26828](./c26828.md)

docs/code-quality/c26828.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
description: "Learn more about: C26828"
3+
title: c26828
4+
keywords: c26828
5+
ms.date: 02/23/2022
6+
ms.topic: reference
7+
f1_keywords: ["C26828"]
8+
helpviewer_keywords: ["C26828"]
9+
dev_langs: ["C++"]
10+
---
11+
# C26828
12+
13+
> Warning C26828: Different enum types have overlapping values. Did you want to use another enum constant here?
14+
15+
Most of the times we use a single enum to describe all the bit flags we can use for an option. If we use two different enum types in the same bitwise expression where the enums have overlapping values the chances are good those enums were not designed to be used in the expression.
16+
17+
## Example
18+
19+
```cpp
20+
21+
enum BitWiseA
22+
{
23+
A = 1,
24+
B = 2,
25+
C = 4
26+
};
27+
28+
enum class BitWiseB
29+
{
30+
AA = 1,
31+
BB = 2,
32+
CC = 4,
33+
All = 7
34+
};
35+
36+
int overlappingBitwiseEnums(BitWiseA a)
37+
{
38+
return (int)a|(int)BitWiseB::AA; // Warning C26828: Different enum types have overlapping values. Did you want to use another enum constant here?
39+
}
40+
```
41+
42+
To fix the warning make sure `enum`s that are designed to be used together have no overlapping values or make sure all the related options are in a single `enum`.
43+
44+
```cpp
45+
46+
enum BitWiseA
47+
{
48+
A = 1,
49+
B = 2,
50+
C = 4
51+
};
52+
53+
int overlappingBitwiseEnums(BitWiseA a)
54+
{
55+
return (int)a|(int)BitWiseA::A; // No warning.
56+
}
57+
```
58+
59+
## See also
60+
61+
[C26813](./c26813.md)
62+
[C26827](./c26827.md)
63+

docs/code-quality/toc.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,8 +579,14 @@ items:
579579
href: ../code-quality/c26810.md
580580
- name: C26811
581581
href: ../code-quality/c26811.md
582+
- name: C26813
583+
href: ../code-quality/c26813.md
582584
- name: C26826
583585
href: ../code-quality/c26826.md
586+
- name: C26827
587+
href: ../code-quality/c26827.md
588+
- name: C26828
589+
href: ../code-quality/c26828.md
584590
- name: C28020
585591
href: ../code-quality/c28020.md
586592
- name: C28021

0 commit comments

Comments
 (0)